簡體   English   中英

如何在生產者的activemq中檢查消息是否已過期

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM