[英]how to check if a message has expired or not in activemq in the producer
我正在使用 activeMQ,我正在通过队列从生产者向消费者发送消息。假设我的消费者由于某种原因失败了。 我已将消息的到期时间设置为 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();
}
}*
默认情况下,如果消息过期并且消息不是持久的,它将被丢弃并且应用程序将无法获取它。 如果它是您在 AMQ 中的持久设置,它将被移动到 DLQ
此处的详细信息: 自动丢弃过期消息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.