Search in sources :

Example 6 with CachingConnectionFactory

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

the class JMSPublisherConsumerIT method validateFailOnUnsupportedMessageType.

/**
 * At the moment the only two supported message types are TextMessage and
 * BytesMessage which is sufficient for the type if JMS use cases NiFi is
 * used. The may change to the point where all message types are supported
 * at which point this test will no be longer required.
 */
@Test
public void validateFailOnUnsupportedMessageType() throws Exception {
    final String destinationName = "validateFailOnUnsupportedMessageType";
    JmsTemplate jmsTemplate = CommonTest.buildJmsTemplateForDestination(false);
    try {
        jmsTemplate.send(destinationName, new MessageCreator() {

            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createObjectMessage();
            }
        });
        JMSConsumer consumer = new JMSConsumer((CachingConnectionFactory) jmsTemplate.getConnectionFactory(), jmsTemplate, mock(ComponentLog.class));
        consumer.consume(destinationName, false, false, null, "UTF-8", new ConsumerCallback() {

            @Override
            public void accept(JMSResponse response) {
            // noop
            }
        });
    } finally {
        ((CachingConnectionFactory) jmsTemplate.getConnectionFactory()).destroy();
    }
}
Also used : TextMessage(javax.jms.TextMessage) BytesMessage(javax.jms.BytesMessage) Message(javax.jms.Message) JMSResponse(org.apache.nifi.jms.processors.JMSConsumer.JMSResponse) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) ConsumerCallback(org.apache.nifi.jms.processors.JMSConsumer.ConsumerCallback) JMSException(javax.jms.JMSException) ComponentLog(org.apache.nifi.logging.ComponentLog) MessageCreator(org.springframework.jms.core.MessageCreator) Session(javax.jms.Session) Test(org.junit.Test)

Example 7 with CachingConnectionFactory

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

the class AppConfig method connectionFactory.

// private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
@Bean(name = "connectionFactory")
public CachingConnectionFactory connectionFactory() {
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory();
    cachingConnectionFactory.setTargetConnectionFactory(amqConnectionFactory());
    cachingConnectionFactory.setSessionCacheSize(8);
    cachingConnectionFactory.setExceptionListener(jmsExceptionListener());
    return cachingConnectionFactory;
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Bean(org.springframework.context.annotation.Bean)

Example 8 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestMessageIdNonOptimized.

@Test
public void messageCorrelationBasedOnRequestMessageIdNonOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("standardMessageIdCopyingConsumerWithoutOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueNonOptimizedB", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueNonOptimizedB", Destination.class);
        new Thread(() -> {
            final Message requestMessage = jmsTemplate.receive(requestDestination);
            jmsTemplate.send(replyDestination, (MessageCreator) session -> {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            });
        }).start();
        org.springframework.messaging.Message<?> siReplyMessage = gateway.exchange(new GenericMessage<String>("foo"));
        assertEquals("bar", siReplyMessage.getPayload());
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageCreator(org.springframework.jms.core.MessageCreator) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 9 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestMessageIdOptimized.

@Test(expected = MessageTimeoutException.class)
public void messageCorrelationBasedOnRequestMessageIdOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("standardMessageIdCopyingConsumerWithOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueOptimizedA", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueOptimizedA", Destination.class);
        new Thread(() -> {
            final Message requestMessage = jmsTemplate.receive(requestDestination);
            jmsTemplate.send(replyDestination, (MessageCreator) session -> {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            });
        }).start();
        gateway.exchange(new GenericMessage<String>("foo"));
    } finally {
        context.close();
    }
}
Also used : Destination(javax.jms.Destination) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) MessageCreator(org.springframework.jms.core.MessageCreator) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Example 10 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.

the class RequestReplyScenariosWithTempReplyQueuesTests method messageCorrelationBasedOnRequestMessageId.

@SuppressWarnings("resource")
@Test
public void messageCorrelationBasedOnRequestMessageId() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-temp-reply-consumers.xml", this.getClass());
    RequestReplyExchanger gateway = context.getBean(RequestReplyExchanger.class);
    CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
    final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
    final Destination requestDestination = context.getBean("siOutQueue", Destination.class);
    new Thread(() -> {
        final Message requestMessage = jmsTemplate.receive(requestDestination);
        Destination replyTo = null;
        try {
            replyTo = requestMessage.getJMSReplyTo();
        } catch (Exception e) {
            fail();
        }
        jmsTemplate.send(replyTo, (MessageCreator) session -> {
            try {
                TextMessage message = session.createTextMessage();
                message.setText("bar");
                message.setJMSCorrelationID(requestMessage.getJMSMessageID());
                return message;
            } catch (Exception e) {
            }
            return null;
        });
    }).start();
    gateway.exchange(new GenericMessage<String>("foo"));
    context.close();
}
Also used : ActiveMQDestination(org.apache.activemq.command.ActiveMQDestination) Destination(javax.jms.Destination) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) RequestReplyExchanger(org.springframework.integration.gateway.RequestReplyExchanger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) JmsTemplate(org.springframework.jms.core.JmsTemplate) MessageDeliveryException(org.springframework.messaging.MessageDeliveryException) MessageCreator(org.springframework.jms.core.MessageCreator) TextMessage(javax.jms.TextMessage) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) 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