Search in sources :

Example 16 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project opennms by OpenNMS.

the class AppConfig method connectionFactory.

@Bean(name = "connectionFactory")
public CachingConnectionFactory connectionFactory() {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setTargetConnectionFactory(amqConnectionFactory());
    cachingConnectionFactory.setSessionCacheSize(16);
    cachingConnectionFactory.setExceptionListener(jmsExceptionListener());
    return cachingConnectionFactory;
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Bean(org.springframework.context.annotation.Bean)

Example 17 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project opennms by OpenNMS.

the class AppConfig method connectionFactory.

@Bean(name = "connectionFactory")
public CachingConnectionFactory connectionFactory() {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setTargetConnectionFactory(amqConnectionFactory());
    cachingConnectionFactory.setSessionCacheSize(16);
    cachingConnectionFactory.setExceptionListener(jmsExceptionListener());
    return cachingConnectionFactory;
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Bean(org.springframework.context.annotation.Bean)

Example 18 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class CommonTest method buildJmsTemplateForDestination.

static JmsTemplate buildJmsTemplateForDestination(boolean pubSub) {
    ConnectionFactory activeMqConnectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    final ConnectionFactory connectionFactory = new CachingConnectionFactory(activeMqConnectionFactory);
    JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    jmsTemplate.setPubSubDomain(pubSub);
    jmsTemplate.setSessionAcknowledgeMode(Session.CLIENT_ACKNOWLEDGE);
    jmsTemplate.setReceiveTimeout(10L);
    return jmsTemplate;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) ConnectionFactory(javax.jms.ConnectionFactory) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate)

Example 19 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class JMSPublisherConsumerIT method validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes.

@Test
public void validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes() throws Exception {
    final String destinationName = "validateJmsHeadersAndPropertiesAreTransferredFromFFAttributes";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        Map<String, String> flowFileAttributes = new HashMap<>();
        flowFileAttributes.put("foo", "foo");
        flowFileAttributes.put("illegal-property", "value");
        flowFileAttributes.put("another.illegal", "value");
        flowFileAttributes.put(JmsHeaders.REPLY_TO, "myTopic");
        // value expected to be integer, make sure non-integer doesn't cause problems
        flowFileAttributes.put(JmsHeaders.EXPIRATION, "never");
        publisher.publish(destinationName, "hellomq".getBytes(), flowFileAttributes);
        Message receivedMessage = jmsTemplate.receive(destinationName);
        assertTrue(receivedMessage instanceof BytesMessage);
        assertEquals("foo", receivedMessage.getStringProperty("foo"));
        assertFalse(receivedMessage.propertyExists("illegal-property"));
        assertFalse(receivedMessage.propertyExists("another.illegal"));
        assertTrue(receivedMessage.getJMSReplyTo() instanceof Topic);
        assertEquals("myTopic", ((Topic) receivedMessage.getJMSReplyTo()).getTopicName());
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) HashMap(java.util.HashMap) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) BytesMessage(javax.jms.BytesMessage) Topic(javax.jms.Topic) ComponentLog(org.apache.nifi.logging.ComponentLog) Test(org.junit.Test)

Example 20 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project nifi by apache.

the class JMSPublisherConsumerIT method testMultipleThreads.

@Test(timeout = 20000)
public void testMultipleThreads() throws Exception {
    String destinationName = "testMultipleThreads";
    JmsTemplate publishTemplate = CommonTest.buildJmsTemplateForDestination(false);
    final CountDownLatch consumerTemplateCloseCount = new CountDownLatch(4);
    try {
        JMSPublisher publisher = new JMSPublisher((CachingConnectionFactory) publishTemplate.getConnectionFactory(), publishTemplate, mock(ComponentLog.class));
        for (int i = 0; i < 4000; i++) {
            publisher.publish(destinationName, String.valueOf(i).getBytes(StandardCharsets.UTF_8));
        }
        final AtomicInteger msgCount = new AtomicInteger(0);
        final ConsumerCallback callback = new ConsumerCallback() {

            @Override
            public void accept(JMSResponse response) {
                msgCount.incrementAndGet();
            }
        };
        final Thread[] threads = new Thread[4];
        for (int i = 0; i < 4; i++) {
            final Thread t = new Thread(() -> {
                JmsTemplate consumeTemplate = CommonTest.buildJmsTemplateForDestination(false);
                try {
                    JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) consumeTemplate.getConnectionFactory(), consumeTemplate, mock(ComponentLog.class));
                    for (int j = 0; j < 1000 && msgCount.get() < 4000; j++) {
                        consumer.consume(destinationName, false, false, null, "UTF-8", callback);
                    }
                } finally {
                    ((CachingConnectionFactory) consumeTemplate.getConnectionFactory()).destroy();
                    consumerTemplateCloseCount.countDown();
                }
            });
            threads[i] = t;
            t.start();
        }
        int iterations = 0;
        while (msgCount.get() < 4000) {
            Thread.sleep(10L);
            if (++iterations % 100 == 0) {
                System.out.println(msgCount.get() + " messages received so far");
            }
        }
    } finally {
        ((CachingConnectionFactory) publishTemplate.getConnectionFactory()).destroy();
        consumerTemplateCloseCount.await();
    }
}
Also used : JMSResponse(org.apache.nifi.jms.processors.JMSConsumer.JMSResponse) ConsumerCallback(org.apache.nifi.jms.processors.JMSConsumer.ConsumerCallback) CountDownLatch(java.util.concurrent.CountDownLatch) ComponentLog(org.apache.nifi.logging.ComponentLog) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) Test(org.junit.Test)

Aggregations

CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)32 Test (org.junit.Test)21 JmsTemplate (org.springframework.jms.core.JmsTemplate)20 TextMessage (javax.jms.TextMessage)15 Message (javax.jms.Message)11 GenericMessage (org.springframework.messaging.support.GenericMessage)11 MessageCreator (org.springframework.jms.core.MessageCreator)10 BeanFactory (org.springframework.beans.factory.BeanFactory)9 Destination (javax.jms.Destination)8 ComponentLog (org.apache.nifi.logging.ComponentLog)7 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)6 JmsChannelFactoryBean (org.springframework.integration.jms.config.JmsChannelFactoryBean)6 ConnectionFactory (javax.jms.ConnectionFactory)5 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)5 ArrayList (java.util.ArrayList)4 ExecutorService (java.util.concurrent.ExecutorService)4 Session (javax.jms.Session)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 Executors (java.util.concurrent.Executors)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3