ActiveMq简单使用

xiaoxiao2021-02-28  35

ActiveMq服务端配置:

         首先引入ActiveMq.Jar

                    

        Spring-Mq配置文件:

                    

配置文件如下:

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:amq="http://activemq.apache.org/schema/core" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd" default-autowire="byName"> <!-- Activemq 连接工厂 --> <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg value="system1" /> <constructor-arg value="manager1" /> <constructor-arg value="failover:(tcp://localhost:61616)?timeout=2000" /> </bean> <!-- ConnectionFactory Definition --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="activeMQConnectionFactory" /> </bean> <!-- Default Destination Queue Definition --> <!-- 测试配置多个Destination --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg index="0" value="test.activemq.queue" /> </bean> <!-- JmsTemplate Definition --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="connectionFactory" /> <property name="defaultDestination" ref="destination" /> </bean> <!-- Message Sender Definition 需要在Message接收端注入这两个封装好的类 --> <bean id="messageSender" class="com.yl.protocol.handler.MessageSender"> <constructor-arg index="0" ref="jmsTemplate" /> <constructor-arg index="1" ref="destination" /> </bean> </beans>    

将消息发送到ActiveMq对列中

package com.yl.protocol.handler; import javax.jms.Destination; import org.springframework.jms.core.JmsTemplate; public class MessageSender { private final JmsTemplate jmsTemplate; private final Destination destination; public MessageSender(final JmsTemplate jmsTemplate, final Destination destination) { this.jmsTemplate = jmsTemplate; this.destination = destination; } public void send(final String text) { try { jmsTemplate.setDefaultDestination(destination); jmsTemplate.convertAndSend(text); System.out.println("发送消息 : " + text); } catch (Exception e) { e.printStackTrace(); } } }

-------分割线------

新建一工程,在新的工程中取ActiveMq中消息

        导入Jar

            

        配置

                

<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:amq="http://activemq.apache.org/schema/core" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://activemq.apache.org/schema/core http://activemq.apache.org/schema/core/activemq-core-5.8.0.xsd" default-autowire="byName"> <!-- Activemq 连接工厂 --> <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <constructor-arg value="system1" /> <constructor-arg value="manager1" /> <constructor-arg value="failover:(tcp://localhost:61616)?timeout=2000" /> </bean> <!-- ConnectionFactory Definition --> <bean id="connectionFactory" class="org.springframework.jms.connection.CachingConnectionFactory"> <constructor-arg ref="activeMQConnectionFactory" /> </bean> <!-- 消息监听器,主要监听的目的地址 Message Receiver Definition --> <bean id="messageReceiver" class="com.yl.message.MessageReceiver"> </bean> <bean class="org.springframework.jms.listener.SimpleMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destinationName" value="test.activemq.queue" /> <property name="messageListener" ref="messageReceiver" /> </bean> </beans>

读取Mq消息: 

import java.util.UUID; import javax.annotation.Resource; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; import com.*.core.model.Car; import com.*.core.model.NowTrackCar; import com.*.core.model.TrackCar; import com.*.core.service.CarService; import com.*.core.service.NowTrackCarService; import com.*.core.service.TrackCarService; public class MessageReceiver implements MessageListener { @Override public void onMessage(Message message) { if (message instanceof TextMessage) { TextMessage textMessage = (TextMessage) message; try {//得到消息对列中的值 String content = textMessage.getText(); //对该消息进行处理 } catch (JMSException e) { e.printStackTrace(); } } } }

Maven中配置ActiveMq:

<!-- active mq --> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-pool</artifactId> <version>${activemq.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>${spring.version}</version> </dependency>这样就可以使用了。

转载请注明原文地址: https://www.6miu.com/read-2631249.html

最新回复(0)