spring中使用ActiveMQ

ActiveMQ自行安装
pox.xml文件配置

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com</groupId>
    <artifactId>ActiveMQ</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>ActiveMQ</name>
    <url>http://maven.apache.org</url>
    <properties>
        <spring.version>4.2.9.RELEASE</spring.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>

    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.11</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-web</artifactId>
            <version>${spring.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${spring.version}</version>
        </dependency>

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.7.0</version>
            <!-- 用<exclusion>来排除一些不需要同时下载的依赖jar -->
            <exclusions>
                <exclusion>
                    <artifactId>spring-context</artifactId>
                    <groupId>org.springframework</groupId>
                </exclusion>
            </exclusions>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

common.xml 公共的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    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.2.xsd">

    <context:annotation-config></context:annotation-config>
    <!-- ActiveMQ 为我们提供的ConnectionFactory -->
    <bean id="MQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
        <property name="brokerURL" value="tcp://127.0.0.1:61616" />
    </bean>

    <!-- spring jms 提供的连接池 -->
    <bean id="connectionFactory"
        class="org.springframework.jms.connection.SingleConnectionFactory">
        <property name="targetConnectionFactory" ref="MQConnectionFactory"></property>
    </bean>

    <!-- 一个队列的目的地 点对点 -->
    <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue">
        <constructor-arg value="queue"></constructor-arg>
    </bean>

    <!-- 配置主题模式 目的地 -->
    <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic">
        <constructor-arg value="topic"></constructor-arg>
    </bean>
</beans>

消费者配置文件(consumer.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    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.2.xsd">

    <import resource="common.xml" />

    <bean id="consumerMessageListener" class="com.jms.spring.consumer.ConsumerMessageListener"></bean>
    <!-- 配置消息(jms) 容器 管理 连接工产 消息目的地 -->
    <bean id="jmsContainer"
        class="org.springframework.jms.listener.DefaultMessageListenerContainer">
        <property name="connectionFactory" ref="connectionFactory"></property>
        <!-- 使用 队列模式 -->
        <property name="destination" ref="topicDestination"></property>
        <!-- 使用 主题模式 -->
        <!-- <property name="destination" ref="queueDestination"></property> -->
        <property name="messageListener" ref="consumerMessageListener"></property>
    </bean>


</beans>

生产者配置文件(producer.xml)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    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.2.xsd">

<!-- 引用公共的 配置文件 -->
    <import resource="common.xml"/>

    <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
        <property name="connectionFactory" ref="connectionFactory"></property>
    </bean>

    <bean id="ProducerServerImpl" class="com.jms.spring.producer.ProducerServerImpl"></bean>

</beans>

消费者代码实现

//ConsumerMessageListener.java

package com.jms.spring.consumer;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

public class ConsumerMessageListener implements MessageListener {

    //进行 监听
    @Override
    public void onMessage(Message message) {
        // TODO Auto-generated method stub
        TextMessage textMessage =(TextMessage) message;
        System.out.println(textMessage);
    }
}

//AppConsumer.java 启动消费者

package com.jms.spring.consumer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppConsumer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ApplicationContext context = new ClassPathXmlApplicationContext("/consumer.xml");

    }

}

生产者代码实现

//定义接口
package com.jms.spring.producer;
public interface ProducerServer {
    void sendMessage(String message);
}

//实现接口
package com.jms.spring.producer;

import javax.annotation.Resource;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;
import javax.jms.TextMessage;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;

public class ProducerServerImpl implements ProducerServer {

    @Autowired
    JmsTemplate jmsTemplate;
    //使用 队列模式
    //@Resource(name = "queueDestination")
    //使用 主题模式
    @Resource(name="topicDestination")
    Destination destination;

    @Override
    public void sendMessage(final String message) {
        // TODO Auto-generated method stub
        //jmsTemplate  发送消息
        jmsTemplate.send(destination, new MessageCreator() {
            //创建消息
            @Override
            public Message createMessage(Session session) throws JMSException {
                // TODO Auto-generated method stub
                TextMessage textMessage = session.createTextMessage(message);
                return textMessage;
            }

        });
        System.out.println("生产 消息:" + message);
    }
}

//启动生产者
package com.jms.spring.producer;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

//import org.springframework.context.Applic
public class AppProducer {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        ApplicationContext context=new ClassPathXmlApplicationContext("/producer.xml"); 

        ProducerServer server=context.getBean(ProducerServer.class);

        for (int i = 0; i < 50; i++) {
            server.sendMessage("test:"+i);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值