Search in sources :

Example 11 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class PollableJmsChannelTests method qos.

@Test
public void qos() throws Exception {
    this.queue = new ActiveMQQueue("pollableJmsChannelTestQueue");
    CachingConnectionFactory ccf = new CachingConnectionFactory(connectionFactory);
    ccf.setCacheConsumers(false);
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestination(this.queue);
    factoryBean.setExplicitQosEnabled(true);
    factoryBean.setPriority(5);
    int ttl = 10000;
    factoryBean.setTimeToLive(ttl);
    factoryBean.setDeliveryPersistent(false);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    final JmsTemplate receiver = new JmsTemplate(this.connectionFactory);
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    final AtomicReference<javax.jms.Message> message = new AtomicReference<javax.jms.Message>();
    final CountDownLatch latch1 = new CountDownLatch(1);
    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(() -> {
        message.set(receiver.receive(queue));
        latch1.countDown();
    });
    assertTrue(latch1.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
    assertEquals(5, message.get().getJMSPriority());
    assertTrue(message.get().getJMSExpiration() <= System.currentTimeMillis() + ttl);
    assertTrue(message.get().toString().contains("persistent = false"));
    message.set(null);
    final CountDownLatch latch2 = new CountDownLatch(1);
    boolean sent2 = channel.send(MessageBuilder.withPayload("foo").setPriority(6).build());
    assertTrue(sent2);
    exec.execute(() -> {
        message.set(receiver.receive(queue));
        latch2.countDown();
    });
    assertTrue(latch2.await(10, TimeUnit.SECONDS));
    assertNotNull(message.get());
    assertEquals(6, message.get().getJMSPriority());
    assertTrue(message.get().getJMSExpiration() <= System.currentTimeMillis() + ttl);
    assertTrue(message.get().toString().contains("persistent = false"));
    exec.shutdownNow();
}
Also used : Message(org.springframework.messaging.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) ExecutorService(java.util.concurrent.ExecutorService) ActiveMQQueue(org.apache.activemq.command.ActiveMQQueue) JmsTemplate(org.springframework.jms.core.JmsTemplate) Test(org.junit.Test)

Example 12 with JmsChannelFactoryBean

use of org.springframework.integration.jms.config.JmsChannelFactoryBean in project spring-integration by spring-projects.

the class PollableJmsChannelTests method queueNameWithTruePreReceiveInterceptors.

@Test
public void queueNameWithTruePreReceiveInterceptors() throws Exception {
    JmsChannelFactoryBean factoryBean = new JmsChannelFactoryBean(false);
    CachingConnectionFactory ccf = new CachingConnectionFactory(this.connectionFactory);
    ccf.setCacheConsumers(false);
    factoryBean.setConnectionFactory(ccf);
    factoryBean.setDestinationName("someDynamicQueue");
    factoryBean.setPubSubDomain(false);
    List<ChannelInterceptor> interceptorList = new ArrayList<ChannelInterceptor>();
    ChannelInterceptor interceptor = spy(new SampleInterceptor(true));
    interceptorList.add(interceptor);
    factoryBean.setInterceptors(interceptorList);
    factoryBean.setBeanFactory(mock(BeanFactory.class));
    factoryBean.afterPropertiesSet();
    PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
    boolean sent1 = channel.send(new GenericMessage<String>("foo"));
    assertTrue(sent1);
    Message<?> result1 = channel.receive(10000);
    assertNotNull(result1);
    verify(interceptor, times(1)).preReceive(Mockito.any(MessageChannel.class));
    verify(interceptor, times(1)).postReceive(Mockito.any(Message.class), Mockito.any(MessageChannel.class));
}
Also used : Message(org.springframework.messaging.Message) TextMessage(javax.jms.TextMessage) GenericMessage(org.springframework.messaging.support.GenericMessage) ChannelInterceptor(org.springframework.messaging.support.ChannelInterceptor) ArrayList(java.util.ArrayList) JmsChannelFactoryBean(org.springframework.integration.jms.config.JmsChannelFactoryBean) MessageChannel(org.springframework.messaging.MessageChannel) CachingConnectionFactory(org.springframework.jms.connection.CachingConnectionFactory) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)12 BeanFactory (org.springframework.beans.factory.BeanFactory)12 JmsChannelFactoryBean (org.springframework.integration.jms.config.JmsChannelFactoryBean)12 CachingConnectionFactory (org.springframework.jms.connection.CachingConnectionFactory)10 ActiveMQQueue (org.apache.activemq.command.ActiveMQQueue)7 Message (org.springframework.messaging.Message)7 GenericMessage (org.springframework.messaging.support.GenericMessage)7 ArrayList (java.util.ArrayList)6 MessageListener (javax.jms.MessageListener)6 AbstractMessageListenerContainer (org.springframework.jms.listener.AbstractMessageListenerContainer)6 CountDownLatch (java.util.concurrent.CountDownLatch)5 Matchers.containsString (org.hamcrest.Matchers.containsString)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5 MessageDeliveryException (org.springframework.messaging.MessageDeliveryException)5 Collections (java.util.Collections)4 List (java.util.List)4 TimeUnit (java.util.concurrent.TimeUnit)4 Destination (javax.jms.Destination)4 TextMessage (javax.jms.TextMessage)4 ActiveMQConnectionFactory (org.apache.activemq.ActiveMQConnectionFactory)4