Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Configuration
- public class FixedReplyQueueConfig {
- @Bean
- public ConnectionFactory rabbitConnectionFactory() {
- CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
- connectionFactory.setHost("localhost");
- connectionFactory.setUsername("urbanbuz");
- connectionFactory.setPassword("ub");
- connectionFactory.setVirtualHost("urbanbuzvhost");
- return connectionFactory;
- }
- /**
- * @return Rabbit template with fixed reply queue.
- */
- @Bean
- public RabbitTemplate fixedReplyQRabbitTemplate() {
- RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory());
- template.setExchange(ex().getName());
- template.setRoutingKey("test");
- template.setReplyQueue(replyQueue());
- return template;
- }
- /**
- * @return The reply listener container - the rabbit template is the listener.
- */
- @Bean
- public SimpleMessageListenerContainer replyListenerContainer() {
- SimpleMessageListenerContainer container = new SimpleMe ssageListenerContainer();
- container.setConnectionFactory(rabbitConnectionFactory());
- container.setQueues(replyQueue());
- container.setMessageListener(fixedReplyQRabbitTemplate());
- return container;
- }
- /**
- * @return The listener container that handles the request and returns the reply.
- */
- @Bean
- public SimpleMessageListenerContainer serviceListenerContainer() {
- SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
- container.setConnectionFactory(rabbitConnectionFactory());
- container.setQueues(requestQueue());
- container.setMessageListener(new MessageListenerAdapter(new PojoListener()));
- return container;
- }
- /**
- * @return a non-durable auto-delete exchange.
- */
- @Bean
- public DirectExchange ex() {
- return new DirectExchange("ub.exchange", false, true);
- }
- @Bean
- public Binding binding() {
- return BindingBuilder.bind(requestQueue()).to(ex()).with("test");
- }
- /**
- * @return an anonymous (auto-delete) queue.
- */
- @Bean
- public Queue requestQueue() {
- return new Queue("ub.request");
- }
- /**
- * @return an anonymous (auto-delete) queue.
- */
- @Bean
- public Queue replyQueue() {
- return new Queue("ub.reply");
- }
- /**
- * @return an admin to handle the declarations.
- */
- @Bean
- public RabbitAdmin admin() {
- return new RabbitAdmin(rabbitConnectionFactory());
- }
- }
- public class PojoListener {
- public String handleMessage(String foo) {
- System.out.println("IN MESSAGE RECEIVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
- return foo.toUpperCase();
- }
- }
- ApplicationContext context = new AnnotationConfigApplicationContext(FixedReplyQueueConfig.class);
- rabbitTemplate = context.getBean(RabbitTemplate.class);
- rabbitTemplate.convertSendAndReceive("tommorow is the weekend!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement