RabbitConfiguration.java 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * Copyright (c) 2011-2020, baomidou (jobob@qq.com).
  3. * <p>
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. * <p>
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. * <p>
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.baomidou.mybatisplus.dts.config;
  17. import com.baomidou.mybatisplus.dts.DtsConstants;
  18. import com.baomidou.mybatisplus.dts.listener.RabbitRmtListener;
  19. import com.baomidou.mybatisplus.dts.sender.RabbitRmtSender;
  20. import org.springframework.amqp.core.Binding;
  21. import org.springframework.amqp.core.DirectExchange;
  22. import org.springframework.amqp.core.Queue;
  23. import org.springframework.amqp.core.TopicExchange;
  24. import org.springframework.amqp.rabbit.annotation.EnableRabbit;
  25. import org.springframework.amqp.rabbit.connection.ConnectionFactory;
  26. import org.springframework.amqp.rabbit.core.RabbitAdmin;
  27. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  28. import org.springframework.amqp.rabbit.transaction.RabbitTransactionManager;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
  31. import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
  32. import org.springframework.context.annotation.Bean;
  33. import org.springframework.context.annotation.Configuration;
  34. import javax.annotation.PostConstruct;
  35. import java.util.HashMap;
  36. import java.util.Map;
  37. /**
  38. * Rabbit MQ 可靠消息配置
  39. *
  40. * @author jobob
  41. * @since 2019-04-19
  42. */
  43. @Configuration
  44. @ConditionalOnClass(EnableRabbit.class)
  45. public class RabbitConfiguration {
  46. @Autowired
  47. protected RabbitTemplate rabbitTemplate;
  48. @Autowired
  49. protected RabbitAdmin rabbitAdmin;
  50. @Bean
  51. public RabbitRmtSender rmtSender() {
  52. return new RabbitRmtSender();
  53. }
  54. @Bean
  55. public RabbitRmtListener rabbitRmtListener() {
  56. return new RabbitRmtListener();
  57. }
  58. @Bean
  59. @ConditionalOnMissingClass("org.springframework.jdbc.datasource.DataSourceTransactionManager")
  60. public RabbitTransactionManager rabbitTransactionManager(ConnectionFactory connectionFactory) {
  61. return new RabbitTransactionManager(connectionFactory);
  62. }
  63. @Bean
  64. @ConditionalOnMissingClass("org.springframework.amqp.rabbit.core.RabbitAdmin")
  65. public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
  66. return new RabbitAdmin(connectionFactory);
  67. }
  68. @PostConstruct
  69. protected void init() {
  70. // make rmt template to support transactions
  71. rabbitTemplate.setChannelTransacted(true);
  72. // define deadletter exchange and queue
  73. rabbitAdmin.declareExchange(new DirectExchange(DtsConstants.RABBIT_DEADLETTER_EXCHANGE, true, false));
  74. rabbitAdmin.declareQueue(new Queue(DtsConstants.RABBIT_DEADLETTER_QUEUE, true, false, false, null));
  75. rabbitAdmin.declareBinding(new Binding(DtsConstants.RABBIT_DEADLETTER_QUEUE, Binding.DestinationType.QUEUE,
  76. DtsConstants.RABBIT_DEADLETTER_EXCHANGE, DtsConstants.RABBIT_DEADLETTER_ROUTINGKEY, null));
  77. // define simple exchange, queue with deadletter support and binding
  78. rabbitAdmin.declareExchange(new TopicExchange(DtsConstants.RABBIT_EXCHANGE, true, false));
  79. Map<String, Object> args = new HashMap<>(2);
  80. args.put("x-dead-letter-exchange", DtsConstants.RABBIT_DEADLETTER_EXCHANGE);
  81. args.put("x-dead-letter-routing-key", DtsConstants.RABBIT_DEADLETTER_ROUTINGKEY);
  82. rabbitAdmin.declareQueue(new Queue(DtsConstants.RABBIT_QUEUE, true, false, true, args));
  83. // declare binding
  84. rabbitAdmin.declareBinding(new Binding(DtsConstants.RABBIT_QUEUE, Binding.DestinationType.QUEUE, DtsConstants.RABBIT_EXCHANGE,
  85. DtsConstants.RABBIT_ROUTINGKEY, null));
  86. }
  87. }