简体   繁体   English

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

[英]how to check if a message has expired or not in activemq in the producer

Iam working on activeMQ where iam sending a message to the consumer from producer via the queue.Assume that my consumer has failed due to some reason.我正在使用 activeMQ,我正在通过队列从生产者向消费者发送消息。假设我的消费者由于某种原因失败了。 I have set the expiry time to the message as 15 mins so the message from producer stays on the queue and expires after 15 minutes.我已将消息的到期时间设置为 15 分钟,因此来自生产者的消息保留在队列中并在 15 分钟后到期。 How do i check programatically & ultimately get notified if the activeMQ message has expired or not in queue in producer only not in consumer (This is due my design).如果 activeMQ 消息已过期或不在生产者队列中而不是消费者队列中,我如何以编程方式检查并最终得到通知(这是由于我的设计)。

The code snippet is as below.代码片段如下。

Initialization code初始化代码

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);
        }
    }

Producer Code生产者代码

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();
        }
    }*

By default , if a message is expired and the message is not persistent it will be discarded and the application will not be able to get it.默认情况下,如果消息过期并且消息不是持久的,它将被丢弃并且应用程序将无法获取它。 If it is a persistent setting you have in AMQ it will be moved to the DLQ如果它是您在 AMQ 中的持久设置,它将被移动到 DLQ

Details here : Automatically Discard Expired Messages此处的详细信息: 自动丢弃过期消息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM