Example usage for javax.jms Message getJMSRedelivered

List of usage examples for javax.jms Message getJMSRedelivered

Introduction

In this page you can find the example usage for javax.jms Message getJMSRedelivered.

Prototype


boolean getJMSRedelivered() throws JMSException;

Source Link

Document

Gets an indication of whether this message is being redelivered.

Usage

From source file:org.wso2.mb.integration.common.clients.operations.queue.QueueMessageReceiver.java

public void run() {
    try {//  w w  w  .  j a  v a2s.  c  o m
        if (useMessageListener) {
            QueueMessageListener messageListener = new QueueMessageListener(queueConnection, queueSession,
                    queueReceiver, queueName, messageCounter, delayBetweenMessages, printNumberOfMessagesPer,
                    isToPrintEachMessage, fileToWriteReceivedMessages, stopAfter, ackAfterEach, commitAfterEach,
                    rollbackAfterEach);

            queueReceiver.setMessageListener(messageListener);
        } else {
            int localMessageCount = 0;
            while (true) {
                Message message = queueReceiver.receive();
                if (message != null && message instanceof TextMessage) {
                    messageCounter.incrementAndGet();
                    localMessageCount++;

                    String redelivery;
                    TextMessage textMessage = (TextMessage) message;
                    if (message.getJMSRedelivered()) {
                        redelivery = "REDELIVERED";
                    } else {
                        redelivery = "ORIGINAL";
                    }
                    if (messageCounter.get() % printNumberOfMessagesPer == 0) {
                        log.info("[QUEUE RECEIVE] ThreadID:" + Thread.currentThread().getId() + " queue:"
                                + queueName + " localMessageCount:" + localMessageCount + " totalMessageCount:"
                                + messageCounter.get() + " max count:" + stopAfter);
                    }
                    if (isToPrintEachMessage) {
                        log.info("(count:" + messageCounter.get() + "/threadID:"
                                + Thread.currentThread().getId() + "/queue:" + queueName + ") " + redelivery
                                + " >> " + textMessage.getText());
                        AndesClientUtils.writeToFile(textMessage.getText(), fileToWriteReceivedMessages);
                    }
                }

                if (messageCounter.get() % ackAfterEach == 0) {
                    if (queueSession.getAcknowledgeMode() == QueueSession.CLIENT_ACKNOWLEDGE) {
                        if (message != null) {
                            message.acknowledge();
                        }
                    }
                }

                //commit get priority
                if (messageCounter.get() % commitAfterEach == 0) {
                    queueSession.commit();
                    log.info("Committed Queue Session");
                } else if (messageCounter.get() % rollbackAfterEach == 0) {
                    queueSession.rollback();
                    log.info("Rollbacked Queue Session");
                }

                if (messageCounter.get() == stopAfter) {
                    stopListening();
                    break;
                }
                if (delayBetweenMessages != 0) {
                    try {
                        Thread.sleep(delayBetweenMessages);
                    } catch (InterruptedException e) {
                        //silently ignore
                    }
                }
            }
        }
    } catch (JMSException e) {
        log.error("Error while listening for messages", e);
    }

}

From source file:org.wso2.mb.integration.common.clients.operations.topic.TopicMessageListener.java

public void onMessage(Message message) {
    messageCount.incrementAndGet();//from  w w w.j  ava  2s .  c  o m
    localMessageCount++;
    TextMessage receivedMessage = (TextMessage) message;
    try {
        String redelivery = "";
        if (message.getJMSRedelivered()) {
            redelivery = "REDELIVERED";
        } else {
            redelivery = "ORIGINAL";
        }

        if (messageCount.get() % printNumberOfMessagesPer == 0) {
            log.info("[TOPIC RECEIVE] ThreadID:" + Thread.currentThread().getId() + " topic:" + topicName + " "
                    + "localMessageCount:" + localMessageCount + " totalMessageCount:" + messageCount.get()
                    + " max" + " count:" + stopMessageCount);
        }
        if (isToPrintEachMessage) {
            log.info("(count:" + messageCount.get() + "/threadID:" + Thread.currentThread().getId() + "/topic:"
                    + topicName + ") " + redelivery + " >> " + receivedMessage.getText());
            AndesClientUtils.writeToFile(receivedMessage.getText(), fileToWriteReceivedMessages);
        }

        if (messageCount.get() % ackAfterEach == 0) {
            if (topicSession.getAcknowledgeMode() == QueueSession.CLIENT_ACKNOWLEDGE) {
                receivedMessage.acknowledge();
            }
        }

        //commit get priority
        if (messageCount.get() % commitPerMessageCount == 0) {
            topicSession.commit();
            log.info("Committed Topic Session");
        } else if (messageCount.get() % rollbackPerMessagecount == 0) {
            topicSession.rollback();
            log.info("Rollbacked Topic Session");
        }

        if (messageCount.get() >= unsubscribeMessageCount) {
            unsubscribeConsumer();
            AndesClientUtils.sleepForInterval(200);
        } else if (messageCount.get() >= stopMessageCount) {
            stopMessageListener();
            AndesClientUtils.sleepForInterval(200);
        }

        if (delayBetweenMessages != 0) {
            try {
                Thread.sleep(delayBetweenMessages);
            } catch (InterruptedException e) {
                //silently ignore
            }
        }
    } catch (NumberFormatException e) {
        log.error("Wrong inputs.", e);
    } catch (JMSException e) {
        log.error("JMS Exception", e);
    }
}

