Example usage for org.springframework.jms.listener DefaultMessageListenerContainer CACHE_CONSUMER

List of usage examples for org.springframework.jms.listener DefaultMessageListenerContainer CACHE_CONSUMER

Introduction

In this page you can find the example usage for org.springframework.jms.listener DefaultMessageListenerContainer CACHE_CONSUMER.

Prototype

int CACHE_CONSUMER

To view the source code for org.springframework.jms.listener DefaultMessageListenerContainer CACHE_CONSUMER.

Click Source Link

Document

Constant that indicates to cache a shared JMS Connection , a JMS Session , and a JMS MessageConsumer for each listener thread.

Usage

From source file:ch.algotrader.event.TopicEventDumper.java

public static void main(String... args) throws Exception {

    SingleConnectionFactory jmsActiveMQFactory = new SingleConnectionFactory(
            new ActiveMQConnectionFactory("tcp://localhost:61616"));
    jmsActiveMQFactory.afterPropertiesSet();
    jmsActiveMQFactory.resetConnection();

    ActiveMQTopic topic = new ActiveMQTopic(">");

    DefaultMessageListenerContainer messageListenerContainer = new DefaultMessageListenerContainer();
    messageListenerContainer.setDestination(topic);
    messageListenerContainer.setConnectionFactory(jmsActiveMQFactory);
    messageListenerContainer.setSubscriptionShared(false);
    messageListenerContainer.setCacheLevel(DefaultMessageListenerContainer.CACHE_CONSUMER);

    messageListenerContainer.setMessageListener((MessageListener) message -> {
        try {/*from  ww w .java2  s  .  c  om*/
            Destination destination = message.getJMSDestination();
            if (message instanceof TextMessage) {
                TextMessage textMessage = (TextMessage) message;
                System.out.println(destination + " -> " + textMessage.getText());
            } else if (message instanceof BytesMessage) {
                BytesMessage bytesMessage = (BytesMessage) message;
                byte[] bytes = new byte[(int) bytesMessage.getBodyLength()];
                bytesMessage.readBytes(bytes);
                System.out.println(destination + " -> " + new String(bytes, Charsets.UTF_8));
            }
        } catch (JMSException ex) {
            throw new UnrecoverableCoreException(ex);
        }
    });

    messageListenerContainer.initialize();
    messageListenerContainer.start();

    System.out.println("Dumping messages from all topics");

    Runtime.getRuntime().addShutdownHook(new Thread(messageListenerContainer::stop));

    Thread.sleep(Long.MAX_VALUE);
}

From source file:org.apache.camel.component.jms.JmsConfiguration.java

/**
 * Defaults the JMS cache level if none is explicitly specified. Note that
 * due to this <a/* ww  w  .j a va  2 s.c om*/
 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-3890">Spring
 * Bug</a> we cannot use CACHE_CONSUMER by default (which we should do as
 * its most efficient) unless the spring version is 2.5.1 or later. Instead
 * we use CACHE_CONNECTION - part from for non-durable topics which must use
 * CACHE_CONSUMER to avoid missing messages (due to the consumer being
 * created and destroyed per message).
 *
 * @param endpoint the endpoint
 * @return the cache level
 */
protected int defaultCacheLevel(JmsEndpoint endpoint) {
    // if we are on a new enough spring version we can assume CACHE_CONSUMER
    if (PackageHelper.isValidVersion("org.springframework.jms", 2.51D)) {
        return DefaultMessageListenerContainer.CACHE_CONSUMER;
    } else {
        if (endpoint.isPubSubDomain() && !isSubscriptionDurable()) {
            // we must cache the consumer or we will miss messages
            // see https://issues.apache.org/activemq/browse/CAMEL-253
            return DefaultMessageListenerContainer.CACHE_CONSUMER;
        } else {
            // to enable consuming and sending with a single JMS session (to
            // avoid XA) we can only use CACHE_CONNECTION
            // due to this bug :
            // http://opensource.atlassian.com/projects/spring/browse/SPR-3890
            return DefaultMessageListenerContainer.CACHE_CONNECTION;
        }
    }
}

From source file:org.springframework.integration.jms.SubscribableJmsChannelTests.java

/**
 * Blocks until the listener container has subscribed; if the container does not support
 * this test, or the caching mode is incompatible, true is returned. Otherwise blocks
 * until timeout milliseconds have passed, or the consumer has registered.
 * @see DefaultMessageListenerContainer#isRegisteredWithDestination()
 * @param timeout Timeout in milliseconds.
 * @return True if a subscriber has connected or the container/attributes does not support
 * the test. False if a valid container does not have a registered consumer within
 * timeout milliseconds./*from  w  ww .j a va2  s.  c o m*/
 */
private static boolean waitUntilRegisteredWithDestination(SubscribableJmsChannel channel, long timeout) {
    AbstractMessageListenerContainer container = (AbstractMessageListenerContainer) new DirectFieldAccessor(
            channel).getPropertyValue("container");
    if (container instanceof DefaultMessageListenerContainer) {
        DefaultMessageListenerContainer listenerContainer = (DefaultMessageListenerContainer) container;
        if (listenerContainer.getCacheLevel() != DefaultMessageListenerContainer.CACHE_CONSUMER) {
            return true;
        }
        while (timeout > 0) {
            if (listenerContainer.isRegisteredWithDestination()) {
                return true;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
            }
            timeout -= 100;
        }
        return false;
    }
    return true;
}