RabbitRmtSender.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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.sender;
  17. import com.baomidou.mybatisplus.core.toolkit.ExceptionUtils;
  18. import com.baomidou.mybatisplus.dts.DtsConstants;
  19. import com.baomidou.mybatisplus.dts.DtsMeta;
  20. import com.baomidou.mybatisplus.dts.parser.IDtsParser;
  21. import com.fasterxml.jackson.core.JsonProcessingException;
  22. import lombok.extern.slf4j.Slf4j;
  23. import org.springframework.amqp.AmqpException;
  24. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  25. import org.springframework.beans.factory.annotation.Autowired;
  26. import org.springframework.stereotype.Component;
  27. /**
  28. * <p>
  29. * RabbitMQ 消息发送者
  30. * </p>
  31. *
  32. * @author hubin
  33. * @since 2019-04-17
  34. */
  35. @Slf4j
  36. @Component
  37. public class RabbitRmtSender implements IRmtSender {
  38. @Autowired
  39. private IDtsParser rmtParser;
  40. @Autowired
  41. private RabbitTemplate rabbitTemplate;
  42. /**
  43. * 发送MQ消息
  44. *
  45. * @param dtsMeta Rabbit元信息对象,用于存储交换器、队列名、消息体
  46. */
  47. @Override
  48. public void send(DtsMeta dtsMeta) {
  49. String object = null;
  50. try {
  51. object = rmtParser.toJSONString(dtsMeta);
  52. rabbitTemplate.convertAndSend(DtsConstants.RABBIT_EXCHANGE,
  53. DtsConstants.RABBIT_ROUTINGKEY, object);
  54. } catch (AmqpException e) {
  55. ExceptionUtils.mpe("rabbit send error, dtsMeta: %s", e, object);
  56. } catch (Exception e) {
  57. ExceptionUtils.mpe("rmt parser error, dtsMeta.event: %s", e, dtsMeta.getEvent());
  58. }
  59. }
  60. }