ActiveMQ Topic使用(三)转

本篇主要演示ActiveMQ中的Topic使用

 

在开始演示前先引入一些概念:

Q:什么是Topic

A:Topic就是SUB/HUB即发布订阅模式,是SUB/hub就是一拖NUSB分线器的意思。意思就是一个来源分到N个出口。值得注意的是,此模式下,仅对有效的出口即时发送消息,如publisher发布消息Message AA,B服务器,此时B服务器服务未开启,则仅有A服务器收到消息Message A,事后B服务器开启但乃收不到消息Message A。此时,publisher再次发布消息Message B时,A,B服务器都能收到Message B

Q:ActiveMQ 支持哪些消息确认模式?

A:下面的表格来自《ActiveMQ in Action》原版CHAPTER 13 Tuning ActiveMQ for performance 内的节选,本人翻译的如有偏差请指出,括号内的是我个人的一些理解仅供参考。

确认模式

发送确认

描述

Session.AUTO_ACKNOWLEDGE

每条消息消耗时自动发送一条应答消息到ActiveMQ代理。

很慢,但常常作为消息消费者的默认的处理机制。

Session.DUPS_OK_ACKNOWLEDGE

允许消费者发送一条消息消耗应答消息到ActiveMQ代理

当达到预设限制的50%,一条确认消息将会被发回,最快的消费消息的标准方式。(最快往往意味着你要处理更多的东西,比如侦测和丢弃重发的消息)

ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE

为每条消息消费发送一条确认消息。

准许主控制单独确认已授权的消息,但这会很慢。

optimizeAcknowledge

准许消费者发送一条范围内的确认消息到ActiveMQ代理来确认消息消费。

结合Session.AUTO_ACKNOWLEDGE 一条消费消息将会在目标缓冲到65%时发送确认信息到ActiveMQ代理。这是最快的消息消费方式。(虽然作者很推崇,但我认为和上面一样存在一些问题需要处理。)

 

下面首先是Publisher(发布者)的例子:

 

Java代码 复制代码 收藏代码
  1. publicclass Publisher {
  2. publicstaticint count = 10;// 每次生成量
  3. privatestaticint total = 0;// 总发送次数
  4. privatestatic String brokerURL = "tcp://localhost:61616";// MQ 接口地址
  5. privatestatictransient ConnectionFactory factory;// JMX连接工厂
  6. privatetransient Connection connection;// JMX连接
  7. privatetransient Session session;// JMX会话
  8. privatetransient MessageProducer producer;// 消息生产这
  9. /**
  10. * 发布者构造方法
  11. *
  12. * @throws JMSException
  13. */
  14. public Publisher() throws JMSException {
  15. factory = new ActiveMQConnectionFactory(brokerURL);
  16. connection = factory.createConnection();
  17. try {
  18. connection.start();
  19. } catch (JMSException jmse) {
  20. connection.close();
  21. throw jmse;
  22. }
  23. // 送连接获得会话,获得的会话消息确认模式详见上面介绍
  24. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  25. // 从会话中获得生产者,由于是Topic,仅用于发布消息并未指定目标,所以为null
  26. producer = session.createProducer(null);
  27. }
  28. /**
  29. * 关闭连接
  30. *
  31. * @throws JMSException
  32. */
  33. publicvoid close() throws JMSException {
  34. if (connection != null) {
  35. connection.close();
  36. }
  37. }
  38. publicstaticvoid main(String[] args) throws JMSException {
  39. Publisher publisher = new Publisher();
  40. String[] a = new String[] { "test" };//生产出来的消息,放到哪个库存里
  41. while (total < 100) {
  42. for (int i = 0; i < count; i++) {
  43. publisher.sendMessage(a);
  44. }
  45. total += count;
  46. System.out.println("生产了:" + count + ",总共生产了:" + total);
  47. try {
  48. Thread.sleep(1000);
  49. } catch (InterruptedException x) {
  50. }
  51. }
  52. publisher.close();
  53. }
  54. /**
  55. * 消息发送
  56. *
  57. * @param stocks
  58. * @throws JMSException
  59. */
  60. protectedvoid sendMessage(String[] stocks) throws JMSException {
  61. for (String string : stocks) {
  62. Destination destination = session.createTopic("STOCKS." + string);
  63. Message message = createStockMessage(string, session);//构造消息
  64. System.out.println("发送: " + ((ActiveMQMapMessage) message).getContentMap() + " ,目标为: " + destination);
  65. producer.send(destination, message);//将消息发送给目标
  66. }
  67. }
  68. /**
  69. * 消息发送模版
  70. *
  71. * @param stock
  72. * @param session
  73. * @return
  74. * @throws JMSException
  75. */
  76. protected Message createStockMessage(String stock, Session session) throws JMSException {
  77. MapMessage message = session.createMapMessage();
  78. message.setString("stock", stock);
  79. message.setString("name", "商品");
  80. message.setDouble("price", 100.00);
  81. message.setDouble("offer", 50.00);
  82. message.setBoolean("promotion", false);
  83. return message;
  84. }
  85. }
