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