ActiveMQ 简单应用
ActiveMQ简单应用到复杂的订单模块,提高前台的访问速度。
一、当提交订单后,发送消息给ActiveMQ。
@Service
public class JmsSend { private static ConnectionFactory connectionFactory =new ActiveMQConnectionFactory("failover:(tcp://192.168.174.104:61616,tcp://192.168.174.104:61676)?randomize=false"); private static Connection connection; static { try {
connection = connectionFactory.createConnection(); connection.start(); } catch (JMSException e) { e.printStackTrace();
} } public void sendMsg(Integer customerUuid){
Session session=null; try{ session=connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); Destination queue=session.createQueue("MY_ORDER_QUEUE"); MessageProducer producer=session.createProducer(queue); TextMessage message=session.createTextMessage(customerUuid+""); producer.send(message); session.commit(); }catch(Exception e){ }finally{ try {
session.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} }
} }
二、ActiveMQ消息接收端接收到消息,处理订单具体业务
@Service
public class JmsReceiver { private static ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
"failover:(tcp://192.168.174.104:61616,tcp://192.168.174.104:61676)?randomize=false"); private static Connection connection; @Autowired
private ICartService ics = null; @Autowired
private IStoreService iss = null; @Autowired
private IOrderService ios = null; @Autowired
private IOrderDetailService iods = null; static { try {
connection = connectionFactory.createConnection(); connection.start(); } catch (JMSException e) { e.printStackTrace();
} } public void acceptMsg(){ try {
final Session session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE); Destination queue = session.createQueue("MY_ORDER_QUEUE"); MessageConsumer consumer = session.createConsumer(queue); consumer.setMessageListener(new MessageListener() { public void onMessage(Message message) { TextMessage msg = (TextMessage) message;
Integer customerUuid =null;
try {
customerUuid = Integer.valueOf(msg.getText());
} catch (NumberFormatException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} CartQueryModel cqm = new CartQueryModel();
cqm.getPage().setPageShow(1000);
cqm.setCustomerUuid(customerUuid); Page<CartQueryModel> page = ics.getByConditionPage(cqm);
float totalMoney = 0.0f;
for (CartModel cart : page.getResult()) {
totalMoney += 10;
} OrderModel order = new OrderModel();
order.setCustomerUuid(customerUuid);
order.setOrderTime(DateFormatHelper.long2str(System.currentTimeMillis()));
order.setSaveMoney(0f);
order.setTotalMoney(totalMoney);
order.setState(1);
ios.create(order); OrderQueryModel oqm = new OrderQueryModel();
oqm.setOrderTime(order.getOrderTime());
oqm.setCustomerUuid(customerUuid);
Page<OrderQueryModel> orderPage = ios.getByConditionPage(oqm);
order = orderPage.getResult().get(0); for (CartModel cart : page.getResult()) {
OrderDetailModel orderDetail = new OrderDetailModel();
orderDetail.setGoodsUuid(cart.getGoodsUuid());
orderDetail.setOrderUuid(order.getUuid());
orderDetail.setOrderNum(cart.getBuyNum());
orderDetail.setPrice(10.0f);
orderDetail.setMoney(orderDetail.getPrice()
* orderDetail.getOrderNum());
orderDetail.setSaveMoney(0.0f); iods.create(orderDetail); StoreModel store = iss.getByGoodsUuid(cart.getGoodsUuid());
StoreModel storeModel = new StoreModel();
store.setStoreNum(store.getStoreNum() - cart.getBuyNum());
iss.update(store); ics.delete(cart.getUuid()); } try {
session.commit();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} } }); }catch(Exception e){
e.printStackTrace();
} }
三、监听器实现,当web启动时,开启消息监听。
public class ActiveMQinitListener implements ServletContextListener{
public void contextDestroyed(ServletContextEvent arg0) {
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
JmsReceiver receiver=(JmsReceiver) wac.getBean("jmsReceiver");
Connection conn=receiver.getConnection();
try {
conn.close();
} catch (JMSException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void contextInitialized(ServletContextEvent arg0) {
WebApplicationContext wac=WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
JmsReceiver receiver=(JmsReceiver) wac.getBean("jmsReceiver");
receiver.acceptMsg();
}
}
四、web.xml中配置监听器
<listener>
<listener-class>
org.tarena.front.listener.ActiveMQinitListener
</listener-class>
</listener>
ActiveMQ 简单应用的更多相关文章
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- JMS【三】--ActiveMQ简单的HelloWorld实例
第一篇博文JMS[一]--JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文JMS[二 ...
- 深入浅出JMS(二)--ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了消息通信的规范JM ...
- JMS【二】--ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文JMS[一]--JMS基本概念,我们介绍了消息通信的规范JMS,我 ...
- ActiveMQ简单介绍以及安装
概述 首先简单的介绍一下MQ,MQ英文名MessageQueue,中文名也就是大家用的消息队列,干嘛用的呢,说白了就是一个消息的接受和转发的容器,可用于消息推送. ActiveMQ是Apache所提供 ...
- ActiveMQ简单介绍及安装
消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息中间件(MOM:Message Orient middleware). 消息中间件有很多的用途和优点: 1. 将数据从一个应用程序 ...
- JMS消息队列之ActiveMQ简单示例
废话不多说,在进入主题前先看一张图,对ActiveMQ有个大体的了解: 下面进入主题: 1.添加需要的maven依赖 <!-- active mq begin --> < ...
- 【转】深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
这篇博文,我们使用ActiveMQ为大家实现一种点对点的消息模型.如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍. JMS其实并没有想象的那么高大上,看完这篇博文之后,你就知道什么叫简单,下 ...
- 【转】深入浅出JMS(二)--ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 这篇博文介绍一款开源的JMS具体实现——ActiveMQ.ActiveMQ ...
随机推荐
- Cesium的Property机制总结
前言 Cesium官方教程中有一篇叫<空间数据可视化>(Visualizing Spatial Data).该文文末简单提到了Cesium的Property机制,然后话锋一转,宣告此教程的 ...
- Path.Combine Method
https://docs.microsoft.com/en-us/dotnet/api/system.io.path.combine?view=netframework-4.8#System_IO_P ...
- IDEA导航光标回退和前进快捷键失效
工作中突然发现IDEA里的Ctrl+Alt+Left/Right失效了,即导航光标的回退和前进,影响看代码的效率. 用Windows Hotkey Explorer查看,发现是被igfxHK.exe进 ...
- 有依赖的背包---P1064 金明的预算方案
P1064 金明的预算方案 solution 1 暴搜 70pt dfs (当前搜到了第几个物品,产生的总价值,剩下多少钱) 剪枝 1:如果剩下的钱数<0,直接return就好,没必要继续了 剪 ...
- MySQL 8.0: From SQL Tables to JSON Documents (and back again)
MySQL 8.0: From SQL Tables to JSON Documents (and back again) | MySQL Server Bloghttps://mysqlserver ...
- H5内嵌原生app
前言 其实我们不管是从ios还是安卓都可以看出 原生app能内嵌H5的原因是因为有了webview这个app内嵌浏览器视图,从而使得我们可以开发html然后加载到app中(原理几乎跟pc端请求.加载. ...
- [插件式开发][C#]
Demo 下载 参考文章:https://www.cnblogs.com/hippieZhou/p/9398354.html 技术方面要使用到 依赖注入,可以参考此示例逐步学习:https://git ...
- SQL-W3School-函数:SQL 函数
ylbtech-SQL-W3School-函数:SQL 函数 1.返回顶部 1. SQL 拥有很多可用于计数和计算的内建函数. 函数的语法 内建 SQL 函数的语法是: SELECT function ...
- word excel 未响应
前几天笔记本突然出现word 一打开就未响应的情况,导致完全无法使用.今天发现 excel 也出现了这种情况.今天终于下定决心解决这个问题. 百度上搜索了很多,找到了很多解决方案.总结如下. 一.禁用 ...
- 放射渐变RadialGradient
public RadialGradient(float centerX, float centerY, float radius, int[] colors, float[] stops, TileM ...