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();}}} }
View Code
二、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;@Autowiredprivate ICartService ics = null;@Autowiredprivate IStoreService iss = null;@Autowiredprivate IOrderService ios = null;@Autowiredprivate 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();}}
View Code
三、监听器实现,当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();}}
View Code
四、web.xml中配置监听器
<listener><listener-class>org.tarena.front.listener.ActiveMQinitListener</listener-class></listener>
View Code
转载于:https://www.cnblogs.com/xiaoliangup/p/9363938.html
ActiveMQ 简单应用相关推荐
- JMS学习(3):--ActiveMQ简单的HelloWorld实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- 深入浅出JMS(三)--ActiveMQ简单的HelloWorld实例
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- ActiveMQ简单介绍以及安装
ActiveMQ简单介绍以及安装 现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文深入浅出JMS(一)–JMS ...
- JMS(Java消息服务)(Activemq简单介绍)
是什么? JMS(java消息服务)是规范,它定义了一些规则,一些接口.具体实现由各种做这个产品的厂家或开源组织来实现. 为什么? 在JMS还没有诞生前,每个企业都会有自己的一套内部消息系统,比如项目 ...
- 深入浅出JMS(三)–ActiveMQ简单的HelloWorld实例(转载)
第一篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了JMS的两种消息模型:点对点和发布订阅模型,以及消息被消费的两个方式:同步和异步,JMS编程模型的对象,最后说了JMS的优点. 第二篇博文深入 ...
- JMS学习(2):ActiveMQ简单介绍以及安装
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了消息通信的规范JM ...
- ActiveMQ简单介绍+简单实例
本文出自:http://www.open-open.com/lib/view/open1388994166156.html 1. JMS基本概念 JMS(Java Message Servi ...
- ActiveMQ简单介绍
消息通信的规范JMS,我们这篇博文介绍一款开源的JMS具体实现--ActiveMQ.ActiveMQ是一个易于使用的消息中间件. 消息中间件 我们简单的介绍一下消息中间件,对它有一个基本认识就好,消息 ...
- 深入浅出JMS(二)–ActiveMQ简单介绍以及安装(转载)
现实的企业中,对于消息通信的应用一直都非常的火热,而且在J2EE的企业应用中扮演着特殊的角色,所以对于它研究是非常有必要的. 上篇博文深入浅出JMS(一)–JMS基本概念,我们介绍了消息通信的规范JM ...
最新文章
- php前面有人排队,PHP实现打印类(实现队列排队打印)
- RT/Metro商店应用如何如何获取图片的宽高
- PHP curl_setopt函数用法介绍中篇
- 人工智能时代的用户体验设计与创新
- 开发者论坛一周精粹(第六十八期) 如何把ecs转到另外一个账号?
- VS Code的Error: Running the contributed command: ‘_workbench.downloadResource‘ failed解决
- multer处理post请求的代码演示
- 流程图符号以及绘制流程图方法
- 转载关于使用Ant打包Flex的一些脚本
- Linux驱动中delayed_workqueue使用:
- 从NMEA0183到GNSS定位数据获取(二)软件篇
- 破解wifi密码最简单教程 就三步
- SVD——奇异值分解概述
- 大数据和人工智能属于什么专业 - 学大数据和人工智能出来做什么
- 关于各种网站音频mp3的外链地址,真实的外链播放地址
- 计算机总是提醒更新,电脑关机的时候总是提示系统正在更新怎么办?
- INDEX REBUILD和INDEX REORGANIZE和UPDATE STATISTICS是否涉及Sch-M的案例分析
- 综合评价之熵值法+TOPSIS
- db(德邦快递单号查询)
- FreeRTOS学习-队列管理