activeMQ与spring+springMVC+activeMQ配置

    xiaoxiao2021-03-25  71

    最近在学习activeMQ相关内容 ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线。 1、activeMQ安装 windows下直接在activeMQ官网下载最新的安装包,解压即可 2、activeMQ运行 可以修改conf目录下的若干配置,更改activeMQ配置。可以在jetty-realm.properties中修改服务器的用户名(默认有admin与user两个用户)与密码,在jetty.xml修改管理activeMQ服务器的访问地址端口号(默认8161) 可以在activemq.xml修改使用者的端口号(默认61616)用户名与密码(注意此处的端口号和用户为直接使用消息队列的应用程序的配置与之前的登陆activeMQ后台的端口号与用户不同),注意此处修改用户名与密码须在标签里的标签前加入

    <!-- plugins is added by weigq .use for setting uid and pwd by msg users --> <plugins> <simpleAuthenticationPlugin> <users> <authenticationUser username="${activemq.username}" password="${activemq.password}" groups="users,admins"/> </users> </simpleAuthenticationPlugin> </plugins>

    用户名密码在credentials.properties 中修改 3、我这些都没有做改动,使用的是默认的配置,直接运行bin/win64目录下的activemq.bat文件即可开启消息总线服务器 4、默认情况下访问http://127.0.0.1:8161/admin/可以进入到后台管理界面(用户名与密码均为admin),可以查看目前系统中的queues和topics(Topic和queue的最大区别在于topic是以广播的形式,通知所有在线监听的客户端有新的消息,没有监听的客户端将收不到消息;而queue则是以点对点的形式通知多个处于监听状态的客户端中的一个。) 以上就完成了activeMQ的总线服务器配置,以下为在spring项目中使用这个总线消息服务器

    1、在pom文件中引入依赖:

    <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> <version>4.3.6.RELEASE</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.10.0</version> </dependency>

    关于activemq-all包的版本,我本地测试时,如果应用5.10之后的版本会导致c3p0的datasource注入老是报错,因此暂时使用的是5.10的版本,这个问题以后在研究 resource文件夹下新建文件JMSConfigure.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:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" 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.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jsm/spring-jms-4.3.xsd "> <bean id="connectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <!-- 定义消息队列(Queue(ActiveMQQueue)/Topic(ActiveMQTopic)) --> <bean id="dnkxQueueDestination" class="org.apache.activemq.command.ActiveMQTopic"> <!-- 设置消息队列的名字,如果总线中不存在这个,会自动新建一个这个名字的消息队列 --> <constructor-arg> <value>testtopic</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="dnkxQueueDestination" /> <property name="receiveTimeout" value="10000" /> <!-- true是topic,false是queue,默认是false,此处显示写出false --> <property name="pubSubDomain" value="true" /> </bean> <!-- 显示注入消息监听容器(Queue),配置连接工厂,监听的目标是dnkxQueueDestination,监听器是上面定义的监听器 --> <bean id="queueListenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="dnkxQueueDestination" /> <property name="messageListener" ref="messageReceiver" /> </bean> </beans>

    注意dnkxQueueDestination,此处使用的是topic如果要使用queue只需要将ActiveMQTopic换成ActiveMQQueue即可,在applicationContext.xml中引入这个配置文件

    <import resource="JMSConfigure.xml"/>

    在需要写入消息的程序中注入jmsTemplate即可,调用jmsTemplate.convertAndSend(text);方法即可写入一个String到消息队列 额外需要注意的是消息监听容器,注意注入变量messageReceiver,是我们自己实现的一个消息监听容器

    import javax.jms.Message; import javax.jms.MessageListener; import org.apache.log4j.Logger; import org.springframework.stereotype.Component; @Component public class MessageReceiver implements MessageListener { private static final Logger logger = Logger.getLogger(MessageReceiver.class); @Override public void onMessage(Message message) { try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } logger.info("打印消息内容:"+message.toString()); } }

    这个监听容器只是简单的打印出了消息对象 以上即可完成spring使用activeMQ的配置,(注意本例中我们在同一个工程中同时配置了消息写入与消息监听,在实际工程中这两项可能会分布在不同的工程中,此时只需要分开配置即可),我们可以同时部署多个工程,同时进行多个监听与写入消息

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

    最新回复(0)