Serializing inner classes?

J

Jonathan Bartlett

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
 
A

Andrew McDonagh

Jonathan said:
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


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top