activemq发布/订阅模式

1,发布/订阅模式:不会存储主题消息
     消息立即发送,如果没有订阅者,该消息会弃用,立即删除.     

2,Publish/Subscribe 处理模式(Topic)
               消息生产者(发布)将消息发布到 topic 中,同时有多个消息消费者(订阅)消费该消息。
               和点对点方式不同,发布到 topic 的消息会被所有订阅者消费。 当生产者发布消息,不管是否有消费者。都不会保存               消息 一 定要先有消息的消费者,后有消息的生产者。

                   

   

         (1) 发布者

/**
 * 发布/订阅模式:不会存储主题消息
 *       消息立即发送,如果没有订阅者,该消息会弃用,立即删除。
 * @author reyco
 */
public class Send {
	/** 
	 *   发送消息
	 * @param message  消息内容
	 */
	public void sendText(String message,Integer size) {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageProducer producer = null;
		TextMessage msg = null;
		try {
			connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
			//  创建订阅者/主题目的地
			destination = session.createTopic("first");
			producer = session.createProducer(null);
			for(int i=0;i<size;i++) {
				msg = session.createTextMessage("消息内容:"+message+"\t\r"+i);
				producer.send(destination, msg);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != producer) {
				try {
					producer.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (null != session) {
				try {
					session.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
			if (null != connection) {
				try {
					connection.close();
				} catch (JMSException e) {
					e.printStackTrace();
				}
			}
		}
	}
	public static void main(String[] args) throws Exception {
		Send send = new Send();
		send.sendText("系统信息!",5);
	}
}

      (2)订阅者:

       订阅者一:        

/**
 * 消费者
 * 
 * @author reyco
 */
public class Receiver1 {
	/**
	 * 接收消息
	 * @return
	 * @throws JMSException
	 */
	public void reveiverMessage() throws JMSException {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		try {
			connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
			destination = session.createTopic("first");
			consumer = session.createConsumer(destination);
			consumer.setMessageListener(new MessageListener() {
				@Override
				public void onMessage(Message message) {
					try {
						message.acknowledge();
						if (message instanceof TextMessage) {
							TextMessage tmsg = (TextMessage) message;
							String msg = tmsg.getText().toString();
							System.out.println(msg);
						}
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
			System.in.read();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != consumer) {
				consumer.close();
			}
			if (null != session) {
				session.close();
			}
			if (null != connection) {
				connection.close();
			}
		}
	}
	public static void main(String[] args) throws Exception {
		Receiver1 receiver = new Receiver1();
		receiver.reveiverMessage();
	}
}

              订阅者者二:

       

/**
 * 消费者
 * 
 * @author reyco
 */
public class Receiver2 {
	/**
	 * 接收消息
	 * @return
	 * @throws JMSException
	 */
	public void reveiverMessage() throws JMSException {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		try {
			connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
			destination = session.createTopic("first");
			consumer = session.createConsumer(destination);
			consumer.setMessageListener(new MessageListener() {
				@Override
				public void onMessage(Message message) {
					try {
						message.acknowledge();
						if (message instanceof TextMessage) {
							TextMessage tmsg = (TextMessage) message;
							String msg = tmsg.getText().toString();
							System.out.println(msg);
						}
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
			System.in.read();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != consumer) {
					consumer.close();
				}
				if (null != session) {
					session.close();
				}
				if (null != connection) {
					connection.close();
				}
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) throws Exception {
		Receiver2 receiver = new Receiver2();
		receiver.reveiverMessage();
	}
}

           订阅者三:

/**
 * 消费者
 * 
 * @author reyco
 */
public class Receiver3 {
	/**
	 * 接收消息
	 * @return
	 * @throws JMSException
	 */
	public void reveiverMessage() throws JMSException {
		ConnectionFactory connectionFactory = null;
		Connection connection = null;
		Session session = null;
		Destination destination = null;
		MessageConsumer consumer = null;
		try {
			connectionFactory = new ActiveMQConnectionFactory("username", "password", "tcp://ip:61616");
			connection = connectionFactory.createConnection();
			connection.start();
			session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
			destination = session.createTopic("first");
			consumer = session.createConsumer(destination);
			consumer.setMessageListener(new MessageListener() {
				@Override
				public void onMessage(Message message) {
					try {
						message.acknowledge();
						if (message instanceof TextMessage) {
							TextMessage tmsg = (TextMessage) message;
							String msg = tmsg.getText().toString();
							System.out.println(msg);
						}
					} catch (JMSException e) {
						e.printStackTrace();
					}
				}
			});
			System.in.read();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (null != consumer) {
					consumer.close();
				}
				if (null != session) {
					session.close();
				}
				if (null != connection) {
					connection.close();
				}
			} catch (JMSException e) {
				e.printStackTrace();
			}
		}
	}
	public static void main(String[] args) throws Exception {
		Receiver3 receiver = new Receiver3();
		receiver.reveiverMessage();
	}
}

 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

java的艺术

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值