本文介紹了如何修復因引發java.lang.IlLegalStateException而引發的Hazelcast:未為EntryProcessor啟用用戶代碼部署的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我正在嘗試使用EntryProcessor為我們的應用程序設置一個用于Hazelcast的代碼。為了分析為什么EntryProcessor的代碼在我們的應用程序中不起作用,我嘗試在我的本地計算機上設置一個類似的示例來重現該問題。然而,即使在我可以復制相同的問題之前,我還面臨著另一個問題,該問題應該如下所示:
-
從here下載Hazelcast IMDG 3.10.6。然后將其解壓縮。
新建Java應用程序并添加main page的Java客戶端–>EntryProcessor下給出的代碼。
將Hazelcast-3.10.6和Hazelcast-Client-3.10.6 Jars添加到解壓縮的Hazelcast文件夾的lib文件夾下。
從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客戶端的Map示例運行得很好。另外,由于我們顯式啟用了客戶端配置的用戶代碼部署,我不明白為什么會出現此問題。
推薦答案
您也需要在成員端開啟User Code Deployment。
這篇關于如何修復因引發java.lang.IlLegalStateException而引發的Hazelcast:未為EntryProcessor啟用用戶代碼部署的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,