RMI Serialization EventListener

Hello,

I got a big problem with serializing EventListeners.

The problem is:

I want to send Java-Swing Elements via RMI. If the Swing Components dont have Listeners its no problem, but if there is a listener at a Component I always get the following exception:

java.lang.ClassCastException: server.ActionTryImpl_Stub cannot be cast to java.util.EventListener

java.lang.ClassCastException: server.ActionTryImpl_Stub cannot be cast to java.util.EventListener

at javax.swing.event.EventListenerList.readObject(EventListenerList.java:255)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)

at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)

at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:1945)

at java.io.ObjectInputStream.defaultReadObject(ObjectInputStream.java:480)

at javax.swing.JComponent.readObject(JComponent.java:5382)

at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)

at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)

at java.lang.reflect.Method.invoke(Method.java:597)

at java.io.ObjectStreamClass.invokeReadObject(ObjectStreamClass.java:974)

at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:1846)

at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1753)

at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1329)

at java.io.ObjectInputStream.readObject(ObjectInputStream.java:351)

at sun.rmi.server.UnicastRef.unmarshalValue(UnicastRef.java:306)

at sun.rmi.server.UnicastRef.invoke(UnicastRef.java:155)

at server.ActionTryImpl_Stub.getJComponent(ActionTryImpl_Stub.java:56)

at client.TransferClient.main(TransferClient.java:33)

The classes I have implement for example an ActionListener. But when I look at the stubs which are generated they only implement the Interfaces which extend Remote, but no ActionListener......I dont know if that is the problem. And I dont know how to fix this problem wether it is the missing ActionListener in the stub or not.

Does anybody has experience with serializing Listeners, or would try this on his own machine just to know where my problem is. Is really urgent, because its for a work at my university. Im very happy for every hint you can give me.

Thank you!

[2994 byte] By [Sandmann6a] at [2007-11-27 10:25:59]
# 1

I got a big problem with serializing EventListeners.

Yes you do.

Rule number one: You cannot pass an object in RMI that does not implement java.io.Serializable

Generally you do not pass Swing elements in RMI, you take the elements out of Swing (TextFields, etc) and put them into String, int etc. and pass those elements.

Remote objects run in a different JVM. You cannot pass references (pointers [addresses]) from one JVM to another JVM.

Handle your Swing work locally.

cooper6a at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...
# 2

OK, thank you for your reply.

Please dont ask why I have to send JComponents over the net via RMI....I just have to :-)

So do I see this right: There is no chance to serialize a JComponent which comes with an ActionListener?

Is the only solution to analyse the JComponent at one side and put the information in an XML Document , then send this document via rmi over the net and rebuild the JComponent with Listeners on the other side?

Thanks for answering these questions!

Sandmann6a at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...
# 3

Rule number two: Read rule number one.

Do the XML Classes implement java.io.Serializable ? I don't think so.

You still seem to be missing the point of Remote Procedure Calls (RMI is Java's version.)

A process here wishes to execute some code on another process. In the limited space here, I cannot go into the details of how this works.

In simple terms, you cannot have a Listener on the other process interacting with the calling process. You're missing the purpose of RMI.

People sometimes have trouble using a database with RMI. They try to pass a ResultSet from one process to another. The way to do it is to take the elements out of the ResultSet and put them into a Class the implements java.io.Serializable and then pass that Class to the other process.

This is probably what you need to do.

cooper6a at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...
# 4

what is the point in passing a listener across RMI? You expect to hear the event remotely?

_dnoyeBa at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...
# 5

Of couse I can tell you the main problem I have.

OK:

I have a system on one side that on exists of an empty JFame.

The other sides are different components that all have their own GUI-elements with implemented listeners.

An nice example for something like zhat in reality would be:

You have an remote control with an initially empty screen. Now when you turn for example your DVD-Player on, you get the GUI that comes with your DVD player on the screen of your remote control. When you turn your TV on, you get the GUI element of the TV on your remote control. This should happen via RMI in my example.

I hope my problem gets a bit clearer now. Of course the GUI elements have (for example an JPanel with several JButtons) several Listeners and I want to react to the JButtons if they are pressed on my Remote Control.

The main problem I have know is to serialize for example an ActionListener. The JComponents are no problem, they can be send via RMI.......but the Listeners.

Its for a thesis on my university. And i need an answer pretty quick, because time is running out.

The main thing I have to know: THE WHY? Why are they not serializable? I have a very easy example: I have a class which implements ActionListener. If I want to send this ActionListener via RMI, I get the Exception shown above.

Thanks much for your help

Sandmann6a at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...
# 6

You have been given the answer.

cooper6a at 2007-7-28 17:36:48 > top of Java-index,Core,Core APIs...