这是我的客户端代码:
ClientCache cache = new ClientCacheFactory()
.addPoolLocator("<hostname>", 10334)
.set("log-level", "WARN")
.create();
Execution execution2 = FunctionService.onServers(cache);
ArrayList argList = new ArrayList();
argList.add("region_new");
RegionAttributes attr = new AttributesFactory().create();
argList2.add(attr);
Function function = new CreateRegionFunction();
FunctionService.registerFunction(function);
Object result = execution.setArguments(argList).execute(function).getResult();
我真的建议您使用gfsh
或spring data geode来创建该区域。
如果要使用此功能在服务器上创建区域,则需要执行以下步骤:
CreateRegionFunction
的CreateRegionCacheListener
和docs page的jar。gfsh deploy
命令或在启动服务器时将jar添加到类路径中。FunctionService.registerFunction(function);
同样,我认为使用gfsh
来创建区域要容易得多。
CreateRegionFunction
public CreateRegionFunction() {
this.cache = CacheFactory.getAnyInstance();
this.regionAttributesMetadataRegion =
createRegionAttributesMetadataRegion();
}
clientcache
并像我一样使用execution.execute
运行该函数,该函数将不会在服务器上运行。_regionAttributesMetadata
。我们可以直接创建一个区域,如下所示:
public void execute(FunctionContext context) {
cache = CacheFactory.getAnyInstance();
regionAttributesMetadataRegion =
createRegionAttributesMetadataRegion();
ArrayList arguments = (ArrayList) context.getArguments();
String regionName = (String) arguments.get(0);
PartitionAttributes partition = new
PartitionAttributesFactory().setColocatedWith("/region1").create();
Region region = cache.createRegionFactory().setDataPolicy(DataPolicy.PERSISTENT_PARTITION
).setPartitionAttributes(partition).create(regionName);
// Return status
context.getResultSender().lastResult(region.toString());
}
您收到错误的原因是因为您在构造函数中调用了CacheFactory.getAnyInstance()
和createRegionAttributesMetadataRegion()
。你从客户那里调用CreateRegionFunction()
构造函数吧。所以你得到你的ClientCache
的实例,而不是Cache
。之后,您尝试在ClientCache中创建RegionAttributesMetadataRegion
,这是不受支持的。
一旦你在execute
函数中移动它,它就不会在构造函数(在你的客户端中)被调用,而只会在服务器的execute中调用它。我猜这是Geode文档中的错误。
PS:谢谢你的帖子。我即将放弃动态区域创建,直到我发现它适用于其他人。