Message组成详解
1.1JMS message组成说明
JMS message包含两部分,头和承载体. 头中提供被客户端和provider使用的元数据, 承载体包含着实际的数据.
客户端send方法定义的
- JMS Destination 消息送的目的地
- JMS DeliveryMode--传送模式.总共分两种:持久化和非持久化的.默认是采用持久化的模式
persist: 能确保发送失败的时候不会丢失,会多次发送直到成功,但是需要额外的存储空间开销,并且是可靠的
non-persist:只会发送一次不论成功与否
- JMS Expiration---message会过期,headers用来确保发送的消息不会是过期的消息。设置过期值可以用 MessageProducer.setTimeToLive()来设置全局的过期时间或者用 MessageProducer.send()来为每个消息单独设置过期时间(默认消息是不会过期的,设置过期时间为0消息也不会过期)
- JMS MESSAGEID ---message的唯一标示符.使用messageId会招致额外的开销,消息生产者可以建议MQ不需要依赖headers的值通过使用 MessageProducer.setDisableMessageID()方法,此时messageID必须设置成NULL
- JMS Priority--优先级.用来说明消息的重要性的级别.一旦被设置,那么这个producer的所有消息都适用.JMS提供10个级别从0-9(最高).MQ不需要实现消息排序,尽管很多都有排序.它们只需要保证高优先级的消息先发送出去即可.
- JMS Timestamp---用来纪录被发送的时间戳。可以用方法MessageProducer.setDisableMessageTimestamp()来禁用.
客户端定义的
- JMS Correlation ID --用来关联当前的message和之前的message。例如一个请求message和一个应答message。它的值可以是以下几种:一个特定于提供程序的消息ID, 应用程序特定的字符串 ,字节数组值。
- JMS ReplyTo----有些情况下,一个JMS消息生产者可能会要求消费者对一条消息做出应答。JMSReplyTo消息头包含了一个javax.jms.Destination,标明了JMS消费者应该应答的地址。在使用请求/应答场景时,通过这条消息头属性可以进一步实现消息生产者和消息消费者之间的去耦
- JMS Type -----定义message的类型.类型和具体的provider来定义.
MQ定义的
- JMS Redelivered----JMSRedelivered消息头表示会重新发送给消费者。如果该消息被重新传送,JMSRedelivered消息头就为true,否则为false。如果一个消费者未能确认先前传送的消息,或者JMS提供者并不确定消费者是否已经接收到该消息时,就可以将这条消息标记为重新传送
以下是Message接口定义
public interface Message {
...
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;
boolean propertyExists(String name) 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;
.. }
getPropertyNames用来返回一个枚举列表,通过它可以迭代这个message的所有属性.propertyExists()方法用来判断这个message是否包含属性
你可以获得到三种类型的属性:用户属性. JMS自定义属性,MQ特殊属性.
- 用户属性可以自由定义.通过getBooleanProperty()/setBooleanProperty() , getStringProperty()/setStringProperty这些方法
- JMS自定义属性.
- JMSXAppID —标识发送消息的应用程序
- JMSX ConsumerTXID —被消费的事务标识符内的事务
- JMSXDeliveryCount—消息发送的次数
- JMSXGroupID —这个消息属于哪个消息组
- JMSXGroupSeq—这个消息在消息组中的序号
- JMSXProducerTXID —生产的事务标识符内的事务
- JMSXRcvTimestamp —把消息发送给消费者的时间戳
- JMSXState—用于定义特定于提供程序的状态
- JMSXUserID —发送消息的用户ID
标红的两个属性是比较有用的,使用它们,可以对消息进行分组和排序.
- MQ特殊属性.
下面来看看一个条件过滤的例子
public void sendStockMessage(Session session, MessageProducer producer, Destination destination, String payload,
String symbol, double price) throws JMSException {
TextMessage textMessage = session.createTextMessage();
textMessage.setText(payload);
textMessage.setStringProperty("SYMBOL", symbol);
textMessage.setDoubleProperty("PRICE", price);
producer.send(destination, textMessage);
}
...
//String selector = "SYMBOL = 'AAPL'";(过滤表达式)
String selector = "SYMBOL = 'AAPL' AND PRICE > "
+ getPreviousPrice();
String selector = "SYMBOL IN ('AAPL', 'CSCO') AND PRICE > "
+ getPreviousPrice() + " AND PE_RATIO < "
+ getCurrentAcceptedPriceToEarningsRatioThreshold();
MessageConsumer consumer =
session.createConsumer(destination, selector);
...
以下是选择器的语法
1.3message承载体
jms定义了6种java类型的消息体
- Message — 基础消息类型.没有信息载体,只发送header和properties.通常用于简单的事件通知.
- TextMessge —带有 字符串类型的消息载体.用于发送简单文本和xml数据
- MapMessage —带有一系列键值对的消息载体.key是string类型,value是java基本类型
- ByteMessage —用于包含一个字节数组
- StreamMessage ---用java基本类型来填充的字符流作为消息载体
- ObjectMessage---可序列化的java对象作为载体,也支持java 集合
- 点对点模式:生产者生产的信息只能被一个消费者消费.可以使用MessageConsumer.receive()从队列同步的接受消息或者使用MessageConsumer.setMessageListener()异步的接受消息.队列会储存消息知道被消费.
- 发布订阅模式:生产者生产的信息能被多个消费者消费.Topic不保存消息(除非通过订阅)
