ActiveMQ的简单使用

阅读更多

ActiveMQ的简单使用

ActiveMQ是一种开源的,实现了JMS规范的,面向消息(MOM)的中间件,为应用程序提供高效的、可扩展的、稳定的和安全的企业级消息通信。

相关文章:
范例项目: http://wosyingjun.iteye.com/blog/2312553 
ActiveMQ集群高可用方案:http://wosyingjun.iteye.com/blog/2314683

 
ActiveMQ组成:

ActiveMQ的简单使用_第1张图片

 
ActiveMQ接发送消息流程图:

ActiveMQ的简单使用_第2张图片

一. ActiveMQ的安装和配置

1、官网下载Linux版的ActiveMQ(最新版本为5.13.4)
https://activemq.apache.org/download.html
2、解压安装
tar -zxvf apache-activemq-5.13.4-bin.tar.gz
3、配置(这里采用默认配置,无需修改)
vim /usr/lical/activemq-1/conf/activemq.xml
4、启动
cd /usr/local/activemq-1/bin
./activemq start
5、打开管理界面(管理界面可以查看并管理所有队列及消息)
http://192.168.1.100:8161

二. Spring结合ActiveMQ使用

 1、pom文件引入依赖
		
		
			org.apache.activemq
			activemq-core
			5.7.0
		
		
			org.apache.activemq
			activemq-pool
			5.13.3
		
		
 
2、spring-mq配置文件



    
    
        
        
        
        
        
        
    


    
    
        
            
                
                
            
        
    
    
        
        
        
        
        
        
    
    
        
        
        
        
    

    
    
        
        
    

    
    
        
        
        
    

    
    
        
    
    
        
    


    
    
    
        
        
        
        
        
        
        
        
        
    

    
        
        
        
        
        
        
        
        
        
    

    
    
    


    
    
        
        
        
        
        
        
    

    
        
        
        
        
        
        
    

 
3、重试机制以及死信的配置

    
        
            
                
                
            
        
    
    
        
        
        
        
        
        
    
    
        
        
        
        
    
 
4、发送端代码
package com.yingjun.ssm.biz;

import com.alibaba.fastjson.JSONObject;
import com.yingjun.ssm.common.model.BizOperator;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * @author yingjun
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application.xml")
public class Application {

    private final Logger log = LoggerFactory.getLogger(Application.class);

    @Autowired
    private JmsTemplate bizMqJmsTemplate;

    @Test
    public void mailSend() throws Exception {
        bizMqJmsTemplate.setSessionTransacted(true);
        for (int i = 0; i < 1; i++) {
            log.info("==>send message" + i);
            bizMqJmsTemplate.send(new MessageCreator() {
                @Override
                public Message createMessage(Session session) throws JMSException {
                    log.info("getTransacted:" + session.getTransacted());
                    BizOperator operator = new BizOperator("testDistributedTransaction", 1001);
                    return session.createTextMessage(JSONObject.toJSONString(operator));
                }
            });
            log.info("==>finish send message"+ i);
        }
        while (true) {

        }
    }
}
 
5、接受端代码
package com.yingjun.ssm.mq.listener;

import com.alibaba.fastjson.JSONObject;
import com.yingjun.ssm.common.model.BizOperator;
import com.yingjun.ssm.mq.biz.TransactionBizService;
import org.apache.activemq.command.ActiveMQTextMessage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.listener.SessionAwareMessageListener;
import org.springframework.stereotype.Component;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * 
 * @author yingjun
 */
@Component
public class TransactionBizMessageListener implements SessionAwareMessageListener {

    private static final Logger log = LoggerFactory.getLogger(TransactionBizMessageListener.class);
    private final String transactionBiz = "testDistributedTransaction";

    @Autowired
    private TransactionBizService transactionBizService;

    /**
     * @param message
     * @param session
     */
    public void onMessage(Message message, Session session) throws JMSException{
        //这里建议不要try catch,让异常抛出,通过redeliveryPolicy去重试,达到重试次数进入死信DLQ(Dead Letter Queue)
        ActiveMQTextMessage msg = (ActiveMQTextMessage) message;
        String ms = ms = msg.getText();
        log.info("==>receive message:" + ms);
        // 转换成相应的对象
        BizOperator operator = JSONObject.parseObject(ms, BizOperator.class);
        if (operator != null && transactionBiz.equals(operator.getOperator())) {
            transactionBizService.addScoreBySyn(100);
            //throw new RuntimeException("test redeliveryPolicy");
        } else {
            log.info("==>message:" + ms + " no about operator!");
        }
    }
}

 

如上所以就完成了Spring结合ActiveMQ的简单实现,完整代码可在文章最上头的范例项目中找到。

你可能感兴趣的:(ActiveMQ,消息中间件)