ActiveMQ 一个简单的例子
2009-05-24 00:29
561 查看
ActiveMQ
的一个简单示例
function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}
最近由于公司项目需要,开始学习
JMS
,用的是
ActiveMQ
。由于这方面网上的例子不是很多,而且有的也不完整。于是经过几天的摸索学习,写了一个简单的小例子,现在贴出来与大家分享。
ProducerTool.java
用于发送消息:
package
homework;
import
javax.jms.Connection;
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
ProducerTool ...
{
private
String user = ActiveMQConnection.DEFAULT_USER;
private
String password = ActiveMQConnection.DEFAULT_PASSWORD;
private
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private
String subject = "TOOL.DEFAULT";
private
Destination destination =
null
;
private
Connection connection =
null
;
private
Session session =
null
;
private
MessageProducer producer =
null
;
//
初始化
private
void
initialize()
throws
JMSException, Exception ...
{
ActiveMQConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(
false
, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
//
发送消息
public
void
produceMessage(String message)
throws
JMSException, Exception ...
{
initialize();
TextMessage msg = session.createTextMessage(message);
connection.start();
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
System.out.println("Producer:->Message sent complete!");
}
//
关闭连接
public
void
close()
throws
JMSException ...
{
System.out.println("Producer:->Closing connection");
if
(producer !=
null
)
producer.close();
if
(session !=
null
)
session.close();
if
(connection !=
null
)
connection.close();
}
}
ConsumerTool.java
用于接受消息,我用的是基于消息监听的机制,需要实现
MessageListener
接口,这个接口有个
onMessage
方法,当接受到消息的时候会自动调用这个函数对消息进行处理。
package
homework;
import
javax.jms.Connection;
import
javax.jms.Destination;
import
javax.jms.JMSException;
import
javax.jms.MessageConsumer;
import
javax.jms.Session;
import
javax.jms.MessageListener;
import
javax.jms.Message;
import
javax.jms.TextMessage;
import
org.apache.activemq.ActiveMQConnection;
import
org.apache.activemq.ActiveMQConnectionFactory;
public
class
ConsumerTool
implements
MessageListener ...
{
private
String user = ActiveMQConnection.DEFAULT_USER;
private
String password = ActiveMQConnection.DEFAULT_PASSWORD;
private
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private
String subject = "TOOL.DEFAULT";
private
Destination destination =
null
;
private
Connection connection =
null
;
private
Session session =
null
;
private
MessageConsumer consumer =
null
;
//
初始化
private
void
initialize()
throws
JMSException, Exception ...
{
ActiveMQConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(
false
, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
consumer = session.createConsumer(destination);
}
//
消费消息
public
void
consumeMessage()
throws
JMSException, Exception ...
{
initialize();
connection.start();
System.out.println("Consumer:->Begin listening...");
//
开始监听
consumer.setMessageListener(
this
);
// Message message = consumer.receive();
}
//
关闭连接
public
void
close()
throws
JMSException ...
{
System.out.println("Consumer:->Closing connection");
if
(consumer !=
null
)
consumer.close();
if
(session !=
null
)
session.close();
if
(connection !=
null
)
connection.close();
}
//
消息处理函数
public
void
onMessage(Message message) ...
{
try
...
{
if
(message
instanceof
TextMessage) ...
{
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
}
else
...
{
System.out.println("Consumer:->Received: " + message);
}
}
catch
(JMSException e) ...
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果想主动的去接受消息,而不用消息监听的话,把
consumer.setMessageListener(this)
改为
Message message = consumer.receive()
,手动去调用
MessageConsumer
的
receive
方法即可。
下面是测试类
Test.java
:
package
homework;
import
javax.jms.JMSException;
public
class
Test ...
{
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
throws
JMSException, Exception ...
{
// TODO Auto-generated method stub
ConsumerTool consumer =
new
ConsumerTool();
ProducerTool producer =
new
ProducerTool();
//
开始监听
consumer.consumeMessage();
//
延时
500
毫秒之后发送消息
Thread.sleep(500);
producer.produceMessage("Hello, world!");
producer.close();
//
延时
500
毫秒之后停止接受消息
Thread.sleep(500);
consumer.close();
}
}
以上就是我学习
ActiveMQ
之后写的一个简单的例子,希望对你有帮助,如果有什么错误还请指正。
的一个简单示例
function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}
最近由于公司项目需要,开始学习
JMS
,用的是
ActiveMQ
。由于这方面网上的例子不是很多,而且有的也不完整。于是经过几天的摸索学习,写了一个简单的小例子,现在贴出来与大家分享。
ProducerTool.java
用于发送消息:
package
homework;
import
javax.jms.Connection;
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
ProducerTool ...
{
private
String user = ActiveMQConnection.DEFAULT_USER;
private
String password = ActiveMQConnection.DEFAULT_PASSWORD;
private
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private
String subject = "TOOL.DEFAULT";
private
Destination destination =
null
;
private
Connection connection =
null
;
private
Session session =
null
;
private
MessageProducer producer =
null
;
//
初始化
private
void
initialize()
throws
JMSException, Exception ...
{
ActiveMQConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(
false
, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
producer = session.createProducer(destination);
producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
}
//
发送消息
public
void
produceMessage(String message)
throws
JMSException, Exception ...
{
initialize();
TextMessage msg = session.createTextMessage(message);
connection.start();
System.out.println("Producer:->Sending message: " + message);
producer.send(msg);
System.out.println("Producer:->Message sent complete!");
}
//
关闭连接
public
void
close()
throws
JMSException ...
{
System.out.println("Producer:->Closing connection");
if
(producer !=
null
)
producer.close();
if
(session !=
null
)
session.close();
if
(connection !=
null
)
connection.close();
}
}
ConsumerTool.java
用于接受消息,我用的是基于消息监听的机制,需要实现
MessageListener
接口,这个接口有个
onMessage
方法,当接受到消息的时候会自动调用这个函数对消息进行处理。
package
homework;
import
javax.jms.Connection;
import
javax.jms.Destination;
import
javax.jms.JMSException;
import
javax.jms.MessageConsumer;
import
javax.jms.Session;
import
javax.jms.MessageListener;
import
javax.jms.Message;
import
javax.jms.TextMessage;
import
org.apache.activemq.ActiveMQConnection;
import
org.apache.activemq.ActiveMQConnectionFactory;
public
class
ConsumerTool
implements
MessageListener ...
{
private
String user = ActiveMQConnection.DEFAULT_USER;
private
String password = ActiveMQConnection.DEFAULT_PASSWORD;
private
String url = ActiveMQConnection.DEFAULT_BROKER_URL;
private
String subject = "TOOL.DEFAULT";
private
Destination destination =
null
;
private
Connection connection =
null
;
private
Session session =
null
;
private
MessageConsumer consumer =
null
;
//
初始化
private
void
initialize()
throws
JMSException, Exception ...
{
ActiveMQConnectionFactory connectionFactory =
new
ActiveMQConnectionFactory(
user, password, url);
connection = connectionFactory.createConnection();
session = connection.createSession(
false
, Session.AUTO_ACKNOWLEDGE);
destination = session.createQueue(subject);
consumer = session.createConsumer(destination);
}
//
消费消息
public
void
consumeMessage()
throws
JMSException, Exception ...
{
initialize();
connection.start();
System.out.println("Consumer:->Begin listening...");
//
开始监听
consumer.setMessageListener(
this
);
// Message message = consumer.receive();
}
//
关闭连接
public
void
close()
throws
JMSException ...
{
System.out.println("Consumer:->Closing connection");
if
(consumer !=
null
)
consumer.close();
if
(session !=
null
)
session.close();
if
(connection !=
null
)
connection.close();
}
//
消息处理函数
public
void
onMessage(Message message) ...
{
try
...
{
if
(message
instanceof
TextMessage) ...
{
TextMessage txtMsg = (TextMessage) message;
String msg = txtMsg.getText();
System.out.println("Consumer:->Received: " + msg);
}
else
...
{
System.out.println("Consumer:->Received: " + message);
}
}
catch
(JMSException e) ...
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
如果想主动的去接受消息,而不用消息监听的话,把
consumer.setMessageListener(this)
改为
Message message = consumer.receive()
,手动去调用
MessageConsumer
的
receive
方法即可。
下面是测试类
Test.java
:
package
homework;
import
javax.jms.JMSException;
public
class
Test ...
{
/** */
/**
*
@param
args
*/
public
static
void
main(String[] args)
throws
JMSException, Exception ...
{
// TODO Auto-generated method stub
ConsumerTool consumer =
new
ConsumerTool();
ProducerTool producer =
new
ProducerTool();
//
开始监听
consumer.consumeMessage();
//
延时
500
毫秒之后发送消息
Thread.sleep(500);
producer.produceMessage("Hello, world!");
producer.close();
//
延时
500
毫秒之后停止接受消息
Thread.sleep(500);
consumer.close();
}
}
以上就是我学习
ActiveMQ
之后写的一个简单的例子,希望对你有帮助,如果有什么错误还请指正。
相关文章推荐
- 一个Java activemq的简单例子
- 编写一个简单的SSH例子
- 一个最简单的sdl显示图片例子sdl example sdl 例子 例程SDL贴图bmp
- qt多线程一个简单的例子
- Servlet学习教程(三)---- 一个简单的Servlet例子
- 一个最简单的socket通信的例子
- 在Android Studio下使用百度语音识别的一个简单例子
- Python自动化测试例子--一个简单的自动化测试用例脚本--批量执行测试用例
- 一个简单的三层架构例子(.NET入门)
- 一个简单的WebSerivce传递图片的例子(转贴)
- 想寻找一个简单的将图片插入数据库的例子~
- 一个动态编译的简单例子
- 一个简单的例子教你明白XMLHTTPRequest的原理
- iBATIS教程 (二)环境的安装和配置,以及一个简单的例子
- 如何使用Oracle存储过程的一个简单例子
- 中级JavaScript例子, 如何实现一个简单实用的模板套用机制, GXTemplate , 第4章(估计要写9章)
- 图灵机器人(智能云交互API)的一个java实现简单例子
- 一个poll的简单例子
- 一个使用ListView的简单例子
- 使用Eclipse+maven3插件开发一个Servlet3.0的简单例子