JMS-ActiveMQ-Spring

一、添加依赖

<dependencies>
    <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
    <!--  嵌入式activemq的broker所需要的依赖包   -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>5.16.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.xbean/xbean-spring -->
    <dependency>
        <groupId>org.apache.xbean</groupId>
        <artifactId>xbean-spring</artifactId>
        <version>4.20</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind -->
    <!--  嵌入式activemq的broker所需要的依赖包   -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.12.4</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.springframework/spring-jms -->
    <!-- activemq对JMS的支持,整合Spring和ActiveMQ-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jms</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-pool -->
    <!--activemq所需要的pool包配置-->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-pool</artifactId>
        <version>5.16.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-core -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-aop -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.springframework/spring-orm -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>5.3.9</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjrt -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjrt</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
        <scope>runtime</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/cglib/cglib -->
    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>3.2.12</version>
    </dependency>
</dependencies>

二、spring配置文件

<?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.xsd">
	<!--包的自动扫描-->
	<context:component-scan base-package="edu.dj.spring"/>

	<!--配置生产者-->
	<bean id="jmsFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop">
		<property name="connectionFactory">
			<!--真正可以生产Connection的ConnectionFactory,由对应的服务商提供-->
			<bean class="org.apache.activemq.ActiveMQConnectionFactory">
				<property name="brokerURL" value="tcp://192.168.10.100:61616"/>
			</bean>
		</property>
		<property name="maxConnections" value="100"/>
	</bean>

	<!--队列目的地,点对点-->
	<bean id="destinationQueue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg index="0" value="spring-active-queue"/>
	</bean>

	<!--spring提供的JMS工具类,它可以进行消息的发送、接收-->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="jmsFactory"/>
		<property name="defaultDestination" ref="destinationQueue"/>
		<property name="messageConverter">
			<bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
		</property>
 	</bean>
</beans>

三、队列

3.1 生产者

@Service
public class SpringActiveMQProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringActiveMQProducer producer = (SpringActiveMQProducer) ctx.getBean("springActiveMQProducer");
        producer.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(">>>Spring-ActiveMQ-Send>>>>");
                return textMessage;
            }
        });
        System.out.println("发送成功....");
    }
}

3.2 消费者

@Service
public class SpringActiveMQConsumer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        SpringActiveMQConsumer consumer = (SpringActiveMQConsumer) ctx.getBean("springActiveMQConsumer");

        String retValue = (String) consumer.jmsTemplate.receiveAndConvert();

        System.out.println("Spring-消费者接收消息:"+retValue);
    }
}

四、主题

4.1 Spring配置文件,新增topic主题

<!--主题目的地,一对多-->
<bean id="destinationTopic" class="org.apache.activemq.command.ActiveMQTopic">
    <constructor-arg index="0" value="spring-active-topic"/>
</bean>

<!--spring提供的JMS工具类,它可以进行消息的发送、接收-->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
    <property name="connectionFactory" ref="jmsFactory"/>
    <property name="defaultDestination" ref="destinationTopic"/>
    <property name="messageConverter">
        <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/>
    </property>
</bean>

4.2 生产者

@Service
public class SpringActiveMQProducer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
        SpringActiveMQProducer producer = (SpringActiveMQProducer) ctx.getBean("springActiveMQProducer");
        producer.jmsTemplate.send(new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                TextMessage textMessage = session.createTextMessage(">>>Spring-ActiveMQ-Send>>>>");
                return textMessage;
            }
        });
        System.out.println("发送成功....");
    }
}

4.3 消费者

@Service
public class SpringActiveMQConsumer {

    @Autowired
    private JmsTemplate jmsTemplate;

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");

        SpringActiveMQConsumer consumer = (SpringActiveMQConsumer) ctx.getBean("springActiveMQConsumer");

        String retValue = (String) consumer.jmsTemplate.receiveAndConvert();

        System.out.println("Spring-消费者接收消息:"+retValue);
    }
}

五、监听器配置(不需要启动消费者)

5.1 Spring配置文件

<!--配置监听器-->
	<bean id="jmsListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="jmsFactory"/>
		<property name="destination" ref="destinationTopic"/>
		<!--public class MyMessageListener implements MessageListener-->
		<property name="messageListener" ref="myMessageListener"/>
	</bean>
    <!-- 在MyMessageListener类添加Component或者添加以下bean -->
    <!--<bean id="myMessageListener" class="edu.dj.spring.MyMessageListener"/>-->

5.2 编写一个类实现消息监听

@Component
public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        if (message != null && message instanceof TextMessage){
            TextMessage textMessage = (TextMessage) message;

            try {
                System.out.println(textMessage.getText());
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}
posted @ 2021-08-15 09:15  DingJie1024  阅读(28)  评论(0编辑  收藏  举报