public class Publisher {

	public static int count = 10;// 每次生成量
	private static int total = 0;// 总发送次数

	private static String brokerURL = "tcp://localhost:61616";// MQ 接口地址
	private static transient ConnectionFactory factory;// JMX连接工厂
	private transient Connection connection;// JMX连接
	private transient Session session;// JMX会话
	private transient MessageProducer producer;// 消息生产这

	/**
	 * 发布者构造方法
	 * 
	 * @throws JMSException
	 */
	public Publisher() throws JMSException {
		factory = new ActiveMQConnectionFactory(brokerURL);
		connection = factory.createConnection();
		try {
			connection.start();
		} catch (JMSException jmse) {
			connection.close();
			throw jmse;
		}
		// 送连接获得会话,获得的会话消息确认模式详见上面介绍
		session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		// 从会话中获得生产者,由于是Topic,仅用于发布消息并未指定目标,所以为null
		producer = session.createProducer(null);
	}

	/**
	 * 关闭连接
	 * 
	 * @throws JMSException
	 */
	public void close() throws JMSException {
		if (connection != null) {
			connection.close();
		}
	}

	public static void main(String[] args) throws JMSException {
		Publisher publisher = new Publisher();
		String[] a = new String[] { "test" };//生产出来的消息,放到哪个库存里
		while (total < 100) {
			for (int i = 0; i < count; i++) {
				publisher.sendMessage(a);
			}
			total += count;
			System.out.println("生产了:" + count + ",总共生产了:" + total);
			try {
				Thread.sleep(1000);
			} catch (InterruptedException x) {
			}
		}
		publisher.close();
	}

	/**
	 * 消息发送
	 * 
	 * @param stocks
	 * @throws JMSException
	 */
	protected void sendMessage(String[] stocks) throws JMSException {
		for (String string : stocks) {
			Destination destination = session.createTopic("STOCKS." + string);
			Message message = createStockMessage(string, session);//构造消息
			System.out.println("发送: " + ((ActiveMQMapMessage) message).getContentMap() + " ,目标为: " + destination);
			producer.send(destination, message);//将消息发送给目标
		}
	}

	/**
	 * 消息发送模版
	 * 
	 * @param stock
	 * @param session
	 * @return
	 * @throws JMSException
	 */
	protected Message createStockMessage(String stock, Session session) throws JMSException {
		MapMessage message = session.createMapMessage();
		message.setString("stock", stock);
		message.setString("name", "商品");
		message.setDouble("price", 100.00);
		message.setDouble("offer", 50.00);
		message.setBoolean("promotion", false);
		return message;
	}
}

接着是Consumer

Java代码 复制代码 收藏代码
  1. publicclass Consumer {
  2. privatestatic String brokerURL = "tcp://localhost:61616";
  3. privatestatictransient ConnectionFactory factory;
  4. privatetransient Connection connection;
  5. privatetransient Session session;
  6. public Consumer() throws JMSException {
  7. factory = new ActiveMQConnectionFactory(brokerURL);
  8. connection = factory.createConnection();
  9. connection.start();
  10. session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
  11. }
  12. publicvoid close() throws JMSException {
  13. if (connection != null) {
  14. connection.close();
  15. }
  16. }
  17. publicstaticvoid main(String[] args) throws JMSException {
  18. Consumer consumer = new Consumer();
  19. String[] stocks=new String[]{"test"};//数据仓库名
  20. for (String stock : stocks) {
  21. Destination destination = consumer.getSession().createTopic("STOCKS." + stock);
  22. MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);
  23. messageConsumer.setMessageListener(new MyListener());
  24. }
  25. }
  26. public Session getSession() {
  27. return session;
  28. }
  29. }
public class Consumer {  
  
    private static String brokerURL = "tcp://localhost:61616";  
    private static transient ConnectionFactory factory;  
    private transient Connection connection;  
    private transient Session session;  
      
    public Consumer() throws JMSException {  
        factory = new ActiveMQConnectionFactory(brokerURL);  
        connection = factory.createConnection();  
        connection.start();  
        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);  
    }  
      
    public void close() throws JMSException {  
        if (connection != null) {  
            connection.close();  
        }  
    }      
      
    public static void main(String[] args) throws JMSException {  
        Consumer consumer = new Consumer();  
        String[] stocks=new String[]{"test"};//数据仓库名
        for (String stock : stocks) {  
            Destination destination = consumer.getSession().createTopic("STOCKS." + stock);  
            MessageConsumer messageConsumer = consumer.getSession().createConsumer(destination);  
            messageConsumer.setMessageListener(new MyListener());  
        }  
    }  
      
    public Session getSession() {  
        return session;  
    }  
  
} 

最后是一个消息监听器,十分简单

Java代码 复制代码 收藏代码
  1. publicclass MyListener implements MessageListener {
  2. publicstaticint a=0;
  3. @Override
  4. publicvoid onMessage(Message message) {
  5. System.out.println("进入监听");
  6. try {
  7. MapMessage map = (MapMessage) message;
  8. String stock = map.getString("stock");
  9. String name = map.getString("name");
  10. double price = map.getDouble("price");
  11. double offer = map.getDouble("offer");
  12. boolean promotion = map.getBoolean("promotion");
  13. DecimalFormat df = new DecimalFormat("#,###,###,##0.00");
  14. System.out.println("仓库号:"+stock + ",商品名:"+name+",销售价格:" + df.format(price) + ",供应价格:" + df.format(offer)
  15. + ",是否促销:" + (promotion ? "是" : "否")+",共获得"+ ++a);
  16. } catch (Exception e) {
  17. e.printStackTrace();
  18. }
  19. }
  20. }
public class MyListener implements MessageListener {
	public static int a=0;
	@Override
	public void onMessage(Message message) {
		System.out.println("进入监听");
		try {
			MapMessage map = (MapMessage) message;
			String stock = map.getString("stock");
			String name = map.getString("name");
			double price = map.getDouble("price");
			double offer = map.getDouble("offer");
			boolean promotion = map.getBoolean("promotion");
			DecimalFormat df = new DecimalFormat("#,###,###,##0.00");
			System.out.println("仓库号:"+stock + ",商品名:"+name+",销售价格:" + df.format(price) + ",供应价格:" + df.format(offer)
				+ ",是否促销:" + (promotion ? "是" : "否")+",共获得"+ ++a);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

使用方法:(不太喜欢传图片,自己执行代码看结果吧)

启动2个Consumer,成功启动后,启动publisher,可以看到2个Consumer正常打印了publisher传过来的所有数据和条数一一对应。

 

启动1个Consumer,成功启动后启动publisher,在publisher未生产完所有数据前,启动另一个Consumer,可以看到先启动的一个正常接收了所有数据,另一个只接收了他启动后publisher生产的数据。

猜你喜欢

转载自frank1998819.iteye.com/blog/2230632
今日推荐