如何在生产者的activemq中检查消息是否已过期

时间:2016-09-21 08:53:22

标签: java jms activemq jms-topic activemq-artemis

我正在使用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();
        }
    }*

1 个答案:

答案 0 :(得分:1)

默认情况下,如果邮件过期且邮件不是持久性的,则会被丢弃,应用程序将无法获取该邮件。如果它是AMQ中的持久设置,它将被移动到DLQ

详细信息:Automatically Discard Expired Messages