码迷,mamicode.com
首页 > 其他好文 > 详细

ActiveMQ 简单应用

时间:2018-07-25 10:23:08      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:lap   lock   price   xtu   accept   view   cep   The   mit   

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;
    
    
    @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();
        }
        
    }
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

 

ActiveMQ 简单应用

标签:lap   lock   price   xtu   accept   view   cep   The   mit   

原文地址:https://www.cnblogs.com/xiaoliangup/p/9363938.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!