jms-message对象

message必须通过session创建,以便完成对应的初始化,而不是通过new创建,new创建出来的对象,确实很多信息。

Message
消息由三部分组成


头,属性和内容。


所有获取头内容的方法都一getJMSXxx命名。


大致定义如下

public interface Message {

    static final int DEFAULT_DELIVERY_MODE = DeliveryMode.PERSISTENT;

    static final int DEFAULT_PRIORITY = 4;

    static final long DEFAULT_TIME_TO_LIVE = 0;

    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;

    void setJMSCorrelationID(String 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;
}


其中消息头分为两类,一类是自动分配的,一类是开发人员指定的,大多数是自动分配。
获取消息头一般通过Message对象就可以获取
设置消息头,自动类则大多是通过发送者设置。非自动类头则通过message对象设置。

JMSDestination
该头信息用来标示来源,是一个Topic或者Queue对象。

JMSDeliveryMode
信息交付模式,分为永久的和非永久的,永久的情况,jms收到消息,就持久化,防止丢失,而非永久的JMS中间件要是挂了,就呵呵吧。

int deliverymode = message.getJMSDeliveryMode();
if (deliverymode == javax.jms.DeliveryMode.PERSISTENT) {
...
} else { // equals DeliveryMode.NON_PERSISTENT
...
}

如上方式可以判断是否永久。
设置非永久性消息是发送者设置的。
TopicPublisher topicPublisher = topicSession.createPublisher(topic);
topicPublisher.setDeliveryMode(DeliveryMode.NON_PERSISTENT);

JMSMessageID
消息ID,唯一标示,在做历史库信息的时候很有用。
String messageid = message.getJMSMessageID();

JMSTimestamp
长整型,而不是时间形,用来确定中间件收到的时间(不是发送者发送的时间)。
long timestamp = message.getJMSTimestamp();

JMSExpiration
消息过期时间。
long timeToLive = message.getJMSExpiration();
,也是通过发送者设置过期时间
topicPublisher.setTimeToLive(3600000);。
0的时候,不会过期。

JMSRedelivered
是否重新发送boolean isRedelivered = message.getJMSRedelivered()

JMSPriority
设置优先级0-4是正常级别,5-9是高优先级别,
int priority = message.getJMSPriority();

TopicPublisher topicPublisher = TopicSession.createPublisher(someTopic);
topicPublisher.setPriority(9);

另外消息发送出去后,setPriority将会被忽略。

JMSReplyTo
一些情况下,需要再消费者收到消息后,进行回复
//消息生产者进行回复设置,需要定义一个Destination,来帮助消费者回复
message.setJMSReplyTo(topic);

//消息消费者
Topic topic = (Topic) message.getJMSReplyTo();//获取生产者的信息

JMSCorrelationID
对于需要回复的情况下,该值会与发送者的消息的MessageId相同,来表示对哪个消息回复。当然也不是必须相同

JMSType
程序员可以通过该字段,来定义数据类型等等。


属性
属性支持的类型有String, boolean, byte, double, int, long, or float.
分为三类,应用程序特定属性,JMS定义属性,中间件提供商属性。

应用程序特定属性
由开发人员设置,通过message设置
message.setStringProperty("username",username);更多的类型设置和JDBC很像。

另外setObjectProperty() and getObjectProperty()两个方法,只能设置原生类或者对应的封装类,以及String,其他的无法传递。消息发送后,无法修改,如果修改会抛出MessageNotWriteableException。当然可以通过调用clearProperties方法,来清除属性,清除后可以进行修改。

JMS定义的属性
• JMSXUserID
• JMSXAppID
• JMSXProducerTXID
• JMSXConsumerTXID
• JMSXRcvTimestamp
• JMSXDeliveryCount
• JMSXState
• JMSXGroupID
• JMSXGroupSeq

JMSXGroupID and JMSXGroupSeq
这两个属性,根据JMS标准,JMS中间件必须实现的,其他可选。

中间件供应商提供的属性
根据供应商自己决定。

消息
TextMessage, StreamMessage, MapMessage, ObjectMessage, and BytesMessage
这六种类型是必须实现的。

Message类型是基础接口,session提供createMessage();该方法返回一个只有头‘,属性,但内容是空的消息。

消息发送后,会变成只读,可以通过clearBody,使得重新可以写。

客户确认模式
TopicSession topic =
topicConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
当使用该模式,那么收到消息后,需要明确告诉JMS,消息被收到。message.acknowledge();

猜你喜欢

转载自liyixing1.iteye.com/blog/2122661
jms