package com.mgm.mcrm.common.jms; import org.apache.activemq.command.ActiveMQTopic; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Component; import com.alibaba.fastjson2.JSON; import com.mgm.mcrm.common.dtos.msg.JMSTextMessage; import com.mgm.mcrm.common.utils.LogUtil; @Component public class AppControlMsgSender { @Value("${app_ctrl_topic}") private String ctrlTopicName; private ActiveMQTopic ctrlTopic=null; @Autowired private JmsTemplate jmsTemplate; private ActiveMQTopic getTopic() { return this.ctrlTopic==null?new ActiveMQTopic(ctrlTopicName):this.ctrlTopic; } public void sendMessage(String message) { jmsTemplate.convertAndSend(getTopic(), message); } public boolean sendMessage(String msgType, Object msgData) { boolean result =false; try { JMSTextMessage msg = new JMSTextMessage(); msg.setMsgType(msgType); msg.setMsgContent(msgData==null?"":JSON.toJSONString(msgData)); String message = JSON.toJSONString(msg); jmsTemplate.convertAndSend(getTopic(), message); result=true; }catch(Exception err) { LogUtil.error(AppControlMsgSender.class, "Send JMS Failed:"+err.getMessage()); result=false; } return result; } }