Advertisement
Guest User

Untitled

a guest
Nov 26th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.98 KB | None | 0 0
  1. @Configuration
  2. public class FixedReplyQueueConfig {
  3.  
  4. @Bean
  5. public ConnectionFactory rabbitConnectionFactory() {
  6. CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
  7. connectionFactory.setHost("localhost");
  8. connectionFactory.setUsername("urbanbuz");
  9. connectionFactory.setPassword("ub");
  10. connectionFactory.setVirtualHost("urbanbuzvhost");
  11.  
  12. return connectionFactory;
  13. }
  14.  
  15. /**
  16. * @return Rabbit template with fixed reply queue.
  17. */
  18. @Bean
  19. public RabbitTemplate fixedReplyQRabbitTemplate() {
  20. RabbitTemplate template = new RabbitTemplate(rabbitConnectionFactory());
  21. template.setExchange(ex().getName());
  22. template.setRoutingKey("test");
  23. template.setReplyQueue(replyQueue());
  24. return template;
  25. }
  26.  
  27. /**
  28. * @return The reply listener container - the rabbit template is the listener.
  29. */
  30. @Bean
  31. public SimpleMessageListenerContainer replyListenerContainer() {
  32. SimpleMessageListenerContainer container = new SimpleMe ssageListenerContainer();
  33. container.setConnectionFactory(rabbitConnectionFactory());
  34. container.setQueues(replyQueue());
  35. container.setMessageListener(fixedReplyQRabbitTemplate());
  36. return container;
  37. }
  38.  
  39. /**
  40. * @return The listener container that handles the request and returns the reply.
  41. */
  42. @Bean
  43. public SimpleMessageListenerContainer serviceListenerContainer() {
  44. SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
  45. container.setConnectionFactory(rabbitConnectionFactory());
  46. container.setQueues(requestQueue());
  47. container.setMessageListener(new MessageListenerAdapter(new PojoListener()));
  48. return container;
  49. }
  50.  
  51. /**
  52. * @return a non-durable auto-delete exchange.
  53. */
  54. @Bean
  55. public DirectExchange ex() {
  56. return new DirectExchange("ub.exchange", false, true);
  57. }
  58.  
  59. @Bean
  60. public Binding binding() {
  61. return BindingBuilder.bind(requestQueue()).to(ex()).with("test");
  62. }
  63.  
  64. /**
  65. * @return an anonymous (auto-delete) queue.
  66. */
  67. @Bean
  68. public Queue requestQueue() {
  69. return new Queue("ub.request");
  70. }
  71.  
  72. /**
  73. * @return an anonymous (auto-delete) queue.
  74. */
  75. @Bean
  76. public Queue replyQueue() {
  77. return new Queue("ub.reply");
  78. }
  79.  
  80. /**
  81. * @return an admin to handle the declarations.
  82. */
  83. @Bean
  84. public RabbitAdmin admin() {
  85. return new RabbitAdmin(rabbitConnectionFactory());
  86. }
  87. }
  88.  
  89. public class PojoListener {
  90.  
  91. public String handleMessage(String foo) {
  92. System.out.println("IN MESSAGE RECEIVER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
  93. return foo.toUpperCase();
  94. }
  95. }
  96.  
  97. ApplicationContext context = new AnnotationConfigApplicationContext(FixedReplyQueueConfig.class);
  98. rabbitTemplate = context.getBean(RabbitTemplate.class);
  99.  
  100. rabbitTemplate.convertSendAndReceive("tommorow is the weekend!");
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement