Welcome to the new Gigaspaces XAP forum. To recover your account, please follow these instructions.

Ask Your Question

Getting ClassCastException on take

I have one servlet that puts an object into a space.
I have another servlet that takes the object out.
Both servlets have threads that run on one minute timers so there can be some type of timing problem going on.
Intermittently, when I take the object out of the space I get a ClassCastException. The object is not null and I can always print out it's class by doing Object.getClass().getName(). The name matches the expected class name but the cast fails anyway.

Any ideas what might cause the ClassCastException?

This thread was imported from the previous forum.
For your reference, the original is available here

asked 2008-09-26 18:51:29 -0600

tmjennings gravatar image

updated 2013-08-08 09:52:00 -0600

jaissefsfex gravatar image
edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

There is no point to use readIfExists when there are no transaction used. The IfExists operations are useful for objects that are part of transactions to allow your operation to take into considerations matching objects that undergo active transaction.

To deal with the exception:Make sure your POJO class is part of a jar located under the PU folder/shared-lib. All classes that interacts with space proxy should be part of a "common" jar that must be under the shared-lib folder. This will load the class in the correct classloader and will allow the space proxy to find a matching class when caching the POJO class methods used when materializing the object after read/take operations (calling POJO class setter methods).


answered 2008-09-29 18:17:04 -0600

shay hassidim gravatar image
edit flag offensive delete link more

sorry, not using take, using readIfExists.

    NetworkLayerHelper networkLayerHelper = null;

    if (space != null)
            NetworkLayerHelper template = new NetworkLayerHelper();

            // Get the completed results from the space (will be null if it doesn't exist)
            Object temp = space.readIfExists(template, null, JavaSpace.NO_WAIT);
            if (temp != null) {
                System.out.println("GigaSpaceHelper::getNetworkLayerHelper: retrieved object of type " + temp.getClass().getName());
                System.out.println("GigaSpaceHelper::getNetworkLayerHelper: retrieved object of canonicalName " + temp.getClass().getCanonicalName());
                //System.out.println("GigaSpaceHelper::getNetworkLayerHelper: classLoader = " + temp.getClass().getClassLoader().toString());
                //if (temp instanceof com.jdsu.videoPortalDataSource.model.NetworkLayerHelper) {
                    System.out.println("GigaSpaceHelper::getNetworkLayerHelper: casting to NetworkLayerHelper");
                    networkLayerHelper = (NetworkLayerHelper) temp;
                //} else {
                //    System.out.println("GigaSpaceHelper::getNetworkLayerHelper: NOT casting to NetworkLayerHelper");
            } else {
                System.out.println("GigaSpaceHelper::getNetworkLayerHelper: retrieved null");
        catch (Exception ex) 
        System.out.println("GigaSpaceHelper::getNetworkLayerHelper - ERROR Space not accessible (space == null)");

answered 2008-09-26 19:06:58 -0600

tmjennings gravatar image
edit flag offensive delete link more



What is the setup? How do you package your servlets, and where do you run them? Is it a web processing unit?

nirpaz gravatar imagenirpaz ( 2008-09-27 00:25:19 -0600 )edit

With gigaspaces readIfExists shouldn't be any issues. If you could print the out put as well it would be a great help.

Try following to see if the returned object is compatible or not

System.out.println("Class type " + o.getClass().getCanonicalName() + " can" + (MyType.class.isAssignableFrom(o.getClass()) ? " " : "'t ") + " be cast to object of type MyType.");

If you are trying to write/read from different JVM's. There might be one issue with serialization version ID in Object. If you didn't define the version ID in pojo you might** get some exceptions Try defining the static version ID in pojo some thing like private static final long serialVersionUID = -3407611419030021619L;

Print output/topology/pojo for more help.

venkatg gravatar imagevenkatg ( 2008-09-29 12:27:00 -0600 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2008-09-26 18:51:29 -0600

Seen: 65 times

Last updated: Sep 29 '08