ServiceMycatEventService.java 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. package com.yihu.hos.services;
  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.ObjectMapper;
  4. import com.yihu.hos.core.log.Logger;
  5. import com.yihu.hos.core.log.LoggerFactory;
  6. import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
  7. import com.yihu.hos.web.framework.model.bo.ServiceMycat;
  8. import org.springframework.beans.factory.annotation.Autowired;
  9. import org.springframework.stereotype.Component;
  10. import org.zbus.broker.ZbusBroker;
  11. import org.zbus.mq.Producer;
  12. import org.zbus.net.http.Message;
  13. import java.io.IOException;
  14. /**
  15. * mycat配置文件操作 消息到MQ
  16. *
  17. * @author HZY
  18. * @vsrsion 1.0
  19. * Created at 2017/1/5.
  20. */
  21. @Component
  22. public class ServiceMycatEventService {
  23. static final Logger logger = LoggerFactory.getLogger(ServiceMycatEventService.class);
  24. @Autowired
  25. private ObjectMapper objectMapper;
  26. private ZbusBroker zbusBroker;
  27. @Autowired
  28. public void setZbusBroker(ZbusBroker zbusBroker) {
  29. this.zbusBroker = zbusBroker;
  30. }
  31. public void executeMycatConfig(ServiceMycat servviceMycat, String tenant) {
  32. this.sendMsg(ServiceFlowConstant.EXECUTE_MYCAT, servviceMycat, tenant);
  33. }
  34. private void sendMsg(String event, ServiceMycat servviceMycat, String tenant) {
  35. if (zbusBroker == null) {
  36. logger.error("zbusBroker is null.");
  37. return;
  38. }
  39. try {
  40. String msg = objectMapper.writeValueAsString(servviceMycat);
  41. Producer producer = new Producer(zbusBroker, ServiceFlowConstant.MYCAT_UPDATE + "@" + tenant);
  42. producer.createMQ(); //确定为创建消息队列需要显示调用
  43. Message message = new Message();
  44. message.setHead("event", event);
  45. message.setHead("tenant", tenant);
  46. message.setMethod("POST");
  47. message.setBody(msg);
  48. producer.sendSync(message);
  49. } catch (JsonProcessingException e) {
  50. e.printStackTrace();
  51. logger.error(e.getMessage());
  52. } catch (InterruptedException | IOException e) {
  53. e.printStackTrace();
  54. }
  55. }
  56. }