通过Geode的Function.execute()方法访问ClassCastException自定义对象

时间:2019-08-12 10:02:21

标签: geode

我正在将自定义对象(帐户)添加到Cache中,然后尝试通过Function.execute()方法访问该对象。

但是它抛出org.apache.geode.pdx.internal.PdxInstanceImpl cannot be cast to com.sas.cpm.model.Account

自定义对象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) {… }
}

客户代码:

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

函数类: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
                }

        }

 }

错误:

  

线程“主”中的异常org.apache.geode.cache.execute.FunctionException:org.apache.geode.cache.client.ServerOperationException:dsinsbb01ina4(46560:loner)上的远程服务器:59343:2f7e3885:执行远程executeRegionFunction       在org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeOnServer(ServerRegionFunctionExecutor.java:229)       在org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeFunction(ServerRegionFunctionExecutor.java:178)       在org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.execute(ServerRegionFunctionExecutor.java:379)       在geodeproject1.Example.funcUpdateExec(Example.java:186)       在geodeproject1.Example.main(Example.java:68)   由以下原因引起:org.apache.geode.cache.client.ServerOperationException:dsinsbb01ina4上的远程服务器(46560:loner):59343:2f7e3885:执行远程executeRegionFunction时       在org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp $ ExecuteRegionFunctionOpImpl.processResponse(ExecuteRegionFunctionOp.java:606)处       在org.apache.geode.cache.client.internal.AbstractOp.processResponse(AbstractOp.java:225)       在org.apache.geode.cache.client.internal.AbstractOp.attemptReadResponse(AbstractOp.java:198)       在org.apache.geode.cache.client.internal.AbstractOp.attempt(AbstractOp.java:386)       在org.apache.geode.cache.client.internal.ConnectionImpl.execute(ConnectionImpl.java:269)       在org.apache.geode.cache.client.internal.pooling.PooledConnection.execute(PooledConnection.java:325)       在org.apache.geode.cache.client.internal.OpExecutorImpl.executeWithPossibleReAuthentication(OpExecutorImpl.java:892)       在org.apache.geode.cache.client.internal.OpExecutorImpl.execute(OpExecutorImpl.java:171)       在org.apache.geode.cache.client.internal.PoolImpl.execute(PoolImpl.java:772)       在org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp.execute(ExecuteRegionFunctionOp.java:162)       在org.apache.geode.cache.client.internal.ServerRegionProxy.executeFunction(ServerRegionProxy.java:732)       在org.apache.geode.internal.cache.execute.ServerRegionFunctionExecutor.executeOnServer(ServerRegionFunctionExecutor.java:220)       ...另外4个   造成原因:org.apache.geode.cache.execute.FunctionException:java.lang.ClassCastException:org.apache.geode.pdx.internal.PdxInstanceImpl无法转换为com.sas.cpm.model.Account       在org.apache.geode.cache.client.internal.ExecuteRegionFunctionOp $ ExecuteRegionFunctionOpImpl.processResponse(ExecuteRegionFunctionOp.java:583)       ...另外15个   造成原因:java.lang.ClassCastException:org.apache.geode.pdx.internal.PdxInstanceImpl无法转换为com.sas.cpm.model.Account       在com.sas.cpm.geode.UpdateCost.execute(UpdateCost.java:49)       在org.apache.geode.internal.cache.execute.AbstractExecution.executeFunctionLocally处(AbstractExecution.java:331)       在org.apache.geode.internal.cache.execute.AbstractExecution $ 2.run(AbstractExecution.java:300)       在java.util.concurrent.ThreadPoolExecutor.runWorker(未知来源)       在java.util.concurrent.ThreadPoolExecutor $ Worker.run(未知来源)       在org.apache.geode.distributed.internal.ClusterDistributionManager.runUntilShutdown(ClusterDistributionManager.java:949)       在org.apache.geode.distributed.internal.ClusterDistributionManager.doFunctionExecutionThread(ClusterDistributionManager.java:803)       在org.apache.geode.internal.logging.LoggingThreadFactory.lambda $ newThread $ 0(LoggingThreadFactory.java:121)       在java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:0)

抛出ClassCastException的原因是您从缓存中接收到PdxInstance而不是Account,这是在您的域对象中实现PdxSerializable接口时发生的并使用read-serialized=true配置PDX序​​列化。

有关更多详细信息,请查看Implementing PdxSerializable in Your Domain ObjectProgramming Your Application to Use PdxInstances。 干杯。