jms-message对象

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

[b]Message[/b]
消息由三部分组成

[img]http://dl2.iteye.com/upload/attachment/0101/5210/0dc67c23-afdf-388c-8ab2-ecd69a00c64b.png[/img]
头,属性和内容。

[b]头[/b]
所有获取头内容的方法都一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对象设置。

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

[b]JMSDeliveryMode[/b]
信息交付模式,分为永久的和非永久的,永久的情况,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);

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

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

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

[b]JMSRedelivered[/b]
是否重新发送boolean isRedelivered = message.getJMSRedelivered()

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

[b]客户确认模式[/b]
TopicSession topic =
topicConnection.createTopicSession(false, Session.CLIENT_ACKNOWLEDGE);
当使用该模式,那么收到消息后,需要明确告诉JMS,消息被收到。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、付费专栏及课程。

余额充值