List of Interfaced-Objects not being set at serialized

A

andrewfsears

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
 
D

Daniel Pitts

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.
 
A

andrewfsears

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.
 
P

Piotr Kobzda

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
 
A

andrewfsears

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
 
A

andrewfsears

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
 

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

Forum statistics

Threads
473,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top