Example usage for javax.jms TopicSubscriber receive

List of usage examples for javax.jms TopicSubscriber receive

Introduction

In this page you can find the example usage for javax.jms TopicSubscriber receive.

Prototype


Message receive(long timeout) throws JMSException;

Source Link

Document

Receives the next message that arrives within the specified timeout interval.

Usage

From source file:org.apache.activemq.usecases.DurableSubscriptionHangTestCase.java

private Message collectMessagesFromDurableSubscriptionForOneMinute() throws Exception {
    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory("vm://" + brokerName);
    TopicConnection connection = connectionFactory.createTopicConnection();

    connection.setClientID(clientID);// w ww .j ava 2s .com
    TopicSession topicSession = connection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
    Topic topic = topicSession.createTopic(topicName);
    connection.start();
    TopicSubscriber subscriber = topicSession.createDurableSubscriber(topic, durableSubName);
    LOG.info("About to receive messages");
    Message message = subscriber.receive(120000);
    subscriber.close();
    connection.close();
    LOG.info("collectMessagesFromDurableSubscriptionForOneMinute done");

    return message;
}

From source file:org.grouter.common.jms.TopicSenderDestination.java

@Override
public Message waitAndGetReplyFromTemporaryDestination(long waitForMs) {
    TopicSubscriber receiver = null;
    try {//from   w  ww.ja va  2 s .c o m
        if (!useTemporaryReplyDestination) {
            throw new IllegalStateException("You have used this destination in a wrong way. Have you "
                    + "used correct constructor for temporary destinations?");
        }
        receiver = topicSession.createSubscriber(getTemporaryDestination());
        return receiver.receive(waitForMs);
    } catch (JMSException ex) {
        logger.warn("Waiting for reply on temp topic failed", ex);
        return null;
    } finally {
        try {
            if (receiver != null) {
                receiver.close();
            }
        } catch (JMSException ex1) {
            //ignore
        }
    }
}