ProducerTool.java用于发送消息:
package com.google.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 com.google.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 com.google.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的简单例子,我只是留个备份,你们想要可以私聊我
博客http://blog.csdn.net/poorCoder_/article/details/61192791的代码。Spring整合ActiveMQ的简单实例
activemq简单实例 运行后可以在页面查看相关信息 http://127.0.0.1:8161/admin/queueConsumers.jsp?JMSDestination=sample.queue
Apache ActiveMQ5.8 入门最简单例子 消息队列 Master
一个小程序让你快速入门JMS ActiveMQ MQ 消息队列。记录下来方便以后自己查询学习。
运行环境:apache-activemq-5.11.1-bin.zip ,JDK:1.8。网页管理地址:http://ip:8161/admin/queues.jsp 账号密码同为:admin
基于Maven的ActiveMQ的简单实例
activeMQ的java简单实例.zipactiveMQ的java简单实例.zip
包含了ActiveMQ的两种模式(点对点、发布与订阅),用idea开发与测试,运行前请先安装ActiveMQ
ActiveMq-JMS简单实例使用tomcat.pdf
一、JMS基本概念 二、activemq介绍及安装 ...三、activemq简单实例 四、activemq整合spring运用 五、activemq常见问题 5.1 activemq 消息传递 5.2 activemq 消息确认机制 5.3 activemq 持久化机制
对ActiveMQ的两种消息模式用java进行了一个简单实现。
我们使用ActiveMQ为大家实现一种点对点的消息模型。如果你对点对点模型的认识较浅,可以看一下第一篇博文的介绍。
activemq 通过ajax发送接收消息简单例子
ActiveMq-JMS-tomcat简单实例,内有代码文档和源码
ActiveMQ 是Apache出品,最流行的,能力强劲的开源消息总线. 本压缩包是是实现ActiveMQ中点对点与发布订阅模式的java项目,下载后可配合运行好的ActiveMQ服务直接运行。 没有ActiveMQ服务的可下载本人的ActiveMQ消息...
自己写的ActiveMQ简单demo,包括生产者、消费者之间发送消息、持久化到文件和持久化到数据库,期中持久化需要修改activemq.xml文件
ActiveMQ简单例子和流程,供学习用!
ActiveMq-JMS简单实例使用tomcat.doc