1

I write a ExchangeMoney class implements Serializable interface . This class has Session (javax.websocket.Session) property. See code below:

import javax.websocket.Session;
import javax.websocket.EncodeException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.io.Serializable;
...
public class ExchangeMoney implements Serializable {

    private static final long serialVersionUID = -4363184570862565571L;

    private static final Logger logger = Logger.getLogger("ExchangeMoney ");

    private Session session;

    ....
}

Error due to session field non serializable. How to resolve this problem.

Vu NG
  • 280
  • 3
  • 20
  • Does the Session really need to stay open when an ExchangeMoney object is serialized? I would be inclined to make the Session field transient, and keep the information needed to connect (URI and endpoint class name) in serializable fields in the same class, so the Session can be reopened after deserialization. – VGR Feb 09 '15 at 18:18
  • 1
    Yes, The Session really need to stay open when The ExchangeMoney object is serialized. Which solution for this situation? I want to keep this Session to communicate with Client Endpoint . If you make the Session field transient and then reopen after deserialization, you will create new Endpoint and new Session. – Vu NG Feb 10 '15 at 18:18
  • This is solution. See this link for more details http://stackoverflow.com/questions/27037570/find-websocket-session-by-id-in-java-ee-7 – Vu NG Feb 11 '15 at 02:07
  • Unfortunately, the solution you mentioned is for single node. I am in the similar situation where client is connected via web-socket. Server receives data from a queue which need to be send to client via web-socket. This all works fine for single node but not with multiple nodes (there is no cluster), since WebSocket session is not serializable hence the delima :(. Thanks for any help. – Aurvoir Mar 05 '15 at 15:57

2 Answers2

1

Check this post. Java Serialization with non serializable parts

Basically you can extend session and implement serializable and override writeObject and readObject methods and try to persist and read the values you are interested in.

Community
  • 1
  • 1
0

Reconsider the design. Why does a serializable object requires a websocket session inside it and if it is required, shouldn't it be transient?

  • Shouldn't make Session transient. Which solution for this situation? I want to keep this Session to communicate with Client Endpoint. – Vu NG Feb 10 '15 at 17:53