Hello Activemq
0. 如果永远是localhost 可能一直low下去
1.下载安装 activemq
1.1 从官网下载activemq.tar.gz 并上传(rz)到linux系统 并解压 tar zxvf /*/activemq.tar.gz , activemq依赖jdk maven
1.2 进入activemq_home\bin 目录 ./activemq start 启动activemq, 验证ps -ef|grep activemq
http://localhost:8161/admin admin/admin 进入配置页
1.3 todo: jdk activemq 怎么直接在linux下载, wget curl之类命令对应的url不知道
2. 创建maven工程
2.1 pom.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.rocky.demo</groupId> <artifactId>activemaStar</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>activemaStar</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <spring-version>4.3.10.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</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-jms</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>javax.annotation</groupId> <artifactId>jsr250-api</artifactId> <version>1.0</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-core</artifactId> <version>5.7.0</version> </dependency> </dependencies> </project>
2.2 spring配置文件(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" xmlns:jms="http://www.springframework.org/schema/jms" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/jms http://www.springframework.org/schema/jms/spring-jms-3.0.xsd"> <context:component-scan base-package="com.rocky" /> <bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate"> <property name="environment"> <props> <prop key="java.naming.factory.initial"> org.apache.activemq.jndi.ActiveMQInitialContextFactory </prop> <prop key="brokerURL">tcp://192.168.*.*:61616</prop> <prop key="connectionFactoryNames">ConnectionFactory</prop> </props> </property> </bean> <bean id="connectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="ConnectionFactory" /> <property name="jndiTemplate" ref="jndiTemplate" /> </bean> <!-- Spring提供的JMS工具类,它可以进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是我们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory"/> </bean> <!-- 真正可以产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供--> <!-- <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.*.8:61616"/> </bean> --> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <!-- <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> 目标ConnectionFactory对应真实的可以产生JMS Connection的ConnectionFactory <property name="targetConnectionFactory" ref="targetConnectionFactory"/> </bean> --> <!--这个是队列目的地--> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>queue</value> </constructor-arg> </bean> <!-- 消息监听器 --> <bean id="consumerMessageListener" class="com.rocky.demo.activemaStar.ConsumerMessageListener"/> <!-- 消息监听容器 --> <bean id="jmsContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="consumerMessageListener" /> </bean> </beans>
2.3 生产者接口(ProducerService)
package com.rocky.demo.activemaStar; import javax.jms.Destination; public interface ProducerService { public void sendMessage(Destination destination, final String message); }
及实现类(ProducerServiceImpl)
package com.rocky.demo.activemaStar; import javax.annotation.Resource; import javax.jms.Destination; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.Session; import org.springframework.jms.core.JmsTemplate; import org.springframework.jms.core.MessageCreator; import org.springframework.stereotype.Component; @Component public class ProducerServiceImpl implements ProducerService { private JmsTemplate jmsTemplate; public void sendMessage(Destination destination, final String message) { System.out.println("---------------生产者发送消息-----------------"); System.out.println("---------------生产者发了一个消息:" + message); jmsTemplate.send(destination, new MessageCreator() { public Message createMessage(Session session) throws JMSException { return session.createTextMessage(message); } }); } public JmsTemplate getJmsTemplate() { return jmsTemplate; } @Resource public void setJmsTemplate(JmsTemplate jmsTemplate) { this.jmsTemplate = jmsTemplate; } }
2.4 消费者监听
package com.rocky.demo.activemaStar; import javax.jms.JMSException; import javax.jms.Message; import javax.jms.MessageListener; import javax.jms.TextMessage; public class ConsumerMessageListener implements MessageListener { public void onMessage(Message message) { //这里我们知道生产者发送的就是一个纯文本消息,所以这里可以直接进行强制转换 TextMessage textMsg = (TextMessage) message; System.out.println("接收到一个纯文本消息。"); try { System.out.println("消息内容是:" + textMsg.getText()); } catch (JMSException e) { e.printStackTrace(); } } }
2.5 junit测试类
package com.rocky.demo.activemaStar; import javax.jms.Destination; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.rocky.demo.activemaStar.ProducerService; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration("/applicationContext.xml") public class ProducerConsumerTest { @Autowired private ProducerService producerService; @Autowired @Qualifier("queueDestination") private Destination destination; @Test public void testSend() { for (int i=0; i<2; i++) { producerService.sendMessage(destination, "你好,生产者!这是消息:" + (i+1)); } } }
2.6 运行控制台
---------------生产者发送消息-----------------
---------------生产者发了一个消息:你好,生产者!这是消息:1
接收到一个纯文本消息。
消息内容是:你好,生产者!这是消息:1
---------------生产者发送消息-----------------
---------------生产者发了一个消息:你好,生产者!这是消息:2
接收到一个纯文本消息。
消息内容是:你好,生产者!这是消息:2
2.7 回头看 localhost怎么了
上面的程序在localhost环境下是ok的 把activemq部署到另外一台机器 控制台没反应 ?
最后反应过来 是防火墙!!!
firewall-cmd --add-port=8161/tcp
firewall-cmd --add-port=61616/tcp 这是临时生效
firewall-cmd --permanent --add-port=61616/tcp写入配置文件
firewall-cmd --reload 重启防火墙
这样子 hello activemq才好了。