ActiveMQ使用笔记
来源:互联网 发布:阴阳家和道家知乎 编辑:程序博客网 时间:2024/05/03 13:05
http://hbluojiahui.blog.163.com/blog/static/3106476720094634655199/ 转载地址
1.下载ActiveMQ5.1,官方网站地址:http://activemq.apache.org/
2.解压,双击bin目录下activemq.bat
管理页面:http://localhost:8161/admin
3.创建queue,这里创建一个名为queue1的queue,供测试用
4.Queue发送
package demo.activemq;
imp
imp
imp
imp
imp
imp
imp
imp
imp
public class QueueSend {
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);
// queue1需要在admin界面创建
destination = session.createQueue("queue1");
// 得到消息生成者
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("发送消息:" + i+"成功");
producer.send(message);
}
}
}
执行之后
4.Queue接收消息
package demo.activemq;
imp
imp
imp
imp
imp
imp
imp
imp
public class QueueReceive{
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注意参数是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("queue1");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(1000);
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) {
}
}
}
}
需要的包:active-core_5.1.0.jar,commons-logging.jar,geronimo-j2ee-management_1.0_spec-1.0.jar,jms.jar
- ActiveMQ使用笔记
- ActiveMQ使用笔记
- ActiveMQ使用笔记
- ActiveMQ使用笔记
- ActiveMQ使用笔记-1
- ActiveMQ使用笔记(七)ActiveMQ性能优化
- ActiveMQ使用笔记(六)ActiveMQ主备配置
- ActiveMQ使用笔记(五)ActiveMQ负载均衡
- ActiveMQ使用笔记(四)ActiveMQ配置安全性
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- ActiveMQ使用笔记(二)ActiveMQ消息持久化(3)
- ActiveMQ使用笔记(二)ActiveMQ消息持久化(2)
- ActiveMQ使用笔记(二)ActiveMQ消息持久化(1)
- ActiveMQ使用笔记(一)ActiveMQ的安装
- ActiveMQ使用笔记(二)ActiveMQ消息持久化一
- ActiveMQ使用笔记(三)ActiveMQ消息发送与接收
- ActiveMQ使用笔记(七)ActiveMQ性能优化
- Activemq学习笔记---Topic及JNDI使用
- MFC位图加载
- 配置shutdown.sh -s jnp://localhost:1299关闭指定jboss
- hive 创建表和导入数据
- stagefright 架构分析(二) TimedEventQueue
- ftp 命令
- ActiveMQ使用笔记
- ubuntu 12.04 解决txt文件中中文乱码问题
- MFC消息反射机制
- 【iOS-cocos2d-X 游戏开发之四】Cocos2dX创建Android NDK新项目并编译导入Eclipse中正常运行!
- C# C/S模式下实现软件自动在线升级
- Fedora17环境无视频编译linphone3.5.2
- Eclipse快捷键
- JAVA 输出javabean属性方法 详解 利用common包~~~important
- 删除重复数据的触发器.sql