码迷,mamicode.com
首页 > 其他好文 > 详细

activeMQ-简单应用

时间:2018-08-28 15:24:50      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:boolean   tostring   目的   []   mod   创建   trace   jar   user   

所需要的jar

技术分享图片

 点对点消息发送端

package com.lm.acticemq;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.DeliveryMode;
import javax.jms.Destination;
import javax.jms.MapMessage;
import javax.jms.MessageProducer;
import javax.jms.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Sender {

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);
// 获取session注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
// 得到消息生成者【发送者】
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++) {
User user = new User("006"+i, "622", String.valueOf(i+20));
ObjectMessage message = session.createObjectMessage();
message.setObject(user);
/* TextMessage message = session
.createTextMessage("ActiveMq 发送的消息" + i);*/
// 发送消息到目的地方
System.out.println("发送消息:" + "ActiveMq 发送的消息" + user.toString());
producer.send(message);
}
}
}

点对点  消息接收端

 

package com.lm.acticemq;

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.ObjectMessage;
import javax.jms.Session;
import javax.jms.TextMessage;
import org.apache.activemq.ActiveMQConnection;
import org.apache.activemq.ActiveMQConnectionFactory;

public class Receiver {

public static void main(String[] args) {

// MessageListener
// 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注意参数值xingbo.xu-queue是一个服务器的queue,须在在ActiveMq的console配置
destination = session.createQueue("FirstQueue");
consumer = session.createConsumer(destination);
/*while (true) {
//设置接收者接收消息的时间,为了便于测试,这里谁定为100s
// TextMessage message = (TextMessage) consumer.receive(100000);
// if (null != message) {
// System.out.println("收到消息" + message.getText());
// } else {
// break;
// }
ObjectMessage message = (ObjectMessage)consumer.receive();

if (message != null) {
User user = (User)message.getObject();
System.err.println(user.toString());
}
}*/
//使用监听器来监听发送的消息
consumer.setMessageListener(new MessageListener() {

@Override
public void onMessage(Message message) {
try {
Object object = ((ObjectMessage)message).getObject();
if (object != null) {
User user = (User)object;
System.out.println("ActiveMQ接收到的消息"+user.toString());
}
} catch (JMSException e) {
e.printStackTrace();
}
}
});
} catch (Exception e) {
e.printStackTrace();
}
}
}

 

activeMQ-简单应用

标签:boolean   tostring   目的   []   mod   创建   trace   jar   user   

原文地址:https://www.cnblogs.com/bkxyq/p/9548099.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!