如何使用activemq jms发送和接受消息
2014-09-17 10:02
537 查看
1.简介
jms即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。
Activemq 是apache对jms消息机制的实现。
2.下载安装运行
下载地址:http://activemq.apache.org/
运行:解压运行bin目录下的activemq.bat。登录http://localhost:8161/admin/ 查看是否运行成功。
3.创建测试项目
4.编写代码实现
发送端:
package com.ruanko.jms;
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 JMSProvider {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS标准接口 用它创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
// Connection :JMS 客户端到JMS Provider 的连接标准接口
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("RuankoQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
for (int i = 1; i <= 10; i++) {
TextMessage message = session.createTextMessage("发送消息" + i);
// 发送消息到目的地方
System.out.println("发送消息:" + i);
producer.send(message);
}
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != connection)
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
接收端:
package com.ruanko.jms;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSReceiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("RuankoQueue");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != connection)
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
5.运行结果
先运行JMSReceiver准备接受消息,然后运行JMSProvider发送消息,详细结果如图:
6.总结
JMS是很好的一个对系统解耦的中间件,在将来的系统开发中可以很好的减少系统之间的耦合,有利于系统将来的维护,和重构有很大的好处。
jms即Java消息服务(Java Message Service)应用程序接口是一个Java平台中关于面向消息中间件(MOM)的API,用于在两个应用程序之间,或分布式系统中发送消息,进行异步通信。Java消息服务是一个与具体平台无关的API,绝大多数MOM提供商都对JMS提供支持。
Activemq 是apache对jms消息机制的实现。
2.下载安装运行
下载地址:http://activemq.apache.org/
运行:解压运行bin目录下的activemq.bat。登录http://localhost:8161/admin/ 查看是否运行成功。
3.创建测试项目
4.编写代码实现
发送端:
package com.ruanko.jms;
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 JMSProvider {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS标准接口 用它创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
// Connection :JMS 客户端到JMS Provider 的连接标准接口
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// MessageProducer:消息发送者
MessageProducer producer;
// 构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.TRUE,
Session.AUTO_ACKNOWLEDGE);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("RuankoQueue");
// 得到消息生成者【发送者】
producer = session.createProducer(destination);
// 设置不持久化,此处学习,实际根据项目决定
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
// 构造消息,此处写死,项目就是参数,或者方法获取
for (int i = 1; i <= 10; i++) {
TextMessage message = session.createTextMessage("发送消息" + i);
// 发送消息到目的地方
System.out.println("发送消息:" + i);
producer.send(message);
}
session.commit();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != connection)
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
接收端:
package com.ruanko.jms;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;
public class JMSReceiver {
public static void main(String[] args) {
// ConnectionFactory :连接工厂,JMS 用它创建连接
ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
ActiveMQConnection.DEFAULT_USER,
ActiveMQConnection.DEFAULT_PASSWORD, "tcp://127.0.0.1:61616");
// Connection :JMS 客户端到JMS Provider 的连接
Connection connection = null;
// Session: 一个发送或接收消息的线程
Session session;
// Destination :消息的目的地;消息发送给谁.
Destination destination;
// 消费者,消息接收者
MessageConsumer consumer;
try {
// 构造从工厂得到连接对象
connection = connectionFactory.createConnection();
// 启动
connection.start();
// 获取操作连接
session = connection.createSession(Boolean.FALSE,
Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue("RuankoQueue");
consumer = session.createConsumer(destination);
while (true) {
TextMessage message = (TextMessage) consumer.receive(100000);
if (null != message) {
System.out.println("收到消息" + message.getText());
} else {
break;
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != connection)
try {
connection.close();
} catch (JMSException e) {
e.printStackTrace();
}
}
}
}
5.运行结果
先运行JMSReceiver准备接受消息,然后运行JMSProvider发送消息,详细结果如图:
6.总结
JMS是很好的一个对系统解耦的中间件,在将来的系统开发中可以很好的减少系统之间的耦合,有利于系统将来的维护,和重构有很大的好处。
相关文章推荐
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ (二) 使用Queue或者Topic发送/接受消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- spring-jms(activemq实现)使用queue发送消息简单例子
- ActiveMQ使用spring JmsTemplate发送消息(一)
- 使用ActiveMQ发送和接受消息(1)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- activemq使用JMS发送消息和接收消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ (二) 使用Queue或者Topic发送/接受消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- JMS_使用ActiveMQ实现消息的发送和接收
- ActiveMQ使用spring JmsTemplate发送消息(一)
- weblogic中使用jms发送和接受消息
- ActiveMQ使用spring JmsTemplate发送消息(一)
- ActiveMQ使用spring JmsTemplate发送消息(一)