ActiveMQ使用spring JmsTemplate发送消息(一)

来源:互联网 发布:php授权验证系统v2.1 编辑:程序博客网 时间:2024/05/26 09:56

下载地址:http://activemq.apache.org/download-archives.html

解压 启动bin\activemq.bat

生产者接口:

public interface ProducerService {    /**     * 向指定队列发送消息     */    void sendMessage(Destination destination, final String msg);    /**     * 向默认队列发送消息     */    void sendMessage(final String msg);}
消费者接口:

public interface ConsumerService {    /**     * 接受消息     */    void receive(Destination destination);}
生产者实现:

@Service("producerService")public class ProducerServiceImpl implements ProducerService {    @Resource    private JmsTemplate jmsTemplate;    /**     * 向指定队列发送消息     */    public void sendMessage(Destination destination, final String msg) {        System.out.println("向队列" + destination.toString() + "发送了消息------------" + msg);        jmsTemplate.send(destination, new MessageCreator() {            public Message createMessage(Session session) throws JMSException {                return session.createTextMessage(msg);            }        });    }    /**     * 向默认队列发送消息     */    public void sendMessage(final String msg) {        String destination =  jmsTemplate.getDefaultDestination().toString();        System.out.println("向队列" +destination+ "发送了消息------------" + msg);        jmsTemplate.send(new MessageCreator() {            public Message createMessage(Session session) throws JMSException {                return session.createTextMessage(msg);            }        });    }}
消费者实现:

@Service("consumerService")public class ConsumerServiceImpl implements ConsumerService {    @Resource    private JmsTemplate jmsTemplate;    /**     * 接受消息     */    public void receive(Destination destination) {        TextMessage tm = (TextMessage) jmsTemplate.receive(destination);        try {            System.out.println("从队列" + destination.toString() + "收到了消息:\t"                    + tm.getText());        } catch (JMSException e) {            e.printStackTrace();        }    }}
maven:

<dependencies>   <dependency>     <groupId>junit</groupId>     <artifactId>junit</artifactId>     <version>4.11</version>   </dependency>   <dependency>     <groupId>com.hankcs</groupId>     <artifactId>hanlp</artifactId>     <version>portable-1.3.2</version>   </dependency>   <dependency>     <groupId>org.apache.activemq</groupId>     <artifactId>activemq-all</artifactId>     <version>5.11.0</version>   </dependency>   <dependency>     <groupId>org.springframework</groupId>     <artifactId>spring-jms</artifactId>     <version>4.1.4.RELEASE</version>   </dependency> </dependencies>
spring:

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:context="http://www.springframework.org/schema/context"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xsi:schemaLocation="http://www.springframework.org/schema/beans        http://www.springframework.org/schema/beans/spring-beans.xsd        http://www.springframework.org/schema/context        http://www.springframework.org/schema/context/spring-context-4.1.xsd">    <!-- spring 注解支持 -->    <context:annotation-config />    <context:component-scan base-package="com.activemq.service" />    <!-- 配置JMS连接工厂 -->    <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">        <property name="brokerURL" value="failover:(tcp://localhost:61616)" />    </bean>    <!-- 定义消息队列(Queue-->    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">        <!-- 设置消息队列的名字 -->        <constructor-arg>            <value>queue1</value>        </constructor-arg>    </bean>    <!-- 配置JMS模板(Queue),Spring提供的JMS工具类,它发送、接收消息。 -->    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">        <property name="connectionFactory" ref="connectionFactory" />        <property name="defaultDestination" ref="queueDestination" />        <property name="receiveTimeout" value="10000" />    </bean>    </beans>
测试:

@Testpublic void producerTest(){    // 加载spring    ClassPathXmlApplicationContext springContext = SpringContainer.getSpringContext();    ProducerService producerService = (ProducerService)springContext.getBean("producerService");    ConsumerService consumerService = (ConsumerService)springContext.getBean("consumerService");    Destination destination = (Destination)springContext.getBean("queueDestination");    producerService.sendMessage("测试消息队列");    consumerService.receive(destination);}
public class SpringContainer {    public static final String DEFAULT_SPRING_CONFIG = "classpath:META-INF/spring/*.xml";    public static ClassPathXmlApplicationContext getSpringContext() {        return MySpringContainer.context;    }    private static class MySpringContainer{        private static ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(DEFAULT_SPRING_CONFIG.split("[,\\s]+"));    }}

0 0
原创粉丝点击