org.fusesource.stompjms.message.StompJmsObjectMessage Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of stompjms-client Show documentation
Show all versions of stompjms-client Show documentation
STOMP-JMS is a JMS implementation using STOMP as the wire protocol
/**
* Copyright (C) 2010-2011, FuseSource Corp. All rights reserved.
*
* http://fusesource.com
*
* The software in this package is published under the terms of the
* CDDL license a copy of which has been included with this distribution
* in the license.txt file.
*/
package org.fusesource.stompjms.message;
import org.fusesource.hawtbuf.Buffer;
import org.fusesource.stompjms.channel.StompTranslator;
import javax.jms.JMSException;
import javax.jms.ObjectMessage;
import java.io.Serializable;
/**
* An ObjectMessage
object is used to send a message that contains
* a serializable object in the Java programming language ("Java object"). It
* inherits from the Message
interface and adds a body containing a
* single reference to an object. Only Serializable
Java objects
* can be used.
*
*
* If a collection of Java objects must be sent, one of the
* Collection
classes provided since JDK 1.2 can be used.
*
*
* When a client receives an ObjectMessage
, it is in read-only
* mode. If a client attempts to write to the message at this point, a
* MessageNotWriteableException
is thrown. If
* clearBody
is called, the message can now be both read from and
* written to.
*
* @openwire:marshaller code="26"
* @see javax.jms.Session#createObjectMessage()
* @see javax.jms.Session#createObjectMessage(Serializable)
* @see javax.jms.BytesMessage
* @see javax.jms.MapMessage
* @see javax.jms.Message
* @see javax.jms.StreamMessage
* @see javax.jms.TextMessage
*/
public class StompJmsObjectMessage extends StompJmsMessage implements ObjectMessage {
protected transient Serializable object;
public JmsMsgType getMsgType() {
return JmsMsgType.OBJECT;
}
public StompJmsMessage copy() throws JMSException {
StompJmsObjectMessage copy = new StompJmsObjectMessage();
copy(copy);
return copy;
}
private void copy(StompJmsObjectMessage copy) throws JMSException {
super.copy(copy);
storeContent();
copy.object = null;
}
public void storeContent() throws JMSException {
Buffer buffer = getContent();
if (buffer == null && object != null) {
setContent(StompTranslator.writeBufferFromObject(object));
}
}
/**
* Clears out the message body. Clearing a message's body does not clear its
* header values or property entries.
*
*
* If this message body was read-only, calling this method leaves the
* message body in the same state as an empty body in a newly created
* message.
*
* @throws JMSException if the JMS provider fails to clear the message body due to
* some internal error.
*/
public void clearBody() throws JMSException {
super.clearBody();
this.object = null;
}
/**
* Sets the serializable object containing this message's data. It is
* important to note that an ObjectMessage
contains a snapshot
* of the object at the time setObject()
is called; subsequent
* modifications of the object will have no effect on the
* ObjectMessage
body.
*
* @param newObject the message's data
* @throws JMSException if the JMS provider fails to set the object due to some
* internal error.
* @throws javax.jms.MessageFormatException
* if object serialization fails.
* @throws javax.jms.MessageNotWriteableException
* if the message is in read-only mode.
*/
public void setObject(Serializable newObject) throws JMSException {
checkReadOnlyBody();
this.object = newObject;
setContent(null);
storeContent();
}
/**
* Gets the serializable object containing this message's data. The default
* value is null.
*
* @return the serializable object containing this message's data
* @throws JMSException
*/
public Serializable getObject() throws JMSException {
Buffer buffer = getContent();
if (this.object == null && buffer != null) {
this.object = (Serializable) StompTranslator.readObjectFromBuffer(buffer);
}
return this.object;
}
public String toString() {
try {
getObject();
} catch (JMSException e) {
}
return super.toString();
}
}