123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869 |
- package com.yihu.hos.services;
- import com.fasterxml.jackson.core.JsonProcessingException;
- import com.fasterxml.jackson.databind.ObjectMapper;
- import com.yihu.hos.core.log.Logger;
- import com.yihu.hos.core.log.LoggerFactory;
- import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
- import com.yihu.hos.web.framework.model.bo.ServiceMycat;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Component;
- import org.zbus.broker.ZbusBroker;
- import org.zbus.mq.Producer;
- import org.zbus.net.http.Message;
- import java.io.IOException;
- /**
- * mycat配置文件操作 消息到MQ
- *
- * @author HZY
- * @vsrsion 1.0
- * Created at 2017/1/5.
- */
- @Component
- public class ServiceMycatEventService {
- static final Logger logger = LoggerFactory.getLogger(ServiceMycatEventService.class);
- @Autowired
- private ObjectMapper objectMapper;
- private ZbusBroker zbusBroker;
- @Autowired
- public void setZbusBroker(ZbusBroker zbusBroker) {
- this.zbusBroker = zbusBroker;
- }
- public void executeMycatConfig(ServiceMycat servviceMycat, String tenant) {
- this.sendMsg(ServiceFlowConstant.EXECUTE_MYCAT, servviceMycat, tenant);
- }
- private void sendMsg(String event, ServiceMycat servviceMycat, String tenant) {
- if (zbusBroker == null) {
- logger.error("zbusBroker is null.");
- return;
- }
- try {
- String msg = objectMapper.writeValueAsString(servviceMycat);
- Producer producer = new Producer(zbusBroker, ServiceFlowConstant.MYCAT_UPDATE + "@" + tenant);
- producer.createMQ(); //确定为创建消息队列需要显示调用
- Message message = new Message();
- message.setHead("event", event);
- message.setHead("tenant", tenant);
- message.setMethod("POST");
- message.setBody(msg);
- producer.sendSync(message);
- } catch (JsonProcessingException e) {
- e.printStackTrace();
- logger.error(e.getMessage());
- } catch (InterruptedException | IOException e) {
- e.printStackTrace();
- }
- }
- }
|