ActiveMq5.10 maven入门实例
来源:互联网 发布:金粉世家知乎 编辑:程序博客网 时间:2024/06/03 13:42
1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
我的电脑环境是win8系统,我这里下载的是apache-activemq-5.10.0-bin.zip
2.运行ActiveMQ
解压缩apache-activemq-5.10.0-bin.zip,然后双击apache-activemq-5.10.0-bin.zip\bin\win64\activemq.bat运行ActiveMQ程序。选择activemq应用程序启动:
启动ActiveMQ以后,在cmd查看端口,如果有端口表示启动成功
登陆:http://localhost:8161/admin/,输入用户名:admin,密码:admin 进入系统
3.创建Eclipse项目并运行
创建project:system-common-activemq,并通过maven3导入apache-activemq-5.10.0-all jar,项目结构如下图所示:
pom.xml (我这里使用了父级继承POM,你只需要按照普通的pom加上dependencies导入jar的gav就可以)如下:
<?xml version="1.0"?><project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><parent><groupId>com.system</groupId><artifactId>system-common</artifactId><version>0.0.1-SNAPSHOT</version></parent><artifactId>system-common-activemq</artifactId><name>system-common-activemq</name><url>http://maven.apache.org</url><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><scope>test</scope></dependency><dependency><groupId>org.apache.activemq</groupId><artifactId>activemq-all</artifactId></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-jms</artifactId></dependency></dependencies></project>
3.1.Sender.java
package com.system.common.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.DeliveryMode;import javax.jms.Destination;import javax.jms.MessageProducer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Sender { private static final int SEND_NUMBER = 5; public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // MessageProducer:消息发送者 MessageProducer producer; // TextMessage message; // 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.TRUE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); // 得到消息生成者【发送者】 producer = session.createProducer(destination); // 设置不持久化,此处学习,实际根据项目决定 producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); // 构造消息,此处写死,项目就是参数,或者方法获取 sendMessage(session, producer); session.commit(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } } public static void sendMessage(Session session, MessageProducer producer) throws Exception { for (int i = 1; i <= SEND_NUMBER; i++) { TextMessage message = session .createTextMessage("ActiveMq 发送的消息" + i); // 发送消息到目的地方 System.out.println("发送消息:" + "ActiveMq 发送的消息" + i); producer.send(message); } }}
3.2.Receiver.java
package com.xuwei.activemq;import javax.jms.Connection;import javax.jms.ConnectionFactory;import javax.jms.Destination;import javax.jms.MessageConsumer;import javax.jms.Session;import javax.jms.TextMessage;import org.apache.activemq.ActiveMQConnection;import org.apache.activemq.ActiveMQConnectionFactory;public class Receiver { public static void main(String[] args) { // ConnectionFactory :连接工厂,JMS 用它创建连接 ConnectionFactory connectionFactory; // Connection :JMS 客户端到JMS Provider 的连接 Connection connection = null; // Session: 一个发送或接收消息的线程 Session session; // Destination :消息的目的地;消息发送给谁. Destination destination; // 消费者,消息接收者 MessageConsumer consumer; connectionFactory = new ActiveMQConnectionFactory( ActiveMQConnection.DEFAULT_USER, ActiveMQConnection.DEFAULT_PASSWORD, "tcp://localhost:61616"); try { // 构造从工厂得到连接对象 connection = connectionFactory.createConnection(); // 启动 connection.start(); // 获取操作连接 session = connection.createSession(Boolean.FALSE, Session.AUTO_ACKNOWLEDGE); // 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 destination = session.createQueue("FirstQueue"); consumer = session.createConsumer(destination); while (true) { //设置接收者接收消息的时间,为了便于测试,这里谁定为100s TextMessage message = (TextMessage) consumer.receive(100000); if (null != message) { System.out.println("收到消息" + message.getText()); } else { break; } } } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != connection) connection.close(); } catch (Throwable ignore) { } } }}
4.测试过程
因为是在单机上测试,所以需要开启两个eclipse,每一个eclipse都有自身的workspace。我们在eclipse1中运行Receiver,在eclipse2中运行Sender。
刚开始eclipse1中运行Receiver以后console介面没有任何信息,在eclipse2中运行Sender以后,eclipse2中的console显示如下信息:
发送消息:ActiveMq 发送的消息1
发送消息:ActiveMq 发送的消息2
发送消息:ActiveMq 发送的消息3
发送消息:ActiveMq 发送的消息4
发送消息:ActiveMq 发送的消息5而回到eclipse1中发现console界面出现如下信息:
收到消息ActiveMq 发送的消息1
收到消息ActiveMq 发送的消息2
收到消息ActiveMq 发送的消息3
收到消息ActiveMq 发送的消息4
收到消息ActiveMq 发送的消息5
PS:2012-2-27
今天发现测试并不需要开启两个eclipse,在一个eclipse下页可以启动多个程序,并且有多个console,在上面的Receiver.java中,设置一个较大的时间,比如receive(500000),如下代码所示:
TextMessage message = (TextMessage) consumer.receive(500000);
这个时候运行Receiver.java的话,会使得这个Receiver.java一直运行500秒,在eclipse中可以发现:
点击那个红色方块可以手动停止运行程序。
运行玩receiver以后我们在运行sender,在运行完sender以后,我们要切换到receiver的console,如下图所示:
- ActiveMq5.10 maven入门实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- Maven入门--概念与实例
- android DownloadManager广播事件:下载完成、通知栏点击事件
- undefined reference问题总结
- hdu 3376 Matrix Again【最大费用流】
- 利用UIScrollView实现图片的缩放
- Two Sum,3Sum,3Sum Closest,4Sum
- ActiveMq5.10 maven入门实例
- 深夜的独白
- sde最大连接数调整
- dsp EMIF接口
- Jquery 常用用法
- 正则总结
- linux下每天定时备份mongo
- android点击按钮调转到指定Fragment并指定activity方法
- word中如何批量修改替换超链接路径地址?