须要jdk,安装Linux系统,生产环境都是Linux系统。
安装步骤:
第一步: 把ActiveMQ 的压缩包上传到Linux系统。
第二步:解压缩。
第三步:启动。
使用bin目录下的activemq命令启动:
[root@localhost bin]# ./activemq start
关闭:
[root@localhost bin]# ./activemq stop
查看状态:
[root@localhost bin]# ./activemq status
注意:若是ActiveMQ整合spring使用不要使用activemq-all-5.12.0.jar包。建议使用5.11.2
进入管理后台:
http://192.168.25.143:8161/admin
用户名:admin
密码:admin
spring![]()
对于消息的传递有两种类型:apache
一种是点对点的,即一个生产者和一个消费者一一对应;另外一种是发布/订阅模式,即一个生产者产生消息并进行发送后,能够由多个消费者进行接收。
JMS定义了五种不一样的消息正文格式,以及调用的消息类型,容许你发送并接收以一些不一样形式的数据,提供现有消息格式的一些级别的兼容性。服务器
广播通讯模式session
点到点通讯app
依赖的jar包tcp
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-all</artifactId> </dependency>
Producer:消息的发送方
第一步:建立ConnectionFactory对象,须要指定服务端ip及端口号。
第二步:使用ConnectionFactory对象建立一个Connection对象。
第三步:开启链接,调用Connection对象的start方法。
第四步:使用Connection对象建立一个Session对象。
第五步:使用Session对象建立一个Destination对象(topic、queue),此处建立一个Queue对象。
第六步:使用Session对象建立一个Producer对象。
第七步:建立一个Message对象,建立一个TextMessage对象。
第八步:使用Producer对象发送消息。
第九步:关闭资源。ide
public class ActiveMqTest { /** * 点到点形式发送消息 */ @Test public void testQueueProducer() throws Exception { //一、建立一个链接工厂对象,须要指定服务的ip及端口。 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.144:61616"); //二、使用工厂对象建立一个Connection对象。 Connection connection = connectionFactory.createConnection(); //三、开启链接,调用Connection对象的start方法。 connection.start(); //四、建立一个Session对象。 //第一个参数:是否开启事务。若是true开启事务,第二个参数无心义。通常不开启事务false。 //第二个参数:应答模式。自动应答或者手动应答。通常自动应答。 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //五、使用Session对象建立一个Destination对象。两种形式queue、topic,如今应该使用queue Queue queue = session.createQueue("test-queue"); //六、使用Session对象建立一个Producer对象。 MessageProducer producer = session.createProducer(queue); //七、建立一个Message对象,能够使用TextMessage。 /*TextMessage textMessage = new ActiveMQTextMessage(); textMessage.setText("hello Activemq");*/ TextMessage textMessage = session.createTextMessage("hello activemq"); //八、发送消息 producer.send(textMessage); //九、关闭资源 producer.close(); session.close(); connection.close(); }
Consumer:消息的接收方
第一步:建立一个ConnectionFactory对象。
第二步:从ConnectionFactory对象中得到一个Connection对象。
第三步:开启链接。调用Connection对象的start方法。
第四步:使用Connection对象建立一个Session对象。
第五步:使用Session对象建立一个Destination对象。和发送端保持一致queue,而且队列的名称一致。
第六步:使用Session对象建立一个Consumer对象。
第七步:接收消息。
第八步:打印消息。
第九步:关闭资源工具
@Test public void testQueueConsumer() throws Exception { //建立一个ConnectionFactory对象链接MQ服务器 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.144:61616"); //建立一个链接对象 Connection connection = connectionFactory.createConnection(); //开启链接 connection.start(); //使用Connection对象建立一个Session对象 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //建立一个Destination对象。queue对象 Queue queue = session.createQueue("test-queue"); //使用Session对象建立一个消费者对象。 MessageConsumer consumer = session.createConsumer(queue); //接收消息 consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { //打印结果 TextMessage textMessage = (TextMessage) message; String text; try { text = textMessage.getText(); System.out.println(text); } catch (JMSException e) { e.printStackTrace(); } } }); //等待接收消息 System.in.read(); //关闭资源 consumer.close(); session.close(); connection.close(); }
Topic方式的广播通讯测试性能
Producer:消息的发送方
第一步:建立ConnectionFactory对象,须要指定服务端ip及端口号。
第二步:使用ConnectionFactory对象建立一个Connection对象。
第三步:开启链接,调用Connection对象的start方法。
第四步:使用Connection对象建立一个Session对象。
第五步:使用Session对象建立一个Destination对象(topic、queue),此处建立一个Topic对象。
第六步:使用Session对象建立一个Producer对象。
第七步:建立一个Message对象,建立一个TextMessage对象。
第八步:使用Producer对象发送消息。
第九步:关闭资源。测试
@Test public void testTopicProducer() throws Exception { //一、建立一个链接工厂对象,须要指定服务的ip及端口。 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.144:61616"); //二、使用工厂对象建立一个Connection对象。 Connection connection = connectionFactory.createConnection(); //三、开启链接,调用Connection对象的start方法。 connection.start(); //四、建立一个Session对象。 //第一个参数:是否开启事务。若是true开启事务,第二个参数无心义。通常不开启事务false。 //第二个参数:应答模式。自动应答或者手动应答。通常自动应答。 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //五、使用Session对象建立一个Destination对象。两种形式queue、topic,如今应该使用topic Topic topic = session.createTopic("test-topic"); //六、使用Session对象建立一个Producer对象。 MessageProducer producer = session.createProducer(topic); //七、建立一个Message对象,能够使用TextMessage。 /*TextMessage textMessage = new ActiveMQTextMessage(); textMessage.setText("hello Activemq");*/ TextMessage textMessage = session.createTextMessage("topic message"); //八、发送消息 producer.send(textMessage); //九、关闭资源 producer.close(); session.close(); connection.close(); }
Consumer:消息的接收方
第一步:建立一个ConnectionFactory对象。
第二步:从ConnectionFactory对象中得到一个Connection对象。
第三步:开启链接。调用Connection对象的start方法。
第四步:使用Connection对象建立一个Session对象。
第五步:使用Session对象建立一个Destination对象。和发送端保持一致topic,而且话题的名称一致。
第六步:使用Session对象建立一个Consumer对象。
第七步:接收消息。
第八步:打印消息。
第九步:关闭资源。
@Test public void testTopicConsumer() throws Exception { //建立一个ConnectionFactory对象链接MQ服务器 ConnectionFactory connectionFactory = new ActiveMQConnectionFactory("tcp://192.168.25.144:61616"); //建立一个链接对象 Connection connection = connectionFactory.createConnection(); //开启链接 connection.start(); //使用Connection对象建立一个Session对象 Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE); //建立一个Destination对象。topic对象 Topic topic = session.createTopic("test-topic"); //使用Session对象建立一个消费者对象。 MessageConsumer consumer = session.createConsumer(topic); //接收消息 consumer.setMessageListener(new MessageListener() { @Override public void onMessage(Message message) { //打印结果 TextMessage textMessage = (TextMessage) message; String text; try { text = textMessage.getText(); System.out.println(text); } catch (JMSException e) { e.printStackTrace(); } } }); System.out.println("topic消费者3启动。。。。"); //等待接收消息 System.in.read(); //关闭资源 consumer.close(); session.close(); connection.close(); }
第一步:引用相关的jar包
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-jms</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context-support</artifactId> </dependency>
第二步:配置Activemq整合spring。配置ConnectionFactory
<!-- 真正能够产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.168:61616" /> </bean> <!-- Spring用于管理真正的ConnectionFactory的ConnectionFactory --> <bean id="connectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory"> <!-- 目标ConnectionFactory对应真实的能够产生JMS Connection的ConnectionFactory --> <property name="targetConnectionFactory" ref="targetConnectionFactory" /> </bean> </beans>
第三步:配置生产者
使用JMSTemplate对象发送消息
<!-- 配置生产者 --> <!-- Spring提供的JMS工具类,它能够进行消息发送、接收等 --> <bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate"> <!-- 这个connectionFactory对应的是咱们定义的Spring提供的那个ConnectionFactory对象 --> <property name="connectionFactory" ref="connectionFactory" /> </bean>
第四步:在spring容器中配置Destination
<!--这个是队列目的地,点对点的 --> <bean id="queueDestination" class="org.apache.activemq.command.ActiveMQQueue"> <constructor-arg> <value>spring-queue</value> </constructor-arg> </bean> <!--这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic" /> </bean>
第五步:代码测试
①:初始化一个spring容器
②:从容器中得到JMSTemplate对象。
③:从容器中得到一个Destination对象
④:使用JMSTemplate对象发送消息,须要知道Destination
@Test public void testSpringActiveMq() throws Exception { //初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); //从spring容器中得到JmsTemplate对象 JmsTemplate jmsTemplate = applicationContext.getBean(JmsTemplate.class); //从spring容器中取Destination对象 Destination destination = (Destination) applicationContext.getBean("queueDestination"); //使用JmsTemplate对象发送消息。 jmsTemplate.send(destination, new MessageCreator() { @Override public Message createMessage(Session session) throws JMSException { //建立一个消息对象并返回 TextMessage textMessage = session.createTextMessage("spring activemq queue message"); return textMessage; } }); }
接收消息
在另外一个工程中建立接收消息
第一步:把Activemq相关的jar包添加到工程中
第二步:建立一个MessageListener的实现类
public class MyMessageListener implements MessageListener { @Override public void onMessage(Message message) { try { TextMessage textMessage = (TextMessage) message; //取消息内容 String text = textMessage.getText(); System.out.println(text); } catch (JMSException e) { e.printStackTrace(); } } }
第三步:配置spring和Activemq整合
<!-- 真正能够产生Connection的ConnectionFactory,由对应的 JMS服务厂商提供 --> <bean id="targetConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory"> <property name="brokerURL" value="tcp://192.168.25.144: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>spring-queue</value> </constructor-arg> </bean> <!--这个是主题目的地,一对多的 --> <bean id="topicDestination" class="org.apache.activemq.command.ActiveMQTopic"> <constructor-arg value="topic" /> </bean> <!-- 接收消息 --> <!-- 配置监听器 --> <bean id="myMessageListener" class="cn.e3mall.search.listener.MyMessageListener" /> <!-- 消息监听容器 --> <bean class="org.springframework.jms.listener.DefaultMessageListenerContainer"> <property name="connectionFactory" ref="connectionFactory" /> <property name="destination" ref="queueDestination" /> <property name="messageListener" ref="myMessageListener" /> </bean>
第四步:测试代码
@Test public void testQueueConsumer() throws Exception { //初始化spring容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring/applicationContext-activemq.xml"); //等待 System.in.read(); }
原文地址:https://www.jianshu.com/p/38a67c47b097