Advertisement
Guest User

Untitled

a guest
Jul 10th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. @Configuration
  2. @EnableAutoConfiguration
  3. @PropertySource("file:${HOME}/common/config/wave-planning.properties")
  4. public class RabbitMQConfiguration {
  5.  
  6. private final static String QUEUE_NAME = "orderPlanQueue";
  7.  
  8. private static final String EXCHANGE_NAME = "orderPlanExchange";
  9.  
  10. private static final String DL_EXCHANGE_NAME = "deadLetterExchange";
  11.  
  12. private static final String DL_QUEUE_NAME = "deadLetterQueue";
  13.  
  14. @Value("${rabbitmq.host:localhost}")
  15. private String host;
  16.  
  17. @Value("${rabbitmq.port:5672}")
  18. private int port;
  19.  
  20. @Value("${rabbitmq.user:guest}")
  21. private String userName;
  22.  
  23. @Value("${rabbitmq.password:guest}")
  24. private String password;
  25.  
  26. @Value("${rabbitmq.initial_backoff_interval:1000}")
  27. private int INITIAL_INTERVAL_IN_MILLISECONDS;
  28.  
  29. @Value("${rabbitmq.max_backoff_interval:10000}")
  30. private int MAX_INTERVAL_IN_MILLISECONDS;
  31.  
  32. @Autowired
  33. OrderPlanService orderPlanService;
  34.  
  35. @Bean
  36. Queue queue() {
  37. Map<String, Object> qargs = new HashMap<String, Object>();
  38. qargs.put("x-dead-letter-exchange", DL_EXCHANGE_NAME);
  39. return new Queue(QUEUE_NAME, false, false, false, qargs);
  40. }
  41.  
  42. @Bean
  43. TopicExchange exchange() {
  44. return new TopicExchange(EXCHANGE_NAME);
  45. }
  46.  
  47. @Bean
  48. FanoutExchange deadLetterExchange() { return new FanoutExchange(DL_EXCHANGE_NAME); }
  49.  
  50. @Bean
  51. Queue deadLetterQueue() { return new Queue(DL_QUEUE_NAME); }
  52.  
  53. @Bean
  54. Binding deadLetterBinding(Queue deadLetterQueue, FanoutExchange deadLetterExchange) {
  55. return BindingBuilder.bind(deadLetterQueue).to(deadLetterExchange);
  56. }
  57.  
  58. @Bean
  59. Binding binding(Queue queue, TopicExchange exchange) {
  60. return BindingBuilder.bind(queue).to(exchange).with(QUEUE_NAME);
  61. }
  62.  
  63. @Bean
  64. public ConnectionFactory connectionFactory() {
  65. CachingConnectionFactory connectionFactory = new CachingConnectionFactory(host);
  66. connectionFactory.setPort(port);
  67. connectionFactory.setUsername(userName);
  68. connectionFactory.setPassword(password);
  69. return connectionFactory;
  70. }
  71.  
  72. @Bean
  73. public MessageConverter Jackson2JsonMessageConverter() {
  74. return new Jackson2JsonMessageConverter();
  75. }
  76.  
  77. @Bean
  78. public AmqpTemplate rabbitTemplate() {
  79. RabbitTemplate template = new RabbitTemplate(connectionFactory());
  80.  
  81. RetryTemplate retry = new RetryTemplate();
  82. ExponentialBackOffPolicy policy = new ExponentialBackOffPolicy();
  83.  
  84. policy.setInitialInterval(INITIAL_INTERVAL_IN_MILLISECONDS);
  85. policy.setMultiplier(2);
  86. policy.setMaxInterval(MAX_INTERVAL_IN_MILLISECONDS);
  87.  
  88. retry.setBackOffPolicy(policy);
  89. template.setRetryTemplate(retry);
  90.  
  91. template.setRoutingKey(QUEUE_NAME);
  92. template.setMessageConverter(Jackson2JsonMessageConverter());
  93. return template;
  94. }
  95.  
  96. @Bean
  97. SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
  98. SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
  99. container.setConnectionFactory(connectionFactory);
  100. container.setMessageConverter(Jackson2JsonMessageConverter());
  101. container.setQueueNames(QUEUE_NAME);
  102. container.setMessageListener(listenerAdapter);
  103. container.setDefaultRequeueRejected(false);
  104. return container;
  105. }
  106.  
  107. @Bean
  108. MessageListenerAdapter listenerAdapter() {
  109. return new MessageListenerAdapter(orderPlanService, "consume");
  110. }
  111. }
  112.  
  113. @Service
  114. @Transactional
  115. public class BaseOrderPlanService implements OrderPlanService {
  116.  
  117. ....
  118.  
  119. @Override
  120. public void consume(Object object) {
  121. throw new IllegalArgumentException("Test");
  122. }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement