package org.palm.hazelcast.config;

import java.util.Map;

import com.hazelcast.config.ClasspathXmlConfig;
import com.hazelcast.config.Config;
import com.hazelcast.config.MapConfig;
import com.hazelcast.config.NetworkConfig;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;

/**
 * 展示如何读取配置，如何引入配置
 * @author chkui
 */
public class HazelcastConfigSimple {
	
	// 配置文件存放路径
	final static String DEF_SIMPLE_CONFIG_FILE = "xmlconfig/simple-config.xml";
	
	public static void main(String[] args) {
		// 加载配置文件
		Config config = new ClasspathXmlConfig(DEF_SIMPLE_CONFIG_FILE);
		
		// 获取网络配置
		NetworkConfig netConfig = config.getNetworkConfig();
		
		// 获取用户定义的map配置
		MapConfig mapConfigXml = config.getMapConfig("demo.config");

		// 获取系统默认的map配置
		MapConfig mapConfigDefault = config.getMapConfig("default");
		
		// 输出集群监听的起始端口号
		System.out.println("Current port:" + netConfig.getPort());
		
		// 输出监听端口的累加号
		System.out.println("Current port count:" + netConfig.getPortCount());
		
		// 输出自定义map的备份副本个数
		System.out.println("Config map backup count:" + mapConfigXml.getBackupCount());
		
		// 输出默认map的备份副本个数
		System.out.println("Default map backup count:" + mapConfigDefault.getBackupCount());
		
		HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config);
		HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config);
		
		Map<Integer, String> defaultMap1 = instance1.getMap("defaultMap");
		defaultMap1.put(1, "testMap");
		Map<Integer, String> configMap1 = instance1.getMap("configMap");
		configMap1.put(1, "configMap");
		
		Map<Integer, String> testMap2 = instance2.getMap("defaultMap");
		System.out.println("Default map value:" + testMap2.get(1));
		Map<Integer, String> configMap2 = instance2.getMap("configMap");
		System.out.println("Config map value:" + configMap2.get(1));
	}
}

@@ -318,18 +318,18 @@
 %E5%A6%82%E4%BD%95%E8%AF%BB%E5%8F%96
-%E9%85%8D%E7%BD%AE%EF%BC%8C%E5%A6%82%E4%BD%95
+%E5%92%8C
 %E5%BC%95%E5%85%A5%E9%85%8D%E7%BD%AE%0A
+ * %0A
  * @
@@ -382,18 +382,17 @@
 imple %7B%0A
-%09
 %0A
+
 %09// %E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6
@@ -473,18 +473,17 @@
 g.xml%22;%0A
-%09
 %0A
+
 %09public 
@@ -594,19 +594,16 @@
 _FILE);%0A
-%09%09%0A
 %09%09// %E8%8E%B7%E5%8F%96%E7%BD%91
@@ -661,19 +661,16 @@
 nfig();%0A
-%09%09%0A
 %09%09// %E8%8E%B7%E5%8F%96%E7%94%A8
@@ -742,17 +742,16 @@
 nfig%22);%0A
-%0A
 %09%09// %E8%8E%B7%E5%8F%96%E7%B3%BB
@@ -823,19 +823,16 @@
 ault%22);%0A
-%09%09%0A
 %09%09// %E8%BE%93%E5%87%BA%E9%9B%86
@@ -898,27 +898,24 @@
 getPort());%0A
-%09%09%0A
 %09%09// %E8%BE%93%E5%87%BA%E7%9B%91%E5%90%AC%E7%AB%AF%E5%8F%A3%E7%9A%84
@@ -986,27 +986,24 @@
 rtCount());%0A
-%09%09%0A
 %09%09// %E8%BE%93%E5%87%BA%E8%87%AA%E5%AE%9A%E4%B9%89ma
@@ -1093,19 +1093,16 @@
 unt());%0A
-%09%09%0A
 %09%09// %E8%BE%93%E5%87%BA%E9%BB%98
@@ -1200,19 +1200,17 @@
 unt());%0A
-%09%09
 %0A
+
 %09%09Hazelc
@@ -1345,19 +1345,17 @@
 onfig);%0A
-%09%09
 %0A
+
 %09%09Map%3CIn
@@ -1549,18 +1549,16 @@
 gMap%22);%0A
-%09%09
 %0A%09%09Map%3CI

add hazelcast config demo