初探Activemq

来源:互联网 发布:红包源码 编辑:程序博客网 时间:2024/06/02 10:20

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">项目开发需要用到消息队列。</span>

百度下去好多关于这方便的介绍。。但是下载了他们的项目我是没跑成功。后来才发现Activemq是单独跑的服务。


对于JMS的介绍就不提。


只讲一下我个人使用过程,做一个流水账!


1. 下载Activemq(ActiveMQ 5.11.1 Release)

1.1 解压

1.2 直接运行启动 \apache-activemq-5.11.1\bin\win64\activemq.bat


2.项目支持

        2.1 添加Jar 

需要的可以在这下载 


2.1.1 Maven的方案

<dependencies>        <dependency>            <groupId>junit</groupId>            <artifactId>junit</artifactId>            <version>4.10</version>            <scope>test</scope>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-context</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-jms</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>org.springframework</groupId>            <artifactId>spring-test</artifactId>            <version>${spring-version}</version>        </dependency>        <dependency>            <groupId>javax.annotation</groupId>            <artifactId>jsr250-api</artifactId>            <version>1.0</version>        </dependency>        <dependency>            <groupId>org.apache.activemq</groupId>            <artifactId>activemq-core</artifactId>            <version>5.7.0</version>        </dependency></dependencies>

2.2  编写接收处理代码

@Component("consumerMessageListener")public class ConsumerMessageListener implements SessionAwareMessageListener<TextMessage> {    private static Logger log = Logger.getLogger(ConsumerMessageListener.class);    @Override    public void onMessage(TextMessage message, Session session) throws JMSException {        log.info("接收到消息!" + message.getText());        String filepath = message.getStringProperty("filepath");        int id = message.getIntProperty("category");        log.info("接收到的参数" + filepath + "  " + id);    }   }

2.3 配置Spring

<span style="white-space:pre"></span><!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --><bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"><property name="brokerURL" value="tcp://localhost:61616" /></bean>
<bean id="connectionFactory"class="org.springframework.jms.connection.SingleConnectionFactory"><property name="targetConnectionFactory" ref="targetConnectionFactory" /></bean>
<span style="white-space:pre"></span><!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --><bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"><!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --><property name="connectionFactory" ref="connectionFactory" /></bean>
<span style="white-space:pre"></span><!--这个是队列容器 --><bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"><constructor-arg><value>queue</value></constructor-arg></bean>
<span style="white-space:pre"></span><!-- 消息监听容器 --><bean id="jmsContainer"class="org.springframework.jms.listener.DefaultMessageListenerContainer"><property name="connectionFactory" ref="connectionFactory" /><property name="destination" ref="queueDestination" /><property name="messageListener" ref="consumerMessageListener" /></bean>


2.3 测试发送

@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration(locations={"/applicationContext.xml","/applicationContext-jms.xml"})public class TestConsumerMessageListener {    @Autowired    private JmsTemplate jmsTemplate;        @Autowired    @Qualifier("queueDestination")    private Destination destination;        @Test    public void testSend() {        jmsTemplate.send(destination, new MessageCreator() {                        @Override            public Message createMessage(Session session) throws JMSException {                TextMessage message = session.createTextMessage();                message.setIntProperty("category", 1);                message.setStringProperty("filepath", "C:\\cms\\temp\\115.xls");                return message;            }        });    }    }







0 0