List of usage examples for org.springframework.amqp.rabbit.core RabbitTemplate setConnectionFactory
public final void setConnectionFactory(ConnectionFactory connectionFactory)
From source file:com.apress.prospringintegration.messaging.rabbitmq.jms.adapter.RabbitmqConfiguration.java
@Bean public RabbitTemplate amqpTemplate() { RabbitTemplate rabbitTemplate = new RabbitTemplate(); rabbitTemplate.setConnectionFactory(connectionFactory()); rabbitTemplate.setRoutingKey("ticket.queue"); rabbitTemplate.setQueue("ticket.queue"); return rabbitTemplate; }
From source file:io.manasobi.commons.config.AmqpConfig.java
@Bean public RabbitTemplate rabbitTemplate() { RabbitTemplate rabbitTemplate = new RabbitTemplate(); rabbitTemplate.setQueue("manasobi"); //rabbitTemplate.setRoutingKey("manasobi.key"); //rabbitTemplate.setExchange("manasobi.key"); rabbitTemplate.setRoutingKey("key.manasobi"); rabbitTemplate.setExchange("manasobi"); rabbitTemplate.setConnectionFactory(rabbitConnectionFactory()); return rabbitTemplate; }
From source file:acromusashi.stream.component.rabbitmq.AmqpTemplateFactory.java
/** * RabbitTemplate??//from w w w . j ava2 s .c o m * * @param queueName ?? * @param connectionFactory ConnectionFactory * @return RabbitTemplate * @throws RabbitmqCommunicateException amqpTemplate????????? */ private RabbitTemplate getRabbitTemplate(String queueName, ConnectionFactory connectionFactory) throws RabbitmqCommunicateException { // AmqpTemplate????????? RabbitTemplate template = null; try { template = (RabbitTemplate) BeanUtils.cloneBean(getContextBuilder().getAmqpTemplate(queueName)); } catch (Exception ex) { throw new RabbitmqCommunicateException(ex); } template.setConnectionFactory(connectionFactory); template.setExchange(queueName); template.setQueue(queueName); return template; }
From source file:org.springframework.amqp.rabbit.core.BatchingRabbitTemplateTests.java
@Test public void testDebatchByContainerBadMessageRejected() throws Exception { SimpleMessageListenerContainer container = new SimpleMessageListenerContainer(this.connectionFactory); container.setQueueNames(ROUTE);// w w w .j a v a2s. c o m container.setMessageListener((MessageListener) message -> { }); container.setReceiveTimeout(100); ConditionalRejectingErrorHandler errorHandler = new ConditionalRejectingErrorHandler(); container.setErrorHandler(errorHandler); container.afterPropertiesSet(); container.start(); Log logger = spy(TestUtils.getPropertyValue(errorHandler, "logger", Log.class)); doReturn(true).when(logger).isWarnEnabled(); doAnswer(new DoesNothing()).when(logger).warn(anyString(), any(Throwable.class)); new DirectFieldAccessor(errorHandler).setPropertyValue("logger", logger); try { RabbitTemplate template = new RabbitTemplate(); template.setConnectionFactory(this.connectionFactory); MessageProperties props = new MessageProperties(); props.getHeaders().put(MessageProperties.SPRING_BATCH_FORMAT, MessageProperties.BATCH_FORMAT_LENGTH_HEADER4); Message message = new Message("\u0000\u0000\u0000\u0004foo".getBytes(), props); template.send("", ROUTE, message); Thread.sleep(1000); ArgumentCaptor<Object> arg1 = ArgumentCaptor.forClass(Object.class); ArgumentCaptor<Throwable> arg2 = ArgumentCaptor.forClass(Throwable.class); verify(logger).warn(arg1.capture(), arg2.capture()); assertThat(arg2.getValue().getMessage(), containsString("Bad batched message received")); } finally { container.stop(); } }
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 a v a 2 s .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.xd.dirt.integration.rabbit.RabbitMessageBus.java
private RabbitTemplate determineRabbitTemplate(RabbitPropertiesAccessor properties) { RabbitTemplate rabbitTemplate = null; if (properties.isBatchingEnabled(this.defaultBatchingEnabled)) { BatchingStrategy batchingStrategy = new SimpleBatchingStrategy( properties.getBatchSize(this.defaultBatchSize), properties.geteBatchBufferLimit(this.defaultBatchBufferLimit), properties.getBatchTimeout(this.defaultBatchTimeout)); rabbitTemplate = new BatchingRabbitTemplate(batchingStrategy, getApplicationContext() .getBean(IntegrationContextUtils.TASK_SCHEDULER_BEAN_NAME, TaskScheduler.class)); rabbitTemplate.setConnectionFactory(this.connectionFactory); }//from w ww.j a va2s . c o m if (properties.isCompress(this.defaultCompress)) { if (rabbitTemplate == null) { rabbitTemplate = new RabbitTemplate(this.connectionFactory); } rabbitTemplate.setBeforePublishPostProcessors(this.compressingPostProcessor); rabbitTemplate.afterPropertiesSet(); } if (rabbitTemplate == null) { rabbitTemplate = this.rabbitTemplate; } return rabbitTemplate; }