Search in sources :

Example 21 with CachingConnectionFactory

use of org.springframework.jms.connection.CachingConnectionFactory in project webofneeds by researchstudio-sat.

the class BrokerComponentFactory method configureCachingConnectionFactory.

public synchronized ConnectionFactory configureCachingConnectionFactory(ActiveMQConnectionFactory connectionFactory) {
    // for non-persistent messages setting "AlwaysSyncSend" to true makes it slow, but ensures that a producer is immediately informed
    // about the memory issues on broker (is blocked or gets exception depending on <systemUsage> config)
    // see more info http://activemq.apache.org/producer-flow-control.html
    connectionFactory.setAlwaysSyncSend(false);
    // disable timestamps by default so that ttl of messages is not checked
    connectionFactory.setDisableTimeStampsByDefault(true);
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(connectionFactory);
    cachingConnectionFactory.setCacheConsumers(true);
    cachingConnectionFactory.setCacheProducers(true);
    return cachingConnectionFactory;
}
Also used : CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory)

Example 22 with CachingConnectionFactory

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

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdOptimized.

@Test
public void messageCorrelationBasedOnRequestCorrelationIdOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("correlationPropagatingConsumerWithOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueOptimizedC", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueOptimizedC", 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.getJMSCorrelationID());
                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 23 with CachingConnectionFactory

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

the class RequestReplyScenariosWithCachedConsumersTests method messageCorrelationBasedOnRequestCorrelationIdNonOptimized.

@Test(expected = MessageTimeoutException.class)
public void messageCorrelationBasedOnRequestCorrelationIdNonOptimized() throws Exception {
    ActiveMqTestUtils.prepare();
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("producer-cached-consumers.xml", this.getClass());
    try {
        RequestReplyExchanger gateway = context.getBean("correlationPropagatingConsumerWithoutOptimization", RequestReplyExchanger.class);
        CachingConnectionFactory connectionFactory = context.getBean(CachingConnectionFactory.class);
        final JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
        final Destination requestDestination = context.getBean("siOutQueueNonOptimizedD", Destination.class);
        final Destination replyDestination = context.getBean("siInQueueNonOptimizedD", 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.getJMSCorrelationID());
                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 24 with CachingConnectionFactory

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

the class OutboundGatewayFunctionTests method getConnectionFactory.

private ConnectionFactory getConnectionFactory() {
    ActiveMQConnectionFactory activeMQConnectionFactory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
    CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(activeMQConnectionFactory);
    cachingConnectionFactory.setCacheConsumers(false);
    return cachingConnectionFactory;
}
Also used : ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory)

Example 25 with CachingConnectionFactory

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

the class JmsOutboundGatewayTests method testConnectionBreakOnReplyCustomCorrelation.

@Test
public void testConnectionBreakOnReplyCustomCorrelation() throws Exception {
    CachingConnectionFactory connectionFactory1 = new CachingConnectionFactory(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
    connectionFactory1.setCacheConsumers(false);
    final JmsOutboundGateway gateway = new JmsOutboundGateway();
    gateway.setConnectionFactory(connectionFactory1);
    String requestQ = "requests2";
    gateway.setRequestDestinationName(requestQ);
    String replyQ = "replies2";
    gateway.setReplyDestinationName(replyQ);
    QueueChannel queueChannel = new QueueChannel();
    gateway.setOutputChannel(queueChannel);
    gateway.setBeanFactory(mock(BeanFactory.class));
    gateway.setReceiveTimeout(60000);
    gateway.setCorrelationKey("JMSCorrelationID");
    gateway.afterPropertiesSet();
    gateway.start();
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> gateway.handleMessage(new GenericMessage<String>("foo")));
    CachingConnectionFactory connectionFactory2 = new CachingConnectionFactory(new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"));
    connectionFactory2.setCacheConsumers(false);
    JmsTemplate template = new JmsTemplate(connectionFactory2);
    template.setReceiveTimeout(10000);
    template.afterPropertiesSet();
    final Message request = template.receive(requestQ);
    assertNotNull(request);
    connectionFactory1.resetConnection();
    MessageCreator reply = session -> {
        TextMessage reply1 = session.createTextMessage("bar");
        reply1.setJMSCorrelationID(request.getJMSCorrelationID());
        return reply1;
    };
    logger.debug("Sending reply to: " + replyQ);
    template.send(replyQ, reply);
    logger.debug("Sent reply to: " + replyQ);
    org.springframework.messaging.Message<?> received = queueChannel.receive(20000);
    assertNotNull(received);
    assertEquals("bar", received.getPayload());
    gateway.stop();
    connectionFactory1.destroy();
    connectionFactory2.destroy();
    exec.shutdownNow();
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) QueueChannel(org.springframework.integration.channel.QueueChannel) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) TestUtils(org.springframework.integration.test.util.TestUtils) ArrayList(java.util.ArrayList) Session(javax.jms.Session) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JmsTemplate(org.springframework.jms.core.JmsTemplate) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Message(javax.jms.Message) ExecutorService(java.util.concurrent.ExecutorService) ArgumentMatchers.isNull(org.mockito.ArgumentMatchers.isNull) ErrorHandlingTaskExecutor(org.springframework.integration.util.ErrorHandlingTaskExecutor) Connection(javax.jms.Connection) JmsException(org.springframework.jms.JmsException) Assert.assertNotNull(org.junit.Assert.assertNotNull) TextMessage(javax.jms.TextMessage) ObjectUtils(org.springframework.util.ObjectUtils) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Executors(java.util.concurrent.Executors) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) List(java.util.List) MessageCreator(org.springframework.jms.core.MessageCreator) MessageConsumer(javax.jms.MessageConsumer) Rule(org.junit.Rule) Log4j2LevelAdjuster(org.springframework.integration.test.rule.Log4j2LevelAdjuster) Destination(javax.jms.Destination) BeanFactory(org.springframework.beans.factory.BeanFactory) Log(org.apache.commons.logging.Log) LogFactory(org.apache.commons.logging.LogFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ReplyContainerProperties(org.springframework.integration.jms.JmsOutboundGateway.ReplyContainerProperties) GenericMessage(org.springframework.messaging.support.GenericMessage) TemporaryQueue(javax.jms.TemporaryQueue) Assert.assertEquals(org.junit.Assert.assertEquals) ConnectionFactory(javax.jms.ConnectionFactory) Mockito.mock(org.mockito.Mockito.mock) QueueChannel(org.springframework.integration.channel.QueueChannel) Message(javax.jms.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) MessageCreator(org.springframework.jms.core.MessageCreator) ActiveMQConnectionFactory(org.apache.activemq.ActiveMQConnectionFactory) GenericMessage(org.springframework.messaging.support.GenericMessage) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) ExecutorService(java.util.concurrent.ExecutorService) JmsTemplate(org.springframework.jms.core.JmsTemplate) TextMessage(javax.jms.TextMessage) 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