activeMq demo

package com.example.demo;

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.ActiveMQConnectionFactory;

/**
 * <table border='1'>
 * <tr>
 * <td>JMS 公共</td>
 * <td>点对点域</td>
 * <td>发布/订阅域</td>
 * </tr>
 * <tr>
 * <td>ConnectionFactory</td>
 * <td>QueueConnectionFactory</td>
 * <td>TopicConnectionFactory</td>
 * </tr>
 * <tr>
 * <td>Connection</td>
 * <td>QueueConnection</td>
 * <td>TopicConnection</td>
 * </tr>
 * <tr>
 * <td>Destination</td>
 * <td>Queue</td>
 * <td>Topic</td>
 * </tr>
 * <tr>
 * <td>Session</td>
 * <td>QueueSession</td>
 * <td>TopicSession</td>
 * </tr>
 * <tr>
 * <td>MessageProducer</td>
 * <td>QueueSender</td>
 * <td>TopicPublisher</td>
 * </tr>
 * <tr>
 * <td>MessageConsumer</td>
 * <td>QueueReceiver</td>
 * <td>TopicSubscriber</td>
 * </tr>
 * 
 * </table>
 * 
 * (1)、点对点方式(point-to-point) 点对点的消息发送方式主要建立在 Message
 * Queue,Sender,reciever上,Message Queue 存贮消息, Sneder
 * 发送消息,receive接收消息.具体点就是Sender Client发送Message Queue , 而 receiver
 * Cliernt从Queue中接收消息和"发送消息已接受"到Quere,确认消息接收。
 * 消息发送客户端与接收客户端没有时间上的依赖,发送客户端可以在任何时刻发送信息到Queue,而不需要知道接收客户端是不是在运行 (2)、发布/订阅
 * 方式(publish/subscriber Messaging) 发布/订阅方式用于多接收客户端的方式.作为发布订阅的方式,可能存在多个接收客户端,
 * 并且接收端客户端与发送客户端存在时间上的依赖。一个接收端只能接收他创建以后发送客户端发送的信息。作为subscriber
 * ,在接收消息时有两种方法,destination的receive方法,和实现message listener 接口的onMessage 方法。
 *
 *
 */
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, ActiveMQConnection.DEFAULT_BROKER_URL);
		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++) {
			String msg = "ActiveMq 发送的消息" + i;
			TextMessage message = session.createTextMessage(msg);
			// 发送消息到目的地方
			System.out.println(msg);
			producer.send(message);
		}
	}
}
package com.example.demo;

import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Destination;
import javax.jms.MessageConsumer;
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) {
		// 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, ActiveMQConnection.DEFAULT_BROKER_URL);
		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;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != connection)
					connection.close();
			} catch (Throwable ignore) {
			}
		}
	}
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值