List of usage examples for org.springframework.amqp.rabbit.connection CachingConnectionFactory CachingConnectionFactory
public CachingConnectionFactory()
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testAtomicSendAndReceiveWithConversion() throws Exception { final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory()); template.setRoutingKey(ROUTE);/*from ww w . j av a 2s . c o m*/ template.setQueue(ROUTE); ExecutorService executor = Executors.newFixedThreadPool(1); // Set up a consumer to respond to our producer Future<String> received = executor.submit(new Callable<String>() { public String call() throws Exception { Message message = null; for (int i = 0; i < 10; i++) { message = template.receive(); if (message != null) { break; } Thread.sleep(100L); } assertNotNull("No message received", message); template.send(message.getMessageProperties().getReplyTo(), message); return (String) template.getMessageConverter().fromMessage(message); } }); String result = (String) template.convertSendAndReceive("message"); assertEquals("message", received.get(1000, TimeUnit.MILLISECONDS)); assertEquals("message", result); // Message was consumed so nothing left on queue result = (String) template.receiveAndConvert(); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplateIntegrationTests.java
@Test public void testAtomicSendAndReceiveWithConversionAndMessagePostProcessor() throws Exception { final RabbitTemplate template = new RabbitTemplate(new CachingConnectionFactory()); template.setRoutingKey(ROUTE);/*w w w . j av a 2s . c o m*/ template.setQueue(ROUTE); ExecutorService executor = Executors.newFixedThreadPool(1); // Set up a consumer to respond to our producer Future<String> received = executor.submit(new Callable<String>() { public String call() throws Exception { Message message = null; for (int i = 0; i < 10; i++) { message = template.receive(); if (message != null) { break; } Thread.sleep(100L); } assertNotNull("No message received", message); template.send(message.getMessageProperties().getReplyTo(), message); return (String) template.getMessageConverter().fromMessage(message); } }); String result = (String) template.convertSendAndReceive((Object) "message", new MessagePostProcessor() { public Message postProcessMessage(Message message) throws AmqpException { try { byte[] newBody = new String(message.getBody(), "UTF-8").toUpperCase().getBytes("UTF-8"); return new Message(newBody, message.getMessageProperties()); } catch (Exception e) { throw new AmqpException("unexpected failure in test", e); } } }); assertEquals("MESSAGE", received.get(1000, TimeUnit.MILLISECONDS)); assertEquals("MESSAGE", result); // Message was consumed so nothing left on queue result = (String) template.receiveAndConvert(); assertEquals(null, result); }
From source file:org.springframework.amqp.rabbit.core.RabbitTemplatePublisherCallbacksIntegrationTests.java
@Before public void create() { connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setChannelCacheSize(10); connectionFactory.setPort(BrokerTestUtils.getPort()); connectionFactoryWithConfirmsEnabled = new CachingConnectionFactory(); connectionFactoryWithConfirmsEnabled.setHost("localhost"); // When using publisher confirms, the cache size needs to be large enough // otherwise channels can be closed before confirms are received. connectionFactoryWithConfirmsEnabled.setChannelCacheSize(100); connectionFactoryWithConfirmsEnabled.setPort(BrokerTestUtils.getPort()); connectionFactoryWithConfirmsEnabled.setPublisherConfirms(true); templateWithConfirmsEnabled = new RabbitTemplate(connectionFactoryWithConfirmsEnabled); connectionFactoryWithReturnsEnabled = new CachingConnectionFactory(); connectionFactoryWithReturnsEnabled.setHost("localhost"); connectionFactoryWithReturnsEnabled.setChannelCacheSize(1); connectionFactoryWithReturnsEnabled.setPort(BrokerTestUtils.getPort()); connectionFactoryWithReturnsEnabled.setPublisherReturns(true); templateWithReturnsEnabled = new RabbitTemplate(connectionFactoryWithReturnsEnabled); }
From source file:org.springframework.amqp.rabbit.listener.MessageListenerContainerMultipleQueueIntegrationTests.java
private void doTest(int concurrentConsumers, ContainerConfigurer configurer) { int messageCount = 10; RabbitTemplate template = new RabbitTemplate(); CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setChannelCacheSize(concurrentConsumers); connectionFactory.setPort(BrokerTestUtils.getPort()); template.setConnectionFactory(connectionFactory); SimpleMessageConverter messageConverter = new SimpleMessageConverter(); messageConverter.setCreateMessageIds(true); template.setMessageConverter(messageConverter); for (int i = 0; i < messageCount; i++) { template.convertAndSend(queue1.getName(), new Integer(i)); template.convertAndSend(queue2.getName(), new Integer(i)); }//from w w w . j av a 2s . co m final SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(connectionFactory); final CountDownLatch latch = new CountDownLatch(messageCount * 2); PojoListener listener = new PojoListener(latch); container.setMessageListener(new MessageListenerAdapter(listener)); container.setAcknowledgeMode(AcknowledgeMode.AUTO); container.setChannelTransacted(true); container.setConcurrentConsumers(concurrentConsumers); configurer.configure(container); container.afterPropertiesSet(); container.start(); try { int timeout = Math.min(1 + messageCount / concurrentConsumers, 30); boolean waited = latch.await(timeout, TimeUnit.SECONDS); logger.info("All messages recovered: " + waited); assertEquals(concurrentConsumers, container.getActiveConsumerCount()); assertTrue("Timed out waiting for messages", waited); } catch (InterruptedException e) { Thread.currentThread().interrupt(); throw new IllegalStateException("unexpected interruption"); } finally { container.shutdown(); assertEquals(0, container.getActiveConsumerCount()); } assertNull(template.receiveAndConvert(queue1.getName())); assertNull(template.receiveAndConvert(queue2.getName())); }
From source file:org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainerIntegration2Tests.java
@Before public void declareQueues() { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); connectionFactory.setPort(BrokerTestUtils.getPort()); template.setConnectionFactory(connectionFactory); admin = new RabbitAdmin(connectionFactory); admin.deleteQueue(queue.getName());//from www.j a va2s. co m admin.declareQueue(queue); admin.deleteQueue(queue1.getName()); admin.declareQueue(queue1); }
From source file:org.springframework.amqp.rabbit.test.BrokerFederated.java
@SuppressWarnings("deprecation") @Override/*from w w w . j ava 2 s.c o m*/ public Statement apply(Statement base, FrameworkMethod method, Object target) { // Check at the beginning, so this can be used as a static field if (assumeOnline) { Assume.assumeTrue(brokerOnline.get(port)); } else { Assume.assumeTrue(brokerOffline.get(port)); } CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); try { connectionFactory.setPort(port); if (StringUtils.hasText(hostName)) { connectionFactory.setHost(hostName); } RabbitAdmin admin = new RabbitAdmin(connectionFactory); org.springframework.amqp.core.FederatedExchange exchange = new org.springframework.amqp.core.FederatedExchange( "fedDirectRuleTest"); exchange.setBackingType("direct"); exchange.setUpstreamSet("upstream-set"); admin.declareExchange(exchange); admin.deleteExchange("fedDirectRuleTest"); brokerOffline.put(port, false); if (!assumeOnline) { Assume.assumeTrue(brokerOffline.get(port)); } } catch (Exception e) { logger.warn("Not executing tests because federated connectivity test failed", e); brokerOnline.put(port, false); if (assumeOnline) { Assume.assumeNoException(e); } } finally { connectionFactory.destroy(); } return super.apply(base, method, target); }
From source file:org.springframework.integration.amqp.rule.BrokerRunning.java
@Override public Statement apply(Statement base, Description description) { CachingConnectionFactory connectionFactory = new CachingConnectionFactory(); connectionFactory.setHost("localhost"); try {/*from w ww.j a v a 2 s . com*/ connectionFactory.setPort(PORT); RabbitAdmin admin = new RabbitAdmin(connectionFactory); for (Queue queue : queues) { String queueName = queue.getName(); logger.info("Deleting queue: " + queueName); // Delete completely - gets rid of consumers and bindings as well admin.deleteQueue(queueName); if (!DEFAULT_QUEUE_NAME.getName().equals(queueName)) { admin.declareQueue(queue); } } } catch (final Exception e) { logger.warn("Not executing tests because basic connectivity test failed", e); Assume.assumeNoException(e); } finally { connectionFactory.destroy(); } return super.apply(base, description); }