ActiveMQ消息队列技术融合Spring过程解析

网友投稿 232 2022-12-20


ActiveMQ消息队列技术融合Spring过程解析

这篇文章主要介绍了ActiveMQ消息队列技术融合Spring过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下

一、业务逻辑

我想在修改一个物品的状态时,同时发送广播,给对应的监听器去实现,此商品存储到solr中,同时通过网页静态模板生成一个当前物品的详情页面,此时用到了广播机制

当我删除一个商品时,发送一个广播,给对应的监听器,同时删除solr中对应的物品。

广播机制:必须要同时在线,才能接收我的消息

使用消息中间件需要导入配置文件

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

发布广播:

if ("1".equals(status)){

jmsTemplate.send(topicPageAndSolrDestination, new MessageCreator() {

@Override

public Message createMessage(Session session) throws JMSException {

TextMessage textMessage = session.createTextMessage(String.valueOf(id));

return textMessage;

}

});

}

监听器1,将当前商品存入solr中:操作solr的服务器配置文件

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

#对应的用来监听执行往solr中保存库存的消息

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

#对应的用来监听执行往solr中保存库存的消息

监听器类

public class ItemSearchListener implements MessageListener {

@Autowired

private SearchService searchService;

@Autowired

private ItemDao itemDao;

@Override

public void onMessage(Message message) {

//获取生产者发布的广播,往solr中添加库存列表

ActiveMQTextMessage atm = (ActiveMQTextMessage) message;

try {

//获取广播中的数据。

Long goodsId = Long.valueOf(atm.getText());

//通过传过来的商品id去查询库存表

ItemQuery query = new ItemQuery();

ItemQuery.Criteria criteria = query.createCriteria();

criteria.andGoodsIdEqualTo(goodsId);

//查询对应商品id的库存表

List items = itemDao.selectByExample(query);

        //调用对应的方法,往solr中添加当前商品对应库存信息

searchService.importList(items);

} catch (JMSException e) {

e.printStackTrace();

}

}

}

监听器类2:配置文件

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

&lthttp://;bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">

xmlns:jms="http://springframework.org/schema/jms"

xsi:schemaLocation="http://springframework.org/schema/beans

http://springframework.org/schema/beans/spring-beans.xsd

http://springframework.org/schema/context

http://springframework.org/schema/context/spring-context.xsd">

&lthttp://;bean class="org.springframework.jms.listener.DefaultMessageListenerContainer">

监听器类2:生成静态网页模板

public class PageListener implements MessageListener {

@Autowired

private cmsService cmsService;

@Override

public void onMessage(Message message) {

ActiveMQTextMessage atm = (ActiveMQTextMessage) message;

try {

Long goodsId = Long.valueOf(atm.getText());

Map goodsData = cmsService.findGoodsData(goodsId);

cmsService.createStaticPage(goodsId,goodsData);

} catch (Exception e) {

e.printStackTrace();

}

}

}

点对点

当我删除商品时,我需要对应的服务进行删除solr中库存信息,添加和删除使用的是同一个服务中,使用的是上面的配置文件

//发布广播,

@Autowired

private ActiveMQTopic topicPageAndSolrDestination;

//在修改的代码方法中来广播发布当前商品的id

if (ids.length>0) {

jmsTemplate.send(queueSolrDeleteDestination, new MessageCreator() {

@Override

public Message createMessage(Session session) throws JMSException {

TextMessage textMessage = session.createTextMessage(String.valueOf(ids));

return textMessage;

}

});

}

#执行删除solr中库存信息

public class ItemDeleteListener implements MessageListener {

@Autowired

private SearchService searchService;

@Override

public void onMessage(Message message) {

ActiveMQTextMessage atm = (ActiveMQTextMessage) message;

try {

Long goodsId = Long.valueOf(atm.getText());

searchService.deleteById(goodsId);

} catch (JMSException e) {

e.printStackTrace();

}

}

}


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Spring MVC4.1服务器端推送实现过程解析
下一篇:Springboot整合thymleaf模板引擎过程解析
相关文章

 发表评论

暂时没有评论,来抢沙发吧~