📄 persistentmessage.java
字号:
/**
* Redistribution and use of this software and associated documentation
* ("Software"), with or without modification, are permitted provided
* that the following conditions are met:
*
* 1. Redistributions of source code must retain copyright
* statements and notices. Redistributions must also contain a
* copy of this document.
*
* 2. Redistributions in binary form must reproduce the
* above copyright notice, this list of conditions and the
* following disclaimer in the documentation and/or other
* materials provided with the distribution.
*
* 3. The name "Exolab" must not be used to endorse or promote
* products derived from this Software without prior written
* permission of Exoffice Technologies. For written permission,
* please contact info@exolab.org.
*
* 4. Products derived from this Software may not be called "Exolab"
* nor may "Exolab" appear in their names without prior written
* permission of Exoffice Technologies. Exolab is a registered
* trademark of Exoffice Technologies.
*
* 5. Due credit should be given to the Exolab Project
* (http://www.exolab.org/).
*
* THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
* FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
* EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*
* Copyright 2000-2001,2003 (C) Exoffice Technologies Inc. All Rights Reserved.
*
* $Id: PersistentMessage.java,v 1.9 2003/08/17 01:32:25 tanderson Exp $
*
* Date Author Changes
*/
package org.exolab.jms.persistence;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInput;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import javax.jms.JMSException;
import org.exolab.core.foundation.PersistentObject;
import org.exolab.jms.message.DestinationImpl;
import org.exolab.jms.message.MessageImpl;
/**
* A wrapper to the Message Object to assist in persistency.
*
*
* @version $Revision: 1.9 $ $Date: 2003/08/17 01:32:25 $
* @author <a href="mailto:mourikis@exolab.org">Jim Mourikis</a>
*/
public class PersistentMessage extends PersistentObject {
// Used for serialization
static final long serialVersionUID = 2;
// The message destination
private String destination_;
// The message id.
private String messageId_;
// The message type, i.e. BytesMessage, TextMessage, StreamMessage etc.
private String messageType_;
// The message priority.
private int priority_;
// The timestamp when the message was received.
private long timeStamp_;
// The message expiry time
private long expiryTime_;
// The message holder.
private MessageImpl message_;
// Indicates whether the message has been processed by the provider
private boolean processed_;
/**
* Default constructor.
*
* @param MessageImpl the message to persist
*
*/
public PersistentMessage() {
super();
}
/**
* Store the message to be persisted. And extract any required column
* details.
*
* @param MessageImpl the message to persist
*
*/
public PersistentMessage(MessageImpl message) throws JMSException {
super();
message_ = message;
destination_ =
((DestinationImpl) message.getJMSDestination()).getDestination();
messageId_ = message.getMessageId().getId();
messageType_ = message.getJMSType();
priority_ = message.getJMSPriority();
expiryTime_ = message.getJMSExpiration();
timeStamp_ = message.getAcceptedTime();
processed_ = message.getProcessed();
}
/**
* Set the message held by this persistent object
*
* @param message - the message held
*/
public void setMessage(MessageImpl message) {
message_ = message;
}
/**
* return the held message from the persistent object.
*
* @return MessageImpl the held message.
*
*/
public MessageImpl getMessage() {
return message_;
}
/**
* Get the message as a serialized blob.
*
* @return String The serialized message.
*
*/
public byte[] getMessageBlob() {
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream tmp = new ObjectOutputStream(bout);
tmp.writeObject(message_);
tmp.close();
return bout.toByteArray();
} catch (Exception err) {
System.err.println
("Error PersistentMessage: Failed to serialize message\n"
+ err);
return null;
}
}
/**
* Set the message from a serialized blob
*
* @param blob The serialized message.
*
*/
public void setMessageBlob(byte[] blob) {
try {
if (blob != null) {
ByteArrayInputStream bin =
new ByteArrayInputStream(blob);
ObjectInputStream tmpin = new ObjectInputStream(bin);
message_ = (MessageImpl) tmpin.readObject();
tmpin.close();
} else {
message_ = null;
}
} catch (Exception err) {
System.err.println
("Error PersistentMessage: Failed to de-serialize message\n"
+ err);
err.printStackTrace();
}
}
/**
* Get the message destination
*
* @return String The destination of the message
*
*/
public String getDestination() {
return destination_;
}
/**
* Set the message destination
*
* @param destination The destination of the message
*
*/
public void setDestination(String destination) {
destination_ = destination;
}
/**
* Get the message id
*
* @return the message id
*
*/
public String getMessageId() {
return messageId_;
}
/**
* Set the message id
*
* @param id The message id
*
*/
public void setMessageId(String id) {
messageId_ = id;
}
/**
* Get the message type
*
* @return String The message type
*
*/
public String getMessageType() {
return messageType_;
}
/**
* Set the message type
*
* @param messageType The message type
*
*/
public void setMessageType(String messageType) {
messageType_ = messageType;
}
/**
* Get the message priority
*
* @return int The priority
*
*/
public int getPriority() {
return priority_;
}
/**
* Set the message priority
*
* @param priority The message priority to set
*
*/
public void setPriority(int priority) {
priority_ = priority;
}
/**
* Get the time the message was received by the MessageMgr
*
* @return long The message creation time in secs since epoc
*
*/
public long getJMSTimeStamp() {
return timeStamp_;
}
/**
* Set the time the message was received by the MessageMgr
*
* @param timeStamp The message creation time in secs since epoc
*
*/
public void setJMSTimeStamp(long time) {
timeStamp_ = time;
}
/**
* Retrieve the message expiry time
*
* @return long - the time that the underlying message expires
*
*/
public long getExpiryTime() {
return expiryTime_;
}
/**
* Set the message expiry time.
*
* @param time - message expiry time in milliseconds
*
*/
public void setExpiryTime(long time) {
expiryTime_ = time;
}
/**
* Set the state of the processed flag
*
* @param value - true if message has been processed
*/
public void setProcessed(boolean value) {
processed_ = value;
}
/**
* Return the state of the processed flag
*
* @return boolean - true if the message has been processed
*/
public boolean getProcessed() {
return processed_;
}
// implementation of Externalizable.writeExternal
public void writeExternal(ObjectOutput stream) throws IOException {
stream.writeLong(serialVersionUID);
stream.writeObject(destination_);
stream.writeUTF(messageId_);
stream.writeObject(messageType_);
stream.writeInt(priority_);
stream.writeLong(timeStamp_);
stream.writeLong(expiryTime_);
stream.writeBoolean(processed_);
stream.writeObject(message_);
super.writeExternal(stream);
}
// implementation of Externalizable.writeExternal
public void readExternal(ObjectInput stream)
throws IOException, ClassNotFoundException {
long version = stream.readLong();
if (version == serialVersionUID) {
destination_ = (String) stream.readObject();
messageId_ = stream.readUTF();
messageType_ = (String) stream.readObject();
priority_ = stream.readInt();
timeStamp_ = stream.readLong();
expiryTime_ = stream.readLong();
processed_ = stream.readBoolean();
message_ = (MessageImpl) stream.readObject();
} else {
throw new IOException("PersistentMessage with version "
+ version + " is not supported.");
}
super.readExternal(stream);
}
} //-- PersistentMessage
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -