使用activeMQ实现jms
来源:互联网 发布:94版三国演义 知乎 编辑:程序博客网 时间:2024/05/22 06:13
使用activeMQ实现jms
(2010-07-14 22:56:00)杂谈
分类: java高级应用一:jms介绍
二: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.ActiveMQConnectionFactor y; -
- public
class Sender { -
static final int SEND_NUMBER 5;= -
-
static void main(String[] args) { -
ConnectionFactory :连接工厂,JMS 用它创建连接 -
ConnectionFactory connectionFactory; -
Connection :JMS 客户端到JMS Provider 的连接 -
Connection connection = -
Session: 一个发送或接收消息的线程 -
Session session; -
Destination :消息的目的地;消息发送给谁. -
Destination destination; -
MessageProducer:消息发送者 -
MessageProducer producer; -
TextMessage message; -
构造ConnectionFactory实例对象,此处采用ActiveMq的实现jar -
-
connectionFactory = ActiveMQConnectionFactor y( -
ActiveMQConnection.DEFAULT_USER, -
ActiveMQConnection.DEFAULT_PASSWORD, -
-
{ -
构造从工厂得到连接对象 -
connection = connectionFactory.createConnection(); -
启动 -
connection.start(); -
获取操作连接 -
session = connection.createSession(Boolean.TRUE, -
Session.AUTO_ACKNOWLEDGE); -
获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置 -
destination = session.createQueue( -
得到消息生成者【发送者】 -
producer = session.createProducer(destination); -
设置不持久化,可以更改 -
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT); -
构造消息 -
sendMessage(session, producer); -
session.commit(); -
-
} (Exception e) { -
e.printStackTrace(); -
} { -
{ -
( null!= connection) -
connection.close(); -
} (Throwable ignore) { -
} -
} -
-
} -
static void sendMessage(Session session, MessageProducer producer) -
Exception { -
( inti 1;= i <=SEND_NUMBER; i++) { -
TextMessage message = session -
.createTextMessage( 发送的消息" + i); -
发送消息到目的地方 -
System.out.println( + i); -
producer.send(message); -
} -
} - }
-
-
-
-
-
-
- import
javax.jms.Connection; - import
javax.jms.ConnectionFactory; - import
javax.jms.Destination; - import
javax.jms.JMSException; - import
javax.jms.Message; - import
javax.jms.MessageConsumer; - import
javax.jms.MessageListener; - import
javax.jms.Session; - import
javax.jms.TextMessage; -
- import
org.apache.activemq.ActiveMQConnection; - import
org.apache.activemq.ActiveMQConnectionFactor y; -
- public
class Receiver { -
static void main(String[] args) { -
-
ConnectionFactory :连接工厂,JMS 用它创建连接 -
ConnectionFactory connectionFactory; -
Connection :JMS 客户端到JMS Provider 的连接 -
Connection connection = -
Session: 一个发送或接收消息的线程 -
Session session; -
Destination :消息的目的地;消息发送给谁. -
Destination destination; -
消费者,消息接收者 -
MessageConsumer consumer; -
-
connectionFactory = ActiveMQConnectionFactor y( -
ActiveMQConnection.DEFAULT_USER, -
ActiveMQConnection.DEFAULT_PASSWORD, -
{ -
构造从工厂得到连接对象 -
connection = connectionFactory.createConnection(); -
启动 -
connection.start(); -
获取操作连接 -
session = connection.createSession(Boolean.FALSE, -
Session.AUTO_ACKNOWLEDGE); -
-
destination = session.createQueue( -
consumer = session.createConsumer(destination); -
consumer.setMessageListener( MessageListener() { -
void onMessage(Message arg0) { -
System.out.println( -
{ -
System.out.println( -
+ ((TextMessage) arg0).getText()); -
} (JMSException e) { -
TODO Auto-generated catch block -
e.printStackTrace(); -
} -
-
} -
}); -
-
MessageConsumer consumer1 = session.createConsumer(destination); -
consumer1.setMessageListener( MessageListener() { -
void onMessage(Message arg0) { -
System.out.println( -
{ -
System.out.println( -
+ ((TextMessage) arg0).getText()); -
} (JMSException e) { -
TODO Auto-generated catch block -
e.printStackTrace(); -
} -
-
} -
}); -
} (Exception e) { -
e.printStackTrace(); -
} -
-
} finally { -
try { -
if (null != connection) -
connection.close(); -
} catch (Throwable ignore) { -
} -
} -
-
} - }
-
-
-
-
-
上面的是用Queue的方式来创建,下面再用topic的方式实现同样的功能。 -
-
-
- import
javax.jms.Connection; - import
javax.jms.JMSException; - import
javax.jms.Message; - import
javax.jms.MessageConsumer; - import
javax.jms.MessageListener; - import
javax.jms.MessageProducer; - import
javax.jms.Session; - import
javax.jms.TextMessage; - import
javax.jms.Topic; -
- import
org.apache.activemq.ActiveMQConnectionFactor y; - import
org.apache.activemq.command.ActiveMQTopic; -
- public
class TopicTest { -
static void main(String[] throwsargs) Exception { -
ActiveMQConnectionFactor y factory = ActiveMQConnectionFactor y( -
-
-
Connection connection = factory.createConnection(); -
connection.start(); -
-
创建一个Topic -
Topic topic = ActiveMQTopic( "testTopic"); -
Session session = connection.createSession( -
Session.AUTO_ACKNOWLEDGE); -
-
注册消费者1 -
MessageConsumer comsumer1 = session.createConsumer(topic); -
comsumer1.setMessageListener( MessageListener() { -
void onMessage(Message m) { -
{ -
System.out.println( get " -
+ ((TextMessage) m).getText()); -
} (JMSException e) { -
e.printStackTrace(); -
} -
} -
}); -
-
注册消费者2 -
MessageConsumer comsumer2 = session.createConsumer(topic); -
comsumer2.setMessageListener( MessageListener() { -
void onMessage(Message m) { -
{ -
System.out.println( get " -
+ ((TextMessage) m).getText()); -
} (JMSException e) { -
e.printStackTrace(); -
} -
} -
-
}); -
-
创建一个生产者,然后发送多个消息。 -
MessageProducer producer = session.createProducer(topic); -
( inti 0;= i 10;< i++) { -
System.out.println( begin );produce=======" -
producer.send(session.createTextMessage( + i)); -
} -
} -
- }
- 使用activeMQ实现jms
- 使用activeMQ实现jms
- ActiveMq实现JMS
- activeMQ jms 简单实现
- JMS -- activeMQ使用
- spring-jms(activemq实现)使用queue发送消息简单例子
- JMS实现-ActiveMQ,介绍,安装,使用,注意点,spring整合
- java消息服务使用总结(JMS:通过ActiveMQ实现)
- spring 使用 ActiveMQ,JMS使用
- 【JMS】JMS之ActiveMQ的使用
- JMS 集成Spring 实现ActiveMQ
- JMS之ActiveMQ简单实现
- JMS Apache ActiveMQ使用(1)
- JMS Apache ActiveMQ使用(3)
- JMS Apache ActiveMQ使用(1)
- JMS Apache ActiveMQ使用(2)
- JMS Apache ActiveMQ使用(3)
- ActiveMQ和JMS的使用
- 【POJ】1639 Picnic Planning 度限制最小生成树
- 指针函数与函数指针的区别
- C语言中exit()与return的区别
- Nginx站点配置
- 设置debian静态ip
- 使用activeMQ实现jms
- CentOS下架设TFTP服务器
- android圆形旋转菜单,并支持移动换位功能
- CentOS下架设NFS服务器
- Java 中的堆和栈:
- 黑马程序员_C#基础之变量、数据类型、运算符
- 条纹光带学习笔记
- 【leetcode】Insertion Sort List
- Textarea去空格问题