0
Iam使用activeMQ构建应用程序,其中有一个生产者和一个消费者。 消费者使用MessageListener异步收听来自生产者的消息,这是通过使用名为onMessage(消息消息)的方法完成的。 但在使用消息之前,我想执行条件检查,然后使用消息。 我不想使用消息的同步消费,因为它会违反我的设计。这里像检测因特网连接等如何暂停和恢复异步使用JMS消息
public void onMessage(final Message message) {
Preconditions.checkNotNull(message);
if (!(message instanceof TextMessage)) {
_LOG.error("The message is not of type TextMessage but of type {} so we could not process", message.getClass().getSimpleName());
throw new IllegalArgumentException("This type '" + message.getClass().getSimpleName() + "' of message could not been handled");
}
try {
final String messageType = message.getStringProperty("messageType");
Preconditions.checkNotNull(messageType);
_LOG.info("The MessageType is {}", messageType);
final String msg = ((TextMessage) message).getText();
Preconditions.checkNotNull(msg);
_LOG.debug(msg);
process(messageType, msg);
} catch (final JMSException e) {
_LOG.error("We could not read the message", e);
}
}
任何代码示例的
void initialize() throws JMSException {
this.connection = this.connectionFactory.createConnection();
this.connection.start();
final Session session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = session.createQueue("testQ");
this.consumer = session.createConsumer(destination);
this.consumer.setMessageListener(this);
}
检查条件将是巨大的。