Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   Serializing inner classes? (http://www.velocityreviews.com/forums/t145696-serializing-inner-classes.html)

Jonathan Bartlett 08-20-2005 05:14 PM

Serializing inner classes?
 
I'm having trouble serializing inner classes. Here's the issue, and
perhaps you can even find a better way to do this architecturally.

Basically, the goal is to have objects which, when certain methods are
called, send JMS messages to a message processor. I think it would be
easiest to have both sides of the messages in the class file, so for
example, the message processor would look for an interface called
ActionMessage, with a single function process(). The general form would be

while there are messages to be processed
get next message
call process() on that message

The main classes themselves would look like this:

public interface ActionMessage extends Serializable {
public void process();
}

public class WhateverClass {

public void doSomething() {
//do something local here
//Setup q connection
ActionMessage am =
new ActionMessage() {
public void process() {
//do something remote here
}
};

//note -- session is a QueueSession
ObjectMessage msg = session.createObjectMessage();
msg.setObject(am);
sender.send(msg)
}

This always throws a exception saying "Object cannot be serialized".

Any ideas? So far I've been testing it even with a completely empty
ActionMessage and it still fails.

If anyone has a better idea of how to accomplish this in general, it
would also be appreciated.

Jon
----
Learn to program using Linux assembly language
http://www.cafeshops.com/bartlettpublish.8640017

Andrew McDonagh 08-20-2005 06:28 PM

Re: Serializing inner classes?
 
Jonathan Bartlett wrote:
> I'm having trouble serializing inner classes. Here's the issue, and
> perhaps you can even find a better way to do this architecturally.
>
> Basically, the goal is to have objects which, when certain methods are
> called, send JMS messages to a message processor. I think it would be
> easiest to have both sides of the messages in the class file, so for
> example, the message processor would look for an interface called
> ActionMessage, with a single function process(). The general form would be
>
> while there are messages to be processed
> get next message
> call process() on that message
>
> The main classes themselves would look like this:
>
> public interface ActionMessage extends Serializable {
> public void process();
> }
>
> public class WhateverClass {
>
> public void doSomething() {
> //do something local here
> //Setup q connection
> ActionMessage am =
> new ActionMessage() {
> public void process() {
> //do something remote here
> }
> };
>
> //note -- session is a QueueSession
> ObjectMessage msg = session.createObjectMessage();
> msg.setObject(am);
> sender.send(msg)
> }
>
> This always throws a exception saying "Object cannot be serialized".
>
> Any ideas? So far I've been testing it even with a completely empty
> ActionMessage and it still fails.
>
> If anyone has a better idea of how to accomplish this in general, it
> would also be appreciated.
>
> Jon
> ----
> Learn to program using Linux assembly language
> http://www.cafeshops.com/bartlettpublish.8640017



Its because when the inner class is being serialized, its parent class
(WhateverClass) is also trying to be serialized and because
WhateverClass is not serialisable, the inner class can't be serialized.....

Use a named static inner class, this will break the relationship to the
parent (WhateverClass) class.






All times are GMT. The time now is 12:01 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.