如何修复 Hazelcast 不为 EntryProcessor 抛出 'java.lang.IllegalStateException: User Code Deployment is not enabled'
How to fix Hazelcast from throwing 'java.lang.IllegalStateException: User Code Deployment is not enabled' for EntryProcessor
我正在尝试使用 EntryProcessor 为我们的应用程序设置 Hazelcast 代码。为了分析为什么 EntryProcessor 的代码在我们的应用程序中不起作用,我试图在我的本地机器上设置一个类似的例子来重现这个问题。但是,即使在我可以重现相同的问题之前,我也面临着另一个应该可以重现的问题,如下所示:
- 从 here 下载 Hazelcast IMDG 3.10.6。并解压。
- 创建一个新的 Java 应用程序并添加 Java Client --> EntryProcessor 下给出的代码 main page.
- 在解压的 hazelcast 文件夹的 lib 文件夹下添加 hazelcast-3.10.6 和 hazelcast-client-3.10.6 jar。
- 从 bin 文件夹下的 start.bat 文件启动 hazelcast 成员(服务器)。
- 运行 步骤 2 中给出的 Java 客户端代码。
我也将 Java 客户端代码粘贴在下面以供参考。
package client;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientUserCodeDeploymentConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.AbstractEntryProcessor;
import java.io.Serializable;
import java.util.Map;
public class EntryProcessorSample {
public static class IncEntryProcessor extends AbstractEntryProcessor<String, Integer> implements Serializable {
@Override
public Object process(Map.Entry<String, Integer> entry) {
// Get the value passed
int oldValue = entry.getValue();
// Update the value
int newValue = oldValue + 1;
// Update the value back to the entry stored in the Hazelcast Member this EntryProcessor is running on.
entry.setValue(newValue);
// No need to return anything back to the caller, we can return whatever we like here.
return null;
}
}
public static void main(String[] args) {
// Enable Code Deployment from this Client classpath to the Cluster Members classpath
// User Code Deployment needs to be enabled on the Cluster Members as well.
ClientConfig config = new ClientConfig();
ClientUserCodeDeploymentConfig userCodeDeploymentConfig = config.getUserCodeDeploymentConfig();
userCodeDeploymentConfig.setEnabled(true);
userCodeDeploymentConfig.addClass(EntryProcessorSample.IncEntryProcessor.class);
// Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
HazelcastInstance hz = HazelcastClient.newHazelcastClient(config);
// Get the Distributed Map from Cluster.
IMap<String, Integer> map = hz.getMap("my-distributed-map");
// Put the integer value of 0 into the Distributed Map
map.put("key", 0);
// Run the IncEntryProcessor class on the Hazelcast Cluster Member holding the key called "key"
map.executeOnKey("key", new IncEntryProcessor());
// Show that the IncEntryProcessor updated the value.
System.out.println("new value:" + map.get("key"));
// Shutdown this Hazelcast Client
hz.shutdown();
}
}
我希望代码 运行 没有任何问题,因为在 hazelcast site 中为 Java 客户端提供的地图示例工作得很好。此外,由于我们明确启用了 ClientConfig 的用户代码部署,我不明白为什么我们会遇到这个问题。
您也需要在会员端启用User Code Deployment。
我正在尝试使用 EntryProcessor 为我们的应用程序设置 Hazelcast 代码。为了分析为什么 EntryProcessor 的代码在我们的应用程序中不起作用,我试图在我的本地机器上设置一个类似的例子来重现这个问题。但是,即使在我可以重现相同的问题之前,我也面临着另一个应该可以重现的问题,如下所示:
- 从 here 下载 Hazelcast IMDG 3.10.6。并解压。
- 创建一个新的 Java 应用程序并添加 Java Client --> EntryProcessor 下给出的代码 main page.
- 在解压的 hazelcast 文件夹的 lib 文件夹下添加 hazelcast-3.10.6 和 hazelcast-client-3.10.6 jar。
- 从 bin 文件夹下的 start.bat 文件启动 hazelcast 成员(服务器)。
- 运行 步骤 2 中给出的 Java 客户端代码。
我也将 Java 客户端代码粘贴在下面以供参考。
package client;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.client.config.ClientUserCodeDeploymentConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
import com.hazelcast.map.AbstractEntryProcessor;
import java.io.Serializable;
import java.util.Map;
public class EntryProcessorSample {
public static class IncEntryProcessor extends AbstractEntryProcessor<String, Integer> implements Serializable {
@Override
public Object process(Map.Entry<String, Integer> entry) {
// Get the value passed
int oldValue = entry.getValue();
// Update the value
int newValue = oldValue + 1;
// Update the value back to the entry stored in the Hazelcast Member this EntryProcessor is running on.
entry.setValue(newValue);
// No need to return anything back to the caller, we can return whatever we like here.
return null;
}
}
public static void main(String[] args) {
// Enable Code Deployment from this Client classpath to the Cluster Members classpath
// User Code Deployment needs to be enabled on the Cluster Members as well.
ClientConfig config = new ClientConfig();
ClientUserCodeDeploymentConfig userCodeDeploymentConfig = config.getUserCodeDeploymentConfig();
userCodeDeploymentConfig.setEnabled(true);
userCodeDeploymentConfig.addClass(EntryProcessorSample.IncEntryProcessor.class);
// Start the Hazelcast Client and connect to an already running Hazelcast Cluster on 127.0.0.1
HazelcastInstance hz = HazelcastClient.newHazelcastClient(config);
// Get the Distributed Map from Cluster.
IMap<String, Integer> map = hz.getMap("my-distributed-map");
// Put the integer value of 0 into the Distributed Map
map.put("key", 0);
// Run the IncEntryProcessor class on the Hazelcast Cluster Member holding the key called "key"
map.executeOnKey("key", new IncEntryProcessor());
// Show that the IncEntryProcessor updated the value.
System.out.println("new value:" + map.get("key"));
// Shutdown this Hazelcast Client
hz.shutdown();
}
}
我希望代码 运行 没有任何问题,因为在 hazelcast site 中为 Java 客户端提供的地图示例工作得很好。此外,由于我们明确启用了 ClientConfig 的用户代码部署,我不明白为什么我们会遇到这个问题。
您也需要在会员端启用User Code Deployment。