@Configuration public class IntegrationConfig { // ... as above @Bean public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setExchange("my-exchange"); return template; } }
private void configureExchangeName(RabbitTemplate rabbitTemplate) { ofNullable(getExchangeName()).ifPresent((exchange) -> { rabbitTemplate.setExchange(exchange); }); }
private RabbitTemplate createDmfClient() { final RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jackson2JsonMessageConverter()); template.setReceiveTimeout(TimeUnit.SECONDS.toMillis(3)); template.setReplyTimeout(TimeUnit.SECONDS.toMillis(3)); template.setExchange(getExchange()); return template; }
/** * Construct an instance using the provided arguments. "Direct replyTo" is used for * replies. * @param connectionFactory the connection factory. * @param exchange the default exchange to which requests will be sent. * @param routingKey the default routing key. * @since 2.0 */ public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey) { Assert.notNull(connectionFactory, "'connectionFactory' cannot be null"); Assert.notNull(routingKey, "'routingKey' cannot be null"); this.template = new RabbitTemplate(connectionFactory); this.template.setExchange(exchange == null ? "" : exchange); this.template.setRoutingKey(routingKey); this.container = null; this.replyAddress = null; this.directReplyToContainer = new DirectReplyToMessageListenerContainer(this.template.getConnectionFactory()); this.directReplyToContainer.setMessageListener(this); }
private RabbitTemplate createDmfClient() { final RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setMessageConverter(new Jackson2JsonMessageConverter()); template.setReceiveTimeout(TimeUnit.SECONDS.toMillis(3)); template.setReplyTimeout(TimeUnit.SECONDS.toMillis(3)); template.setExchange(getExchange()); return template; }
/** * Construct an instance using the provided arguments. "Direct replyTo" is used for * replies. * @param connectionFactory the connection factory. * @param exchange the default exchange to which requests will be sent. * @param routingKey the default routing key. * @since 2.0 */ public AsyncRabbitTemplate(ConnectionFactory connectionFactory, String exchange, String routingKey) { Assert.notNull(connectionFactory, "'connectionFactory' cannot be null"); Assert.notNull(routingKey, "'routingKey' cannot be null"); this.template = new RabbitTemplate(connectionFactory); this.template.setExchange(exchange == null ? "" : exchange); this.template.setRoutingKey(routingKey); this.container = null; this.replyAddress = null; this.directReplyToContainer = new DirectReplyToMessageListenerContainer(this.template.getConnectionFactory()); this.directReplyToContainer.setMessageListener(this); }
Assert.notNull(replyQueue, "'replyQueue' cannot be null"); this.template = new RabbitTemplate(connectionFactory); this.template.setExchange(exchange == null ? "" : exchange); this.template.setRoutingKey(routingKey); this.container = new SimpleMessageListenerContainer(connectionFactory);
Assert.notNull(replyQueue, "'replyQueue' cannot be null"); this.template = new RabbitTemplate(connectionFactory); this.template.setExchange(exchange == null ? "" : exchange); this.template.setRoutingKey(routingKey); this.container = new SimpleMessageListenerContainer(connectionFactory);
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) { RabbitTemplate template = new RabbitTemplate(connectionFactory); template.setExchange(EXCHANGE); return template;
@Bean RabbitTemplate rabbitTemplate(org.springframework.amqp.rabbit.connection.ConnectionFactory cf, ObjectMapper mapper) { RabbitTemplate template = new RabbitTemplate(cf); template.setExchange(EXCHANGE_NAME); RetryTemplate retry = new RetryTemplate(); ExponentialBackOffPolicy backOff = new ExponentialBackOffPolicy(); backOff.setInitialInterval(1000); backOff.setMultiplier(1.5); backOff.setMaxInterval(60000); retry.setBackOffPolicy(backOff); template.setRetryTemplate(retry); template.setMessageConverter(new Jackson2JsonMessageConverter(mapper)); return template; }
rabbitTemplate.setExchange(exchange); rabbitTemplate.setRoutingKey(routingKey);
/** * @return Rabbit template with incorrect fixed reply queue. */ @Bean public RabbitTemplate fixedReplyQRabbitTemplateWrongQueue() { RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory()); template.setExchange(ex().getName()); template.setRoutingKey("test"); template.setReplyAddress(requestQueue().getName()); return template; }
/** * @return Rabbit template with fixed reply queue and small timeout. */ @Bean public RabbitTemplate fixedReplyQRabbitTemplate() { RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory()); template.setExchange(ex().getName()); template.setRoutingKey("dlx.reply.test"); template.setReplyAddress(replyQueue().getName()); template.setReplyTimeout(1); return template; }
/** * @return Rabbit template with fixed reply queue. */ @Bean public RabbitTemplate fixedReplyQRabbitTemplate() { RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory()); template.setExchange(ex().getName()); template.setRoutingKey("test"); template.setReplyAddress(replyExchange().getName() + "/" + replyQueue().getName()); return template; }
/** * @return Rabbit template with fixed reply queue, no reply container, no receive timeout. */ @Bean public RabbitTemplate fixedReplyQRabbitTemplateNoReplyContainer() { RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory()); template.setExchange(ex().getName()); template.setRoutingKey("testNoContainer"); template.setReplyAddress(replyExchange().getName() + "/" + replyQueue().getName()); return template; }
@Test public void testSendAndReceiveWithFanout() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); FanoutExchange exchange = new FanoutExchange("fanout"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange)); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); try { template.convertAndSend("message"); String result = getResult(consumer); assertEquals("message", result); } finally { consumer.stop(); } return null; }); admin.deleteExchange("fanout"); }
@Test // @Ignore("Not sure yet if we need to support a use case like this") public void testSendAndReceiveWithTopicConsumeInBackground() throws Exception { RabbitAdmin admin = new RabbitAdmin(connectionFactory); TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); final CachingConnectionFactory cachingConnectionFactory = new CachingConnectionFactory(); cachingConnectionFactory.setHost("localhost"); final RabbitTemplate template = new RabbitTemplate(cachingConnectionFactory); template.setExchange(exchange.getName()); BlockingQueueConsumer consumer = template.execute(channel -> { BlockingQueueConsumer consumer1 = createConsumer(template); String tag = consumer1.getConsumerTags().iterator().next(); assertNotNull(tag); return consumer1; }); template.convertAndSend("foo", "message"); String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("foo.end", "message"); result = getResult(consumer); assertEquals("message", result); consumer.stop(); admin.deleteExchange("topic"); cachingConnectionFactory.destroy(); }
TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName());
@Test public void testSendAndReceiveWithTopicSingleCallback() throws Exception { final RabbitAdmin admin = new RabbitAdmin(connectionFactory); final TopicExchange exchange = new TopicExchange("topic"); admin.declareExchange(exchange); template.setExchange(exchange.getName()); admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("*.end")); template.execute(channel -> { BlockingQueueConsumer consumer = createConsumer(template); String tag = consumer.getConsumerTags().iterator().next(); assertNotNull(tag); template.convertAndSend("foo", "message"); try { String result = getResult(consumer); assertEquals(null, result); template.convertAndSend("foo.end", "message"); result = getResult(consumer); assertEquals("message", result); } finally { consumer.getChannel().basicCancel(tag); } return null; }); admin.deleteExchange("topic"); }