From source file:org.wso2.mb.integration.common.clients.operations.topic.TopicMessageReceiver.java

public void run() {
    try {/*w w w . j a  va2  s.c  o m*/
        if (useMessageListener) {
            TopicMessageListener messageListener = new TopicMessageListener(topicConnection, topicSession,
                    topicSubscriber, topicName, subscriptionId, messageCounter, delayBetweenMessages,
                    printNumberOfMessagesPer, isToPrintEachMessage, fileToWriteReceivedMessages, stopAfter,
                    unSubscribeAfter, ackAfterEach, rollbackAfterEach, commitAfterEach);
            topicSubscriber.setMessageListener(messageListener);
        } else {

            int localMessageCount = 0;
            while (true) {
                Message message = topicSubscriber.receive();
                if (message != null && message instanceof TextMessage) {
                    messageCounter.incrementAndGet();
                    localMessageCount++;
                    String redelivery;
                    TextMessage textMessage = (TextMessage) message;
                    if (message.getJMSRedelivered()) {
                        redelivery = "REDELIVERED";
                    } else {
                        redelivery = "ORIGINAL";
                    }
                    if (messageCounter.get() % printNumberOfMessagesPer == 0) {
                        log.info("[TOPIC RECEIVE] ThreadID:" + Thread.currentThread().getId() + " topic:"
                                + topicName + " localMessageCount:" + localMessageCount + " totalMessageCount:"
                                + messageCounter.get() + " max count:" + stopAfter);
                    }
                    if (isToPrintEachMessage) {
                        log.info("(count:" + messageCounter.get() + "/threadID:"
                                + Thread.currentThread().getId() + "/topic:" + topicName + ") " + redelivery
                                + " >> " + textMessage.getText());
                        AndesClientUtils.writeToFile(textMessage.getText(), fileToWriteReceivedMessages);
                    }
                }

                if (messageCounter.get() % ackAfterEach == 0) {
                    if (topicSession.getAcknowledgeMode() == QueueSession.CLIENT_ACKNOWLEDGE) {
                        if (message != null) {
                            message.acknowledge();
                        }
                    }
                }

                //commit get priority
                if (messageCounter.get() % commitAfterEach == 0) {
                    topicSession.commit();
                    log.info("Committed session");
                } else if (messageCounter.get() % rollbackAfterEach == 0) {
                    topicSession.rollback();
                    log.info("Rollbacked session");
                }

                if (messageCounter.get() >= unSubscribeAfter) {
                    unsubscribe();
                    break;
                } else if (messageCounter.get() >= stopAfter) {
                    stopListening();
                    break;
                }

                if (delayBetweenMessages != 0) {
                    try {
                        Thread.sleep(delayBetweenMessages);
                    } catch (InterruptedException e) {
                        //silently ignore
                    }
                }
            }
        }
    } catch (JMSException e) {
        log.error("Error while listening for messages", e);
    }
}

From source file:rewards.messaging.server.DiningListenerImpl.java

private void logMessage(Message message, Dining dining) throws JMSException {
    if (log.isDebugEnabled()) {
        String msg = "Received Dining with amount " + dining.getAmount();
        if (message.getJMSRedelivered()) {
            int nrOfDeliveries = message.getIntProperty("JMSXDeliveryCount");
            msg += " (redelivered " + (nrOfDeliveries - 1) + " times)";
        }//  ww  w.j  ava  2 s.com
        log.debug(msg);
    }
}