博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
ActiveMQ入门
阅读量:4188 次
发布时间:2019-05-26

本文共 4435 字,大约阅读时间需要 14 分钟。

1.下载ActiveMQ
去官方网站下载:http://activemq.apache.org/
2.运行ActiveMQ
将apache-activemq-5.5.1-bin.zip解压缩,双击apache-activemq-5.5.1\bin\activemq.bat运行ActiveMQ程序。
启动ActiveMQ以后,登陆:http://localhost:8161/admin/,创建一个Queue,命名为FirstQueue。
3.导入jar包:
4.实例
1、发送端
Java代码 收藏代码
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.JMSException;
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 {
/** 创建Session */
public static Session createSession() {
// 连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory = null;
// MS 客户端到JMS Provider 的连接
Connection connection = null;
// 一个发送或接收消息的会话
Session session = null;
try {
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// 通过连接工厂创建连接
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建Session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
return session;
}
/** 创建消息发送者 */
public static MessageProducer createProducer(Session session) {
// 消息的目的地;消息发送给谁.
Destination destination = null;
// 消息发送者
MessageProducer producer = null;
try {
// 获取session注意参数值FirstQueue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 消息--发送者
producer = session.createProducer(destination);
// 设置不持久化,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
} catch (Exception e) {
e.printStackTrace();
}
return producer;
}
/** 发送消息 */
public static void sendMessage(Session session, MessageProducer producer,
String content) {
TextMessage textMessage = null;
try {
textMessage = session.createTextMessage(content);
producer.send(textMessage);
session.commit();// 提交
} catch (JMSException e) {
e.printStackTrace();
}
}
/** 测试 */
public static void main(String[] args) {
Session session = createSession();
MessageProducer producer = createProducer(session);
for (int i = 0; i < 5; i++) {
String content = "Message---" + i;
System.out.println(content);
sendMessage(session, producer, content);
}
}
}
2、接收端
Java代码 收藏代码
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 {
/** 创建Session */
public static Session createSession() {
// 连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory = null;
// MS 客户端到JMS Provider 的连接
Connection connection = null;
// 一个发送或接收消息的会话
Session session = null;
try {
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD,
"tcp://localhost:61616");
// 通过连接工厂创建连接
connection = connectionFactory.createConnection();
// 启动连接
connection.start();
// 创建Session
session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
} catch (Exception e) {
e.printStackTrace();
}
return session;
}
/** 创建消息接收者 */
public static MessageConsumer createConsumer(Session session) {
// 消息的目的地;消息发送给谁.
Destination destination = null;
// 消息接收者
MessageConsumer consumer = null;
try {
// 获取session注意参数值FirstQueue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 得到消息生成者"发送者"
consumer = session.createConsumer(destination);
} catch (Exception e) {
e.printStackTrace();
}
return consumer;
}
/** 发送消息 */
public static void receiverMessage(MessageConsumer consumer) {
try {
while (true) {
// 设置接收者接收消息的时间
TextMessage message = (TextMessage) consumer.receive(6 * 1000);
if (null != message) {
System.out.println("ReceiverMessage--" + message.getText());
} else {
System.out.println("break");
break;
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/** 测试 */
public static void main(String[] args) {
receiverMessage(createConsumer(createSession()));
}
}
5、测试结果
执行Sender:
Message---0
Message---1
Message---2
Message---3
Message---4
执行Receive:
ReceiverMessage--Message---0
ReceiverMessage--Message---1
ReceiverMessage--Message---2
ReceiverMessage--Message---3
ReceiverMessage--Message---4

转载地址:http://yznoi.baihongyu.com/

你可能感兴趣的文章
近日計劃
查看>>
.NET Framework Security Code Access Security-應用程式篇
查看>>
Designing and Implement Lookup Control for Windows Forms
查看>>
Designing and Implement ButtonEdit Control for Windows Forms
查看>>
如果你是程序员你或许不须以撰写组件维生,但我确信你必定得依赖组件维生!
查看>>
像Component的Control
查看>>
Inside ASP.NET 2.0-即时编译系统
查看>>
修復VS.NET 2005
查看>>
LINQ
查看>>
JavaScript组件之JQuery(A~Z)教程(基于Asp.net运行环境)[示例代码下载]
查看>>
学英语的网站[转载]
查看>>
Asp.net 2.0 自定义控件开发[开发一个图表(WebChart)控件(柱状图示例)](示例代码下载)
查看>>
住在我隔壁储藏室的大学刚毕业的小夫妻[转载]
查看>>
英语学习加油站[收集]
查看>>
Asp.net 2.0 自定义控件开发专题讲解[为用户控件增加DataSource属性, 能够自动识别不同数据源](示例代码下载)
查看>>
2007.5.13 祝天下所有的母亲: 节日快乐!
查看>>
Asp.net 2.0自定义控件开发相关的几种嵌入资源解决方案
查看>>
Asp.net 2.0控件开发相关调试(JavaScript调试和自定义控件设计时调试)
查看>>
Asp.net 2.0 自定义控件开发专题[详细探讨页面状态(视图状态和控件状态)机制及其使用场景](示例代码下载)
查看>>
Asp.net 2.0自定义控件(点击HyperLink后执行事件)[网友问题: DataList里HyperLink控件激发事件,在哪定义?]
查看>>