use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.
the class JmsOutboundGatewayTests method testConnectionBreakOnReplyMessageIdCorrelation.
@Test
public void testConnectionBreakOnReplyMessageIdCorrelation() 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 = "requests1";
gateway.setRequestDestinationName(requestQ);
String replyQ = "replies1";
gateway.setReplyDestinationName(replyQ);
QueueChannel queueChannel = new QueueChannel();
gateway.setOutputChannel(queueChannel);
gateway.setBeanFactory(mock(BeanFactory.class));
gateway.setReceiveTimeout(60000);
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.getJMSMessageID());
return reply1;
};
template.send(replyQ, reply);
org.springframework.messaging.Message<?> received = queueChannel.receive(20000);
assertNotNull(received);
assertEquals("bar", received.getPayload());
gateway.stop();
connectionFactory1.destroy();
connectionFactory2.destroy();
exec.shutdownNow();
}
use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.
the class OutboundGatewayConnectionTests method testContainerWithDestBrokenConnection.
// need a more reliable stop/start for AMQ
@Test
// need a more reliable stop/start for AMQ
@Ignore
public void testContainerWithDestBrokenConnection() throws Exception {
BeanFactory beanFactory = mock(BeanFactory.class);
when(beanFactory.containsBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME)).thenReturn(true);
ThreadPoolTaskScheduler scheduler = new ThreadPoolTaskScheduler();
scheduler.initialize();
when(beanFactory.getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)).thenReturn(scheduler);
final JmsOutboundGateway gateway = new JmsOutboundGateway();
gateway.setBeanFactory(beanFactory);
BrokerService broker = new BrokerService();
broker.addConnector("tcp://localhost:61616?broker.persistent=false");
broker.start();
ActiveMQConnectionFactory amqConnectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61616");
ConnectionFactory connectionFactory = new CachingConnectionFactory(amqConnectionFactory);
gateway.setConnectionFactory(connectionFactory);
gateway.setRequestDestination(requestQueue1);
gateway.setReplyDestination(replyQueue1);
gateway.setCorrelationKey("JMSCorrelationID");
gateway.setUseReplyContainer(true);
gateway.afterPropertiesSet();
gateway.start();
final AtomicReference<Object> reply = new AtomicReference<Object>();
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
ExecutorService exec = Executors.newSingleThreadExecutor();
exec.execute(() -> {
latch1.countDown();
try {
reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
} finally {
latch2.countDown();
}
});
assertTrue(latch1.await(10, TimeUnit.SECONDS));
JmsTemplate template = new JmsTemplate();
template.setConnectionFactory(amqConnectionFactory);
template.setReceiveTimeout(5000);
javax.jms.Message request = template.receive(requestQueue1);
assertNotNull(request);
final javax.jms.Message jmsReply = request;
template.send(request.getJMSReplyTo(), (MessageCreator) session -> jmsReply);
assertTrue(latch2.await(10, TimeUnit.SECONDS));
assertNotNull(reply.get());
broker.stop();
broker = new BrokerService();
broker.addConnector("tcp://localhost:61616?broker.persistent=false");
broker.start();
final CountDownLatch latch3 = new CountDownLatch(1);
final CountDownLatch latch4 = new CountDownLatch(1);
exec.execute(() -> {
latch3.countDown();
try {
reply.set(gateway.handleRequestMessage(new GenericMessage<String>("foo")));
} finally {
latch4.countDown();
}
});
assertTrue(latch3.await(10, TimeUnit.SECONDS));
template = new JmsTemplate();
template.setConnectionFactory(amqConnectionFactory);
template.setReceiveTimeout(5000);
request = template.receive(requestQueue1);
assertNotNull(request);
final javax.jms.Message jmsReply2 = request;
template.send(request.getJMSReplyTo(), (MessageCreator) session -> jmsReply2);
assertTrue(latch4.await(10, TimeUnit.SECONDS));
assertNotNull(reply.get());
gateway.stop();
broker.stop();
scheduler.destroy();
exec.shutdownNow();
}
use of org.springframework.jms.connection.CachingConnectionFactory in project spring-integration by spring-projects.
the class PollableJmsChannelTests method queueName.
@Test
public void queueName() 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);
factoryBean.setBeanFactory(mock(BeanFactory.class));
factoryBean.afterPropertiesSet();
PollableJmsChannel channel = (PollableJmsChannel) factoryBean.getObject();
boolean sent1 = channel.send(new GenericMessage<String>("foo"));
assertTrue(sent1);
boolean sent2 = channel.send(new GenericMessage<String>("bar"));
assertTrue(sent2);
Message<?> result1 = channel.receive(10000);
assertNotNull(result1);
assertEquals("foo", result1.getPayload());
Message<?> result2 = channel.receive(10000);
assertNotNull(result2);
assertEquals("bar", result2.getPayload());
}
use of org.springframework.jms.connection.CachingConnectionFactory 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();
}
use of org.springframework.jms.connection.CachingConnectionFactory 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));
}
Aggregations