1

I am adding the custom object(Account) into Cache and then trying to access the object in the Function.execute() method.

But it throws org.apache.geode.pdx.internal.PdxInstanceImpl cannot be cast to com.sas.cpm.model.Account.

Custom object Account.java

public class Account implements PdxSerializable, Declarable{

public Account() {
          super();
          // TODO Auto-generated constructor
     }

@Override
       public void fromData(PdxReader pr) {…..}

@Override
       public void toData(PdxWriter pw) {… }
}

Client code:

ClientCache cache = new ClientCacheFactory()
            .addPoolLocator("localhost", 10334).set("log-level", "INFO").create();

    // create a local region that matches the server region
// Account is the domain object 
    Region<String, Account> region =
        cache.<String, Account>createClientRegionFactory(ClientRegionShortcut.CACHING_PROXY)
            .create("testRegion");

feedData(region);   //add Account object to region
Execution execution = FunctionService.onRegion(region);
ResultCollector<Integer, List> rc = execution.execute("UpdateCost");//.ID);//com.sas.cpm.geode

Function class : UpdateCost.java

public class UpdateCost implements Function{
   @Override
            public void execute(FunctionContext context) {

                 RegionFunctionContext regionContext = (RegionFunctionContext) context;
                 Region<String, Account> region = regionContext.getDataSet();
                for ( Map.Entry<String, Account> entry : region.entrySet() ) {
                        Account account = entry.getValue();                   /// THIS LINE GIVES THE ERROR
                }

        }

 }

Error:

Exception in thread "main" org.apache.geode.cache.execute.FunctionException: org.apache.geode.cache.client.ServerOperationException: remote server on dsinsbb01ina4(46560:loner):59343:2f7e3885: While performing a remote executeRegionFunction at org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeOnServer(ServerRegionFunctionExecutor.java:229) at org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeFunction(ServerRegionFunctionExecutor.java:178) at org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.execute(ServerRegionFunctionExecutor.java:379) at geodeproject1.Example.funcUpdateExec(Example.java:186) at geodeproject1.Example.main(Example.java:68) Caused by: org.apache.geode.cache.client.ServerOperationException: remote server on dsinsbb01ina4(46560:loner):59343:2f7e3885: While performing a remote executeRegionFunction at org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp$ExecuteRegionFunctionOpImpl.processResponse(ExecuteRegionFunctionOp.java:606) at org.apache.geode.cache.client.internal.AbstractOp.processResponse(AbstractOp.java:225) at org.apache.geode.cache.client.internal.AbstractOp.attemptReadResponse(AbstractOp.java:198) at org.apache.geode.cache.client.internal.AbstractOp.attempt(AbstractOp.java:386) at org.apache.geode.cache.client.internal.ConnectionImpl.execute(ConnectionImpl.java:269) at org.apache.geode.cache.client.internal.pooling.PooledConnection.execute(PooledConnection.java:325) at org.apache.geode.cache.client.internal.OpExecutorImpl.executeWithPossibleReAuthentication(OpExecutorImpl.java:892) at org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:171) at org.apache.geode.cache.client.internal.PoolImpl.execute(PoolImpl.java:772) at org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp.execute(ExecuteRegionFunctionOp.java:162) at org.apache.geode.cache.client.internal.ServerRegionProxy.executeFunction(ServerRegionProxy.java:732) at org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeOnServer(ServerRegionFunctionExecutor.java:220) ... 4 more Caused by: org.apache.geode.cache.execute.FunctionException: java.lang.ClassCastException: org.apache.geode.pdx.internal.PdxInstanceImpl cannot be cast to com.sas.cpm.model.Account at org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp$ExecuteRegionFunctionOpImpl.processResponse(ExecuteRegionFunctionOp.java:583) ... 15 more Caused by: java.lang.ClassCastException: org.apache.geode.pdx.internal.PdxInstanceImpl cannot be cast to com.sas.cpm.model.Account at com.sas.cpm.geode.UpdateCost.execute(UpdateCost.java:49) at org.apache.geode.internal.cache.execute.AbstractExecution.executeFunctionLocally(AbstractExecution.java:331) at org.apache.geode.internal.cache.execute.AbstractExecution$2.run(AbstractExecution.java:300) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at org.apache.geode.distributed.internal.ClusterDistributionManager.runUntilShutdown(ClusterDistributionManager.java:949) at org.apache.geode.distributed.internal.ClusterDistributionManager.doFunctionExecutionThread(ClusterDistributionManager.java:803) at org.apache.geode.internal.logging.LoggingThreadFactory.lambda$newThread$0(LoggingThreadFactory.java:121) at java.lang.Thread.run(Unknown Source)

Kapil
  • 817
  • 2
  • 13
  • 25
Subhash
  • 11
  • 2

1 Answers1

0

The ClassCastException is thrown because you're receiving a PdxInstance from the cache instead of an Account, this happens when you implement the PdxSerializable interface in your domain object and configure PDX Serialization with read-serialized=true.

Please have a look at Implementing PdxSerializable in Your Domain Object and Programming Your Application to Use PdxInstances for further details. Cheers.

Juan Ramos
  • 1,421
  • 1
  • 8
  • 13