activemq安装使用教程
一、下载安装
下载地址:http://activemq.apache.org/activemq-5158-release.html
然后解压即可,apache的东西解压后就可以使用了。
二、启动
在安装目录的bin目录下:
activemq start
就可以启动了。
访问localhost:8161就可以访问
如果你想修改用户名和密码的话,在conf/jetty-realm.properties中修改即可。
然后重启就可以。
三、demo
启动activemq 中的 demo
activemq start xbean:file:../examples/conf/activemq-demo.xml
访问:
http://localhost:8161/demo/
然后就可以收发消息了。
四、自己使用
介绍web项目中spring集成activemq
maven依赖:
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> <version>5.15.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-spring</artifactId> <version>5.15.0</version> </dependency>
applicationContext.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.xsd"> <!--设置注解类所在的jar包--> <context:component-scan base-package="com.linewell" /> <!-- 连接池 --> <bean id="pooledConnectionFactory" class="org.apache.activemq.pool.PooledConnectionFactory" destroy-method="stop"> <property name="connectionFactory"> <bean class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> </property> </bean> <!-- 连接工厂 --> <bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://localhost:61616"/> </bean> <!-- 配置消息目标 --> <bean id="destination" class="org.apache.activemq.command.ActiveMQQueue"> <!-- 目标,在ActiveMQ管理员控制台创建 http://localhost:8161/admin/queues.jsp --> <constructor-arg index="0" value="sagedragon.mq.queue"/> </bean> <!-- 消息模板 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <property name="connectionFactory" ref="activeMQConnectionFactory"/> <property name="defaultDestination" ref="destination"/> <property name="messageConverter"> <bean class="org.springframework.jms.support.converter.SimpleMessageConverter"/> </property> </bean> </beans>
web.xml配置
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd" > <web-app> <display-name>Archetype Created Web Application</display-name> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath*:applicationContext.xml</param-value> </context-param> <filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/</url-pattern> </filter-mapping> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>MqtestServlet</servlet-name> <servlet-class>com.linewell.servlets.MqtestServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>MqtestServlet</servlet-name> <url-pattern>/activemq/MqtestServlet</url-pattern> </servlet-mapping> </web-app>
发送者:
package com.linewell.activemq; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Service; import javax.jms.JMSException; import javax.jms.MapMessage; import javax.jms.Message; import javax.jms.Session; import java.util.Date; import javax.annotation.Resource; @Service public class SpringSender { @Resource private JmsTemplate jmsTemplate; public void sendMsg() { jmsTemplate.send(new MessageCreator() { public Message createMessage(Session session) throws JMSException { MapMessage message = session.createMapMessage(); System.out.println("开始发送消息..."); message.setString("message", "current system time: " + new Date().getTime()); return message; } }); } }
接收者:
package com.linewell.activemq; import org.springframework.context.ApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.jms.core.JmsTemplate; import org.springframework.stereotype.Service; import javax.annotation.Resource; import java.util.Map; @Service public class SpringReceiver { @Resource private JmsTemplate jmsTemplate; public void receiveMsg() { while (true) { Map<String, Object> map = (Map<String, Object>) jmsTemplate.receiveAndConvert(); System.out.println("收到消息:" + map.get("message")); } } }