简体   繁体   中英

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. 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. 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).

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

Details here : Automatically Discard Expired Messages

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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