无法在Geode中从客户端动态创建区域

问题描述 投票:0回答:3
  • 我想从客户端创建一个新的区域,该区域在服务器上不存在。
  • 遵循官方文档动态创建区域,只有服务器端功能,但没有客户端代码。
  • 我尝试通过客户端缓存调用CreateRegionFunction但得到一个错误:线程“main”中的异常java.lang.UnsupportedOperationException:客户端缓存不支持操作

这是我的客户端代码:

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();
geode
3个回答
1
投票

我真的建议您使用gfshspring data geode来创建该区域。

如果要使用此功能在服务器上创建区域,则需要执行以下步骤:

  1. 创建一个包含来自CreateRegionFunctionCreateRegionCacheListenerdocs page的jar。
  2. 确保服务器可以使用jar(使用gfsh deploy命令或在启动服务器时将jar添加到类路径中。
  3. 从客户端代码中删除以下行:FunctionService.registerFunction(function);
  4. 然后运行您的客户端代码。

同样,我认为使用gfsh来创建区域要容易得多。


1
投票
  • 在官方文档中有一个CreateRegionFunction public CreateRegionFunction() { this.cache = CacheFactory.getAnyInstance(); this.regionAttributesMetadataRegion = createRegionAttributesMetadataRegion(); }
  • 这样,如果你创建一个clientcache并像我一样使用execution.execute运行该函数,该函数将不会在服务器上运行。
  • 然后我发现官方doc的方法似乎已经过时了。似乎没有必要创建一个_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()); }

0
投票

您收到错误的原因是因为您在构造函数中调用了CacheFactory.getAnyInstance()createRegionAttributesMetadataRegion()。你从客户那里调用CreateRegionFunction()构造函数吧。所以你得到你的ClientCache的实例,而不是Cache。之后,您尝试在ClientCache中创建RegionAttributesMetadataRegion,这是不受支持的。

一旦你在execute函数中移动它,它就不会在构造函数(在你的客户端中)被调用,而只会在服务器的execute中调用它。我猜这是Geode文档中的错误。

PS:谢谢你的帖子。我即将放弃动态区域创建,直到我发现它适用于其他人。

© www.soinside.com 2019 - 2024. All rights reserved.