List of Interfaced-Objects not being set at serialized

Discussion in 'Java' started by andrewfsears@gmail.com, Jul 20, 2007.

  1. Guest

    I can't seem to get my head around this one. For reasons of having my
    application in a cluster/failover setup, I want to be able to store
    any data that is in session, and have it transferred to the other
    server in case the first goes down...

    So, I've added "implements Serializable" to my objects, and for the
    most part, everything is saved in the session, and loads seamlessly
    after I've shutdown the first server. I'm using a file persistence
    setup, and I can see the values being saved there as well.

    Now, here's the tricky part. There is one type of value that isn't
    being saved in the session. It is a list of objects that is defined
    by an interface. For example:

    <code>
    public class MyClass implements Serializable {
    private static final long serialVersionUID = some_long;
    private List<Shape> shapes;

    // getter and setter for shapes here;
    public addShape(Shape shape) { shapes.add(shape); }
    }

    public interface Shape { ... }

    public class Square implements Shape, Serializable {
    private static final long serialVersionUID = some_long;
    ....
    }

    public class Circle implements Shape, Serializable {
    private static final long serialVersionUID = some_long;
    ....
    }
    </code>

    Now, somewhere along the line, I'll have some commands like (make up
    constructors as I go along here):

    <code>
    MyClass blah = new MyClass("This is my class with 3 items in the
    list of shapes.");
    blah.addShape( new Circle("circle1", 3) );
    blah.addShape( new Square("square1", 5) );
    blah.addShape( new Circle("circle2", 8) );
    </code>

    I kill the server, the session file is created with the instance of
    blah, and the string will be saved as one of the attributes. But the
    three shapes are not there.

    If, instead of List<Shape>, I made it List<Square>, and the three
    "addShape()" lines were all for squares, then they would appear in
    that session file.

    Does anyone have idea why this might be happening, and if so, is there
    a solution out there?

    Thanks in advance, Andy
    , Jul 20, 2007
    #1
    1. Advertising

  2. Daniel Pitts Guest

    On Jul 20, 8:37 am, wrote:
    > I can't seem to get my head around this one. For reasons of having my
    > application in a cluster/failover setup, I want to be able to store
    > any data that is in session, and have it transferred to the other
    > server in case the first goes down...
    >
    > So, I've added "implements Serializable" to my objects, and for the
    > most part, everything is saved in the session, and loads seamlessly
    > after I've shutdown the first server. I'm using a file persistence
    > setup, and I can see the values being saved there as well.
    >
    > Now, here's the tricky part. There is one type of value that isn't
    > being saved in the session. It is a list of objects that is defined
    > by an interface. For example:
    >
    > <code>
    > public class MyClass implements Serializable {
    > private static final long serialVersionUID = some_long;
    > private List<Shape> shapes;
    >
    > // getter and setter for shapes here;
    > public addShape(Shape shape) { shapes.add(shape); }
    >
    > }
    >
    > public interface Shape { ... }
    >
    > public class Square implements Shape, Serializable {
    > private static final long serialVersionUID = some_long;
    > ....
    >
    > }
    >
    > public class Circle implements Shape, Serializable {
    > private static final long serialVersionUID = some_long;
    > ....}
    >
    > </code>
    >
    > Now, somewhere along the line, I'll have some commands like (make up
    > constructors as I go along here):
    >
    > <code>
    > MyClass blah = new MyClass("This is my class with 3 items in the
    > list of shapes.");
    > blah.addShape( new Circle("circle1", 3) );
    > blah.addShape( new Square("square1", 5) );
    > blah.addShape( new Circle("circle2", 8) );
    > </code>
    >
    > I kill the server, the session file is created with the instance of
    > blah, and the string will be saved as one of the attributes. But the
    > three shapes are not there.
    >
    > If, instead of List<Shape>, I made it List<Square>, and the three
    > "addShape()" lines were all for squares, then they would appear in
    > that session file.
    >
    > Does anyone have idea why this might be happening, and if so, is there
    > a solution out there?
    >
    > Thanks in advance, Andy


    You might be able to make Shape extends Serializable. What
    implementation of List are you using, that might also make a
    difference.
    Daniel Pitts, Jul 20, 2007
    #2
    1. Advertising

  3. Guest

    Daniel, thanks for your response.

    It would an ArrayList; so somethinglike:

    <code>
    private List<Shape> shapes;
    ...
    shapes = new ArrayList<Shape>();
    </code>

    Also, thanks for the idea about "public interface Shape extends
    Serializable", but no such luck there.
    , Jul 20, 2007
    #3
  4. Piotr Kobzda Guest

    wrote:

    > Does anyone have idea why this might be happening, and if so, is there
    > a solution out there?


    Possibly there is some bug in your code.

    Try that:

    <code>
    import java.io.*;
    import java.util.*;

    public class Test {

    public static class YourClass implements Serializable {
    private static final long serialVersionUID = 1L;
    private List<Shape> shapes
    = new ArrayList<Shape>();

    public void addShape(Shape shape) { shapes.add(shape); }
    }

    public interface Shape { }

    public static class Square implements Shape, Serializable {
    private static final long serialVersionUID = 1L;
    }

    public static class Circle implements Shape, Serializable {
    private static final long serialVersionUID = 1L;
    }


    public static void main(String[] args) throws Exception {
    YourClass blah = new YourClass();
    blah.addShape( new Circle() );
    blah.addShape( new Square() );
    blah.addShape( new Circle() );

    System.out.println(blah.shapes);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(baos);
    oos.writeObject(blah);
    oos.close();
    ObjectInputStream ois = new ObjectInputStream(
    new ByteArrayInputStream(baos.toByteArray()));
    blah = (YourClass) ois.readObject();

    System.out.println(blah.shapes);
    }
    }
    </code>


    The output on my machine is:

    [Test$Circle@30c221, Test$Square@119298d, Test$Circle@f72617]
    [Test$Circle@cd2c3c, Test$Square@13582d, Test$Circle@21b6d]

    So everything seems to work as expected.


    Could you show us the code that you use to serialize/deserialize your
    objects?


    piotr
    Piotr Kobzda, Jul 20, 2007
    #4
  5. Guest

    Thanks for the response, even though it's been a while...
    Testing it by creating a test that does Input/OutputStream, and even
    writing it to a file and opening it back up, works correctly.

    I'm actually using Tomcat/Apache's clustering setup for maintaining
    session information:

    <Manager className="org.apache.catalina.session.PersistentManager">
    <Store className="org.apache.catalina.session.FileStore" directory="/
    var/local/sessionfiles" />
    </Manager>

    Everything else that is in the serialized object is being read back
    into session once the first one is killed (testing in a 2-instance
    setup). It is just the List of interfaced objects (that are also
    serialized) that are not being read back from the session persisted
    file.

    Any ideas?

    Thanks in advance, Andy
    , Aug 14, 2007
    #5
  6. Guest

    Please ignore all my previous postings... Daniel Pitts' entry was
    correct.

    I obviously didn't post my actual code, and the objects were a lot
    more complicated. And somewhere down the line, one of the interfaces
    didn't extent the Serializable interface and that was the cause of all
    my problems.

    So if someone in the future is searching for a similar problem, I
    highly recommend that you check all your classes/interfaces.

    Thanks to all who submitted responses, Andy
    , Aug 15, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chandrashekar Tippur

    Java Serialized objects

    Chandrashekar Tippur, Apr 25, 2004, in forum: Java
    Replies:
    6
    Views:
    446
    Roedy Green
    Apr 26, 2004
  2. Timo Nentwig
    Replies:
    0
    Views:
    299
    Timo Nentwig
    Feb 15, 2005
  3. rohit
    Replies:
    2
    Views:
    507
    rohit
    May 2, 2005
  4. Will
    Replies:
    3
    Views:
    10,472
  5. eesun

    dislin interfaced with tkinter

    eesun, Aug 3, 2004, in forum: Python
    Replies:
    5
    Views:
    478
    Patrick Ellis
    Aug 4, 2004
Loading...

Share This Page