传统模式。
<!--这个是主题目的地,一对多的 -->
<bean id="pointTopic" class="java.lang.String" c:_0="point-topic"/>
<bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
<constructor-arg ref="pointTopic"/>
</bean>
<!-- 消息监听容器,属性中引用的对象要和生产者的一致 -->
<bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">
<property name="connectionFactory" ref="jmsFactory" />
<property name="destination" ref="topicDestination" />
<property name="messageListener" >
<bean class="com.citylinkcloud.parking_charger.service.NotifyChargerJmsMessageListener"/>
</property>
<property name="pubSubDomain" value="true"/>
</bean>
发送。
@Resource
ActiveMQTopic topicDestination;
Map<String, Object> nofityChargerMap =new java.util.HashMap<>(2);
nofityChargerMap.put("pointNo",parkingPointNo);
nofityChargerMap.put("json",json);
jmsTemplate.convertAndSend(topicDestination,nofityChargerMap);//这里需要注意的一点就是。发布topic的时候,这第1个参数不能是字符串。是字符串的话就默认为是queue队列模式。
logger.debug("空位变化JMS发送成功{}."+topicDestination);
接收。
@Service
public class NotifyChargerJmsMessageListener implements javax.jms.MessageListener{
Logger logger=Logger.getLogger(getClass());
@Resource
String pointTopic;
@Override
public void onMessage(Message message) {
Map<String, Object> msgMap = null;
try {
msgMap = ((ActiveMQMapMessage) message).getContentMap();
logger.debug("收到车位变化消息:ActiveMQMapMessage.map:"+msgMap);
// String pointNo = msgMap.get("pointNo").toString();
// JSONObject data= (JSONObject) msgMap.get("json");
} catch (JMSException e) {
e.printStackTrace();
}
}
}
新型写法。springboot
配置。
@Bean("connectionFactory")
public ActiveMQConnectionFactory connectionFactory() {
ActiveMQConnectionFactory f = new ActiveMQConnectionFactory(userName, password, brokerUrl);
f.setTrustAllPackages(true);//这个设置也是很重要的。
return f;
}
@Bean("jmsTemplate")
JmsTemplate jmsTemplate(@Qualifier("connectionFactory") ConnectionFactory connectionFactory) {
JmsTemplate jmsTemplate = new JmsTemplate(connectionFactory);
// jmsTemplate.setMessageConverter(jacksonJmsMessageConverter);
//设置优先级,0最低,9最高
jmsTemplate.setPriority(9);
return jmsTemplate;
}
@Bean("jmsListenerContainerTopic")
public JmsListenerContainerFactory<?> jmsListenerContainerTopic(@Qualifier("connectionFactory")ActiveMQConnectionFactory connectionFactory){
DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
//设置为发布订阅方式, 默认情况下使用的生产消费者方式
bean.setPubSubDomain(true);
bean.setConnectionFactory(connectionFactory);
//设置消息转换器
//bean.setMessageConverter(jacksonJmsMessageConverter());
return bean;
}
发布。
Destination destination = new ActiveMQTopic(mag_device_change_topic);
// ActiveMQObjectMessage msg=new ActiveMQObjectMessage();msg.setObject();
jmsTemplate.convertAndSend(destination,magDeviceStatus);
接收。
/**
* Created by 张某松 on 2020/6/2.
*/
@Service
public class TopicListener {
Logger logger = LoggerFactory.getLogger(getClass());
@Autowired
MagDeviceStatusHistoryMapper magDeviceStatusHistoryMapper;
@JmsListener(destination = "${data.activemq.mag_device_change_topic}", containerFactory = "jmsListenerContainerTopic")
public void receive(Message message){
logger.info("收到JMS主题消息。{}", message);
ActiveMQObjectMessage objectMsg = (ActiveMQObjectMessage)message;
// String msg ="";
try {
// msg = GetMessageContentUtils.getContent(objectMsg.getText());
// System.out.println(objectMsg);
//这个消息来自 com/happyiot/controller/MagDeviceStatusController.java:90
// MagDeviceStatus magDeviceStatus= objectMsg.getBody(MagDeviceStatus.class);
MagDeviceStatus magDeviceStatus= (MagDeviceStatus) objectMsg.getObject();//JsonTool.parse(objectMsg..getText(),MagDeviceStatus.class);//.getBody(MagDeviceStatus.class);
// logger.debug("文本消息{}反序列化{}。",objectMsg.getText(),magDeviceStatus);
MagDeviceStatusHistory historyItem=new MagDeviceStatusHistory();//magDeviceStatus.setId(1);
historyItem.setMagDeviceId(magDeviceStatus.getId());
historyItem.setMagDeviceSerNo(magDeviceStatus.getMagSerNo());
historyItem.setOccupiedStatus(magDeviceStatus.getOccupiedStatus());
historyItem.setChangeTime(magDeviceStatus.getDateTime());
magDeviceStatusHistoryMapper.insert(historyItem);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}