我正在使用activeMQ,iam通过队列从生产者向消费者发送消息。假设我的消费者由于某种原因失败了。 我已将消息的到期时间设置为15分钟,因此来自生产者的消息将保留在队列中并在15分钟后过期。 我如何以编程方式检查&如果activeMQ消息已经过期,或者不在队列中的,而不是消费者中,那么最终会收到通知(这是我的设计)。
代码段如下。
初始化代码
public void init() {
try {
final LocalProperties config = new LocalProperties();
final ConnectionFactory factory = new ActiveMQConnectionFactory(config.getActiveMqConnection());
this.connection = factory.createConnection(config.getActiveMqUser(), config.getActiveMqPassword());
this.connection.start();
this.session = this.connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
final Destination destination = this.session.createQueue(QUEUE_NAME);
this.producer = this.session.createProducer(destination);
this.producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} catch (final JMSException e) {
_LOG.error("We could not open the active mq connection", e);
}
}
制片人代码
public Response PostMessages(
final MessageDelivery body, final String messageId) {
try {
this.openConnection();
// Create a messages
final Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
final String json = gson.toJson(body);
final TextMessage request = this.session.createTextMessage(json);
request.setLongProperty(
"expiryTime",
900000
);
request.setStringProperty("messageType", "com/messages/post");
request.setBooleanProperty("deliveryNotification", false);
request.setStringProperty("destination", "XXXX");
request.setStringProperty("destinationType", "FILTER");
_LOG.info(request.getText());
// Tell the producer to send the message
producer.send(request);
//How to get the expired message after producer.send(request)
return Response.status(Response.Status.ACCEPTED).build();
} catch (final Exception e) {
_LOG.error("We could not send the message", e);
return Response.status(Response.Status.BAD_REQUEST).build();
}
}*
答案 0 :(得分:1)
默认情况下,如果邮件过期且邮件不是持久性的,则会被丢弃,应用程序将无法获取该邮件。如果它是AMQ中的持久设置,它将被移动到DLQ