User: chirino
Date: 01/08/17 15:12:50
Modified: src/main/org/jboss/mq SpyObjectMessage.java
Log:
First pass at attempting to optimize the case where an ObjectMessage never
needs to be serialized if it is only traveling in the same VM. May need
to be tweeked to account for different ClassLoaders in the same VM.
Revision Changes Path
1.3 +84 -71 jbossmq/src/main/org/jboss/mq/SpyObjectMessage.java
Index: SpyObjectMessage.java
===================================================================
RCS file: /cvsroot/jboss/jbossmq/src/main/org/jboss/mq/SpyObjectMessage.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- SpyObjectMessage.java 2001/08/17 03:04:01 1.2
+++ SpyObjectMessage.java 2001/08/17 22:12:50 1.3
@@ -1,10 +1,5 @@
-/*
- * JBossMQ, the OpenSource JMS implementation
- *
- * Distributable under LGPL license.
- * See terms of license at gnu.org.
- */
package org.jboss.mq;
+
import java.io.*;
import javax.jms.JMSException;
import javax.jms.MessageFormatException;
@@ -17,73 +12,54 @@
*
* @author Norbert Lataille ([EMAIL PROTECTED])
* @created August 16, 2001
- * @version $Revision: 1.2 $
+ * @version $Revision: 1.3 $
*/
public class SpyObjectMessage
extends SpyMessage
implements ObjectMessage, Externalizable {
// Attributes ----------------------------------------------------
-
- boolean isByteArray = false;
+ // We may find out we have to wrap the object in a marshalled object..
+ //java.rmi.MarshalledObject theObject;
+ Serializable theObject=null;
+ boolean isSerialized = false;
+ boolean isByteArray = false;
byte[] objectBytes = null;
private final static long serialVersionUID = 8809953915407712952L;
- // Public --------------------------------------------------------
-
- public void setObject( Serializable object )
- throws JMSException {
- if ( msgReadOnly ) {
- throw new MessageNotWriteableException( "setObject" );
- }
- try {
- if ( object instanceof byte[] ) {
- //cheat for byte arrays
- isByteArray = true;
- objectBytes = new byte[( ( byte[] )object ).length];
- System.arraycopy( object, 0, objectBytes, 0, objectBytes.length );
- } else {
- ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
- ObjectOutputStream objectOut = new ObjectOutputStream( byteArray );
- objectOut.writeObject( object );
- objectBytes = byteArray.toByteArray();
- objectOut.close();
- }
- } catch ( IOException e ) {
- throw new MessageFormatException( "Object cannot be serialized" );
- }
- }
-
- public Serializable getObject()
- throws JMSException {
-
- Serializable retVal = null;
- try {
- if ( null != objectBytes ) {
- if ( isByteArray ) {
- retVal = new byte[objectBytes.length];
- System.arraycopy( objectBytes, 0, retVal, 0, objectBytes.length );
- } else {
- ObjectInputStream input = new ObjectInputStream( new
ByteArrayInputStream( objectBytes ) );
- retVal = ( Serializable )input.readObject();
- input.close();
- }
- }
- } catch ( ClassNotFoundException e ) {
- throw new MessageFormatException( "ClassNotFoundException" );
- } catch ( IOException e ) {
- throw new MessageFormatException( "IOException" );
- }
- return retVal;
- }
-
public void clearBody()
throws JMSException {
objectBytes = null;
+ theObject = null;
+ isSerialized = false;
super.clearBody();
}
-
+ public Serializable getObject()
+ throws JMSException {
+
+ // Have we not deserialized the object yet???
+ if( theObject==null && objectBytes!=null ) {
+ try {
+ if ( null != objectBytes ) {
+ if ( isByteArray ) {
+ theObject = new byte[objectBytes.length];
+ System.arraycopy( objectBytes, 0, theObject, 0,
objectBytes.length );
+ } else {
+ ObjectInputStream input = new ObjectInputStream( new
ByteArrayInputStream( objectBytes ) );
+ theObject = ( Serializable )input.readObject();
+ input.close();
+ }
+ }
+ } catch ( ClassNotFoundException e ) {
+ throw new MessageFormatException( "ClassNotFoundException" );
+ } catch ( IOException e ) {
+ throw new MessageFormatException( "IOException" );
+ }
+ }
+
+ return theObject;
+ }
public SpyMessage myClone()
throws JMSException {
SpyObjectMessage result = new SpyObjectMessage();
@@ -95,19 +71,6 @@
}
return result;
}
-
- public void writeExternal( java.io.ObjectOutput out )
- throws java.io.IOException {
- super.writeExternal( out );
- out.writeBoolean( isByteArray );
- if ( objectBytes == null ) {
- out.writeInt( -1 );
- } else {
- out.writeInt( objectBytes.length );
- out.write( objectBytes );
- }
- }
-
public void readExternal( java.io.ObjectInput in )
throws java.io.IOException, ClassNotFoundException {
super.readExternal( in );
@@ -120,5 +83,55 @@
in.readFully( objectBytes );
}
}
+ // Public --------------------------------------------------------
+
+ public void setObject( Serializable object )
+ throws JMSException {
+ if ( msgReadOnly ) {
+ throw new MessageNotWriteableException( "setObject" );
+ }
+
+ theObject = object;
+ isSerialized = false;
+ objectBytes = null;
+
+ }
+ public void writeExternal( java.io.ObjectOutput out )
+ throws java.io.IOException {
+ super.writeExternal( out );
+ // Check to see if we need to serialize the object
+ if ( !isSerialized && theObject!=null ) {
+
+ Object object = theObject;
+ /*
+ try {
+ object = theObject.get();
+ } catch ( ClassNotFoundException e ) {
+ throw new IOException(e.getMessage());
+ }
+ */
+ if ( object instanceof byte[] ) {
+ //cheat for byte arrays
+ isByteArray = true;
+ objectBytes = new byte[( ( byte[] )object ).length];
+ System.arraycopy( object, 0, objectBytes, 0, objectBytes.length );
+ } else {
+ ByteArrayOutputStream byteArray = new ByteArrayOutputStream();
+ ObjectOutputStream objectOut = new ObjectOutputStream( byteArray );
+ objectOut.writeObject( object );
+ objectBytes = byteArray.toByteArray();
+ objectOut.close();
+ }
+
+ }
+
+ out.writeBoolean( isByteArray );
+ if ( objectBytes == null ) {
+ out.writeInt( -1 );
+ } else {
+ out.writeInt( objectBytes.length );
+ out.write( objectBytes );
+ }
+ }
}
_______________________________________________
Jboss-development mailing list
[EMAIL PROTECTED]
http://lists.sourceforge.net/lists/listinfo/jboss-development