Jms两种message传输方式Topic和Queue的比较

/**

*  转载请注明作者longdick    http://longdick.iteye.com

*

*/

 

 

Jms规范里的两种message传输方式Topic和Queue,两者的对比如下表():

 

 

Topic Queue
概要 Publish Subscribe messaging 发布订阅消息Point-to-Point 点对点
有无状态 topic数据默认不落地,是无状态的。

Queue数据默认会在mq服务器上以文件形式保存,比如Active MQ一般保存在$AMQ_HOME\data\kr-store\data下面。也可以配置成DB存储。

完整性保障 并不保证publisher发布的每条数据,Subscriber都能接受到。Queue保证每条数据都能被receiver接收。
消息是否会丢失 一般来说publisher发布消息到某一个topic时,只有正在监听该topic地址的sub能够接收到消息;如果没有sub在监听,该topic就丢失了。Sender发送消息到目标Queue,receiver可以异步接收这个Queue上的消息。Queue上的消息如果暂时没有receiver来取,也不会丢失。
消息发布接收策略 一对多的消息发布接收策略,监听同一个topic地址的多个sub都能收到publisher发送的消息。Sub接收完通知mq服务器一对一的消息发布接收策略,一个sender发送的消息,只能有一个receiver接收。receiver接收完后,通知mq服务器已接收,mq服务器对queue里的消息采取删除或其他操作。

 

以下是符合Jms1.1规范的使用Queue传输消息的代码,使用Active mq作为Jms的实现,想要更clean的代码,可以考虑将Amq的实现DI:

 

public void testMySend() throws JMSException {
		Connection connection = new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616").createConnection();
	        Session session = null;
	        Queue queue = null;
	        MessageProducer producer = null;
	        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        queue=session.createQueue("myTest");
	        producer= session.createProducer(queue);
	        Message msg=session.createTextMessage("hello");
	        producer.send(msg);      
	        producer.close();
	        session.close();
	        connection.close();
	}
	
	public void testMyReceive() throws JMSException{
		Connection connection = new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616").createConnection();
		Session session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
		Queue queue = session.createQueue("myTest");
		MessageConsumer consumer= session.createConsumer(queue);
		consumer.setMessageListener(new MyListener());
		connection.start();
		consumer.close();
		session.close();
	        connection.close();
	}
	

 

消费端需要设定一个MessageListener:

 

private class MyListener implements MessageListener{
		public void onMessage(Message message) {
		System.out.println("msg start!----------------");
		try {
			System.out.println(""+((TextMessage)message).getText());
		} catch (JMSException e) {
			e.printStackTrace();
		}
		System.out.println("msg end!----------------");
		}
	}

 

以下是符合Jms1.1规范的使用Topic 发送消息的代码:

 

public void testMyTopicPublisher() throws JMSException {
		 Connection connection = new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616").createConnection();
	        Session session = null;
	        Topic topic = null;
	        MessageProducer producer = null;
	        session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
	        topic=session.createTopic("myTopicTest");
	        producer= session.createProducer(topic);
	        producer.setDeliveryMode(DeliveryMode.NON_PERSISTENT);
	        Message msg=session.createTextMessage("hello and whatever u say");    
	        producer.send(msg);  
	        producer.close();
	        session.close();
	        connection.close();
	}
 

以下是符合Jms1.1规范的使用Topic 接收消息的代码:

 

public static void testMyTopicConsumer() throws JMSException, InterruptedException{
		Connection connection = new ActiveMQConnectionFactory("admin","admin","tcp://localhost:61616").createConnection();
		 Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);
		 Topic topic = session.createTopic("myTopicTest");
		 MessageConsumer consumer= session.createConsumer(topic);
		 consumer.setMessageListener(new MyListener());
		 connection.start();
	}

 

jms在创建Session时可以有两个参数,第一个参数是是否使用事务,第二个参数是消费者向发送者确认消息已经接收的方式:

 

Session session = connection.createSession(false, Session.CLIENT_ACKNOWLEDGE);

 

确认消息的方式有如下三种:

AUTO_ACKNOWLEDGE(自动通知)

CLIENT_ACKNOWLEDGE(客户端自行决定通知时机)

DUPS_OK_ACKNOWLEDGE(延时//批量通知)

 

如果使用的是 客户端自行决定通知时机 方式,那么需要在MessageListener里显式调用message.acknowledge()来通知服务器。服务器接收到通知后采取相应的操作。

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
基于Java通讯开发jms源代码 (jms通讯开发源码) java,net,socket,通讯开发,jms /* * @(#)Message.java 1.60 02/04/09 * * Copyright 1997-2002 Sun Microsystems, Inc. All Rights Reserved. * * SUN PROPRIETARY/CONFIDENTIAL. * This software is the proprietary information of Sun Microsystems, Inc. * Use is subject to license terms. * */ import java.util.Enumeration; public interface Message { String getJMSMessageID() throws JMSException; void setJMSMessageID(String id) throws JMSException; long getJMSTimestamp() throws JMSException; void setJMSTimestamp(long timestamp) throws JMSException; byte [] getJMSCorrelationIDAsBytes() throws JMSException; void setJMSCorrelationIDAsBytes(byte[] correlationID) throws JMSException; String getJMSCorrelationID() throws JMSException; Destination getJMSReplyTo() throws JMSException; void setJMSReplyTo(Destination replyTo) throws JMSException; Destination getJMSDestination() throws JMSException; void setJMSDestination(Destination destination) throws JMSException; int getJMSDeliveryMode() throws JMSException; void setJMSDeliveryMode(int deliveryMode) throws JMSException; boolean getJMSRedelivered() throws JMSException; void setJMSRedelivered(boolean redelivered) throws JMSException; String getJMSType() throws JMSException; void setJMSType(String type) throws JMSException; long getJMSExpiration() throws JMSException; void setJMSExpiration(long expiration) throws JMSException; int getJMSPriority() throws JMSException; void setJMSPriority(int priority) throws JMSException; void clearProperties() throws JMSException; boolean propertyExists(String name) throws JMSException; boolean getBooleanProperty(String name) throws JMSException; byte getByteProperty(String name) throws JMSException; short getShortProperty(String name) throws JMSException; int getIntProperty(String name) throws JMSException; long getLongProperty(String name) throws JMSException; float getFloatProperty(String name) throws JMSException; double getDoubleProperty(String name) throws JMSException; String getStringProperty(String name) throws JMSException; Object getObjectProperty(String name) throws JMSException; Enumeration getPropertyNames() throws JMSException; void setBooleanProperty(String name, boolean value) throws JMSException; void setByteProperty(String name, byte value) throws JMSException; void setShortProperty(String name, short value) throws JMSException; void setIntProperty(String name, int value) throws JMSException; void setLongProperty(String name, long value) throws JMSException; void setFloatProperty(String name, float value) throws JMSException; void setDoubleProperty(String name, double value) throws JMSException; void setStringProperty(String name, String value) throws JMSException; void setObjectProperty(String name, Object value) throws JMSException; void acknowledge() throws JMSException; void clearBody() throws JMSException; } 通讯开发必备源码资料!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值