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中配置监听器
手机扫一扫
移动阅读更方便
你可能感兴趣的文章