├── LICENSE ├── README.md ├── pom.xml └── src └── main ├── java └── org │ └── palm │ └── hazelcast │ ├── config │ ├── HazelcastConfigRuntimeModify.java │ ├── HazelcastConfigSimple.java │ └── HazelcastConfigVariable.java │ ├── getstart │ ├── HazelcastGetStartClient.java │ ├── HazelcastGetStartServerMaster.java │ └── HazelcastGetStartServerSlave.java │ └── map │ ├── config │ ├── StaticMapConfig.java │ └── mapConfig.xml │ ├── getStart │ ├── ClientNode.java │ └── ServiceNode.java │ ├── interceptor │ ├── InterceptorDemo.java │ └── InterceptorExample.java │ ├── listener │ ├── ListenerDemo.java │ └── ListenerExample.java │ └── store │ ├── MapStoreExample.java │ ├── MapStoreExampleMain.java │ └── mapStoreConfig.xml └── resources ├── properties └── variable-config.properties └── xmlconfig ├── simple-config.xml └── variable-config.xml /LICENSE: -------------------------------------------------------------------------------- 1 | Apache License 2 | Version 2.0, January 2004 3 | http://www.apache.org/licenses/ 4 | 5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 6 | 7 | 1. Definitions. 8 | 9 | "License" shall mean the terms and conditions for use, reproduction, 10 | and distribution as defined by Sections 1 through 9 of this document. 11 | 12 | "Licensor" shall mean the copyright owner or entity authorized by 13 | the copyright owner that is granting the License. 14 | 15 | "Legal Entity" shall mean the union of the acting entity and all 16 | other entities that control, are controlled by, or are under common 17 | control with that entity. For the purposes of this definition, 18 | "control" means (i) the power, direct or indirect, to cause the 19 | direction or management of such entity, whether by contract or 20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the 21 | outstanding shares, or (iii) beneficial ownership of such entity. 22 | 23 | "You" (or "Your") shall mean an individual or Legal Entity 24 | exercising permissions granted by this License. 25 | 26 | "Source" form shall mean the preferred form for making modifications, 27 | including but not limited to software source code, documentation 28 | source, and configuration files. 29 | 30 | "Object" form shall mean any form resulting from mechanical 31 | transformation or translation of a Source form, including but 32 | not limited to compiled object code, generated documentation, 33 | and conversions to other media types. 34 | 35 | "Work" shall mean the work of authorship, whether in Source or 36 | Object form, made available under the License, as indicated by a 37 | copyright notice that is included in or attached to the work 38 | (an example is provided in the Appendix below). 39 | 40 | "Derivative Works" shall mean any work, whether in Source or Object 41 | form, that is based on (or derived from) the Work and for which the 42 | editorial revisions, annotations, elaborations, or other modifications 43 | represent, as a whole, an original work of authorship. For the purposes 44 | of this License, Derivative Works shall not include works that remain 45 | separable from, or merely link (or bind by name) to the interfaces of, 46 | the Work and Derivative Works thereof. 47 | 48 | "Contribution" shall mean any work of authorship, including 49 | the original version of the Work and any modifications or additions 50 | to that Work or Derivative Works thereof, that is intentionally 51 | submitted to Licensor for inclusion in the Work by the copyright owner 52 | or by an individual or Legal Entity authorized to submit on behalf of 53 | the copyright owner. For the purposes of this definition, "submitted" 54 | means any form of electronic, verbal, or written communication sent 55 | to the Licensor or its representatives, including but not limited to 56 | communication on electronic mailing lists, source code control systems, 57 | and issue tracking systems that are managed by, or on behalf of, the 58 | Licensor for the purpose of discussing and improving the Work, but 59 | excluding communication that is conspicuously marked or otherwise 60 | designated in writing by the copyright owner as "Not a Contribution." 61 | 62 | "Contributor" shall mean Licensor and any individual or Legal Entity 63 | on behalf of whom a Contribution has been received by Licensor and 64 | subsequently incorporated within the Work. 65 | 66 | 2. Grant of Copyright License. Subject to the terms and conditions of 67 | this License, each Contributor hereby grants to You a perpetual, 68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 69 | copyright license to reproduce, prepare Derivative Works of, 70 | publicly display, publicly perform, sublicense, and distribute the 71 | Work and such Derivative Works in Source or Object form. 72 | 73 | 3. Grant of Patent License. Subject to the terms and conditions of 74 | this License, each Contributor hereby grants to You a perpetual, 75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable 76 | (except as stated in this section) patent license to make, have made, 77 | use, offer to sell, sell, import, and otherwise transfer the Work, 78 | where such license applies only to those patent claims licensable 79 | by such Contributor that are necessarily infringed by their 80 | Contribution(s) alone or by combination of their Contribution(s) 81 | with the Work to which such Contribution(s) was submitted. If You 82 | institute patent litigation against any entity (including a 83 | cross-claim or counterclaim in a lawsuit) alleging that the Work 84 | or a Contribution incorporated within the Work constitutes direct 85 | or contributory patent infringement, then any patent licenses 86 | granted to You under this License for that Work shall terminate 87 | as of the date such litigation is filed. 88 | 89 | 4. Redistribution. You may reproduce and distribute copies of the 90 | Work or Derivative Works thereof in any medium, with or without 91 | modifications, and in Source or Object form, provided that You 92 | meet the following conditions: 93 | 94 | (a) You must give any other recipients of the Work or 95 | Derivative Works a copy of this License; and 96 | 97 | (b) You must cause any modified files to carry prominent notices 98 | stating that You changed the files; and 99 | 100 | (c) You must retain, in the Source form of any Derivative Works 101 | that You distribute, all copyright, patent, trademark, and 102 | attribution notices from the Source form of the Work, 103 | excluding those notices that do not pertain to any part of 104 | the Derivative Works; and 105 | 106 | (d) If the Work includes a "NOTICE" text file as part of its 107 | distribution, then any Derivative Works that You distribute must 108 | include a readable copy of the attribution notices contained 109 | within such NOTICE file, excluding those notices that do not 110 | pertain to any part of the Derivative Works, in at least one 111 | of the following places: within a NOTICE text file distributed 112 | as part of the Derivative Works; within the Source form or 113 | documentation, if provided along with the Derivative Works; or, 114 | within a display generated by the Derivative Works, if and 115 | wherever such third-party notices normally appear. The contents 116 | of the NOTICE file are for informational purposes only and 117 | do not modify the License. You may add Your own attribution 118 | notices within Derivative Works that You distribute, alongside 119 | or as an addendum to the NOTICE text from the Work, provided 120 | that such additional attribution notices cannot be construed 121 | as modifying the License. 122 | 123 | You may add Your own copyright statement to Your modifications and 124 | may provide additional or different license terms and conditions 125 | for use, reproduction, or distribution of Your modifications, or 126 | for any such Derivative Works as a whole, provided Your use, 127 | reproduction, and distribution of the Work otherwise complies with 128 | the conditions stated in this License. 129 | 130 | 5. Submission of Contributions. Unless You explicitly state otherwise, 131 | any Contribution intentionally submitted for inclusion in the Work 132 | by You to the Licensor shall be under the terms and conditions of 133 | this License, without any additional terms or conditions. 134 | Notwithstanding the above, nothing herein shall supersede or modify 135 | the terms of any separate license agreement you may have executed 136 | with Licensor regarding such Contributions. 137 | 138 | 6. Trademarks. This License does not grant permission to use the trade 139 | names, trademarks, service marks, or product names of the Licensor, 140 | except as required for reasonable and customary use in describing the 141 | origin of the Work and reproducing the content of the NOTICE file. 142 | 143 | 7. Disclaimer of Warranty. Unless required by applicable law or 144 | agreed to in writing, Licensor provides the Work (and each 145 | Contributor provides its Contributions) on an "AS IS" BASIS, 146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 147 | implied, including, without limitation, any warranties or conditions 148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A 149 | PARTICULAR PURPOSE. You are solely responsible for determining the 150 | appropriateness of using or redistributing the Work and assume any 151 | risks associated with Your exercise of permissions under this License. 152 | 153 | 8. Limitation of Liability. In no event and under no legal theory, 154 | whether in tort (including negligence), contract, or otherwise, 155 | unless required by applicable law (such as deliberate and grossly 156 | negligent acts) or agreed to in writing, shall any Contributor be 157 | liable to You for damages, including any direct, indirect, special, 158 | incidental, or consequential damages of any character arising as a 159 | result of this License or out of the use or inability to use the 160 | Work (including but not limited to damages for loss of goodwill, 161 | work stoppage, computer failure or malfunction, or any and all 162 | other commercial damages or losses), even if such Contributor 163 | has been advised of the possibility of such damages. 164 | 165 | 9. Accepting Warranty or Additional Liability. While redistributing 166 | the Work or Derivative Works thereof, You may choose to offer, 167 | and charge a fee for, acceptance of support, warranty, indemnity, 168 | or other liability obligations and/or rights consistent with this 169 | License. However, in accepting such obligations, You may act only 170 | on Your own behalf and on Your sole responsibility, not on behalf 171 | of any other Contributor, and only if You agree to indemnify, 172 | defend, and hold each Contributor harmless for any liability 173 | incurred by, or claims asserted against, such Contributor by reason 174 | of your accepting any such warranty or additional liability. 175 | 176 | END OF TERMS AND CONDITIONS 177 | 178 | APPENDIX: How to apply the Apache License to your work. 179 | 180 | To apply the Apache License to your work, attach the following 181 | boilerplate notice, with the fields enclosed by brackets "{}" 182 | replaced with your own identifying information. (Don't include 183 | the brackets!) The text should be enclosed in the appropriate 184 | comment syntax for the file format. We also recommend that a 185 | file or class name and description of purpose be included on the 186 | same "printed page" as the copyright notice for easier 187 | identification within third-party archives. 188 | 189 | Copyright {yyyy} {name of copyright owner} 190 | 191 | Licensed under the Apache License, Version 2.0 (the "License"); 192 | you may not use this file except in compliance with the License. 193 | You may obtain a copy of the License at 194 | 195 | http://www.apache.org/licenses/LICENSE-2.0 196 | 197 | Unless required by applicable law or agreed to in writing, software 198 | distributed under the License is distributed on an "AS IS" BASIS, 199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 200 | See the License for the specific language governing permissions and 201 | limitations under the License. 202 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # hazelcast-demo 2 | hazelcast demo. 3 | -------------------------------------------------------------------------------- /pom.xml: -------------------------------------------------------------------------------- 1 | 3 | 4.0.0 4 | org.palm.hazelcast 5 | hazelcast-demo 6 | 0.0.1-SNAPSHOT 7 | hazelcast-demo 8 | jar 9 | 10 | 3.6.3 11 | 12 | 13 | 14 | com.hazelcast 15 | hazelcast 16 | ${hazelcast.vertsion} 17 | 18 | 19 | com.hazelcast 20 | hazelcast-client 21 | ${hazelcast.vertsion} 22 | 23 | 24 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/config/HazelcastConfigRuntimeModify.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.config; 2 | 3 | import com.hazelcast.config.Config; 4 | import com.hazelcast.config.JoinConfig; 5 | import com.hazelcast.config.MulticastConfig; 6 | import com.hazelcast.config.NetworkConfig; 7 | import com.hazelcast.core.Hazelcast; 8 | 9 | /** 10 | * 展示在代码执行时修改配置初始化方法 11 | * 12 | * @author chkui 13 | */ 14 | public class HazelcastConfigRuntimeModify { 15 | public static void main(String[] args) { 16 | // 创建默认config对象 17 | Config config = new Config(); 18 | 19 | // 获取network元素 20 | NetworkConfig netConfig = config.getNetworkConfig(); 21 | System.out.println("Default port:" + netConfig.getPort()); 22 | 23 | // 设置组网起始监听端口 24 | netConfig.setPort(9701); 25 | System.out.println("Customer port:" + netConfig.getPort()); 26 | // 获取join元素 27 | JoinConfig joinConfig = netConfig.getJoin(); 28 | // 获取multicast元素 29 | MulticastConfig multicastConfig = joinConfig.getMulticastConfig(); 30 | // 输出组播协议端口 31 | System.out.println(multicastConfig.getMulticastPort()); 32 | // 禁用multicast协议 33 | multicastConfig.setEnabled(false); 34 | 35 | // 初始化Hazelcast 36 | Hazelcast.newHazelcastInstance(config); 37 | } 38 | } 39 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/config/HazelcastConfigSimple.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.config; 2 | 3 | import java.util.Map; 4 | 5 | import com.hazelcast.config.ClasspathXmlConfig; 6 | import com.hazelcast.config.Config; 7 | import com.hazelcast.config.MapConfig; 8 | import com.hazelcast.config.NetworkConfig; 9 | import com.hazelcast.core.Hazelcast; 10 | import com.hazelcast.core.HazelcastInstance; 11 | 12 | /** 13 | * 展示如何读取和引入配置 14 | * 15 | * @author chkui 16 | */ 17 | public class HazelcastConfigSimple { 18 | public static void main(String[] args) { 19 | // 从classpath加载配置文件 20 | Config config = new ClasspathXmlConfig("xmlconfig/simple-config.xml"); 21 | // 获取网络配置 22 | NetworkConfig netConfig = config.getNetworkConfig(); 23 | // 获取用户定义的map配置 24 | MapConfig mapConfigXml = config.getMapConfig("demo.config"); 25 | // 获取系统默认的map配置 26 | MapConfig mapConfigDefault = config.getMapConfig("default"); 27 | // 输出集群监听的起始端口号 28 | System.out.println("Current port:" + netConfig.getPort()); 29 | // 输出监听端口的累加号 30 | System.out.println("Current port count:" + netConfig.getPortCount()); 31 | // 输出自定义map的备份副本个数 32 | System.out.println("Config map backup count:" + mapConfigXml.getBackupCount()); 33 | // 输出默认map的备份副本个数 34 | System.out.println("Default map backup count:" + mapConfigDefault.getBackupCount()); 35 | 36 | // 测试创建Hazelcast实例并读写测试数据 37 | HazelcastInstance instance1 = Hazelcast.newHazelcastInstance(config); 38 | HazelcastInstance instance2 = Hazelcast.newHazelcastInstance(config); 39 | 40 | Map defaultMap1 = instance1.getMap("defaultMap"); 41 | defaultMap1.put(1, "testMap"); 42 | Map configMap1 = instance1.getMap("configMap"); 43 | configMap1.put(1, "configMap"); 44 | 45 | Map testMap2 = instance2.getMap("defaultMap"); 46 | System.out.println("Default map value:" + testMap2.get(1)); 47 | Map configMap2 = instance2.getMap("configMap"); 48 | System.out.println("Config map value:" + configMap2.get(1)); 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/config/HazelcastConfigVariable.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.config; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.io.InputStream; 5 | import java.util.Properties; 6 | 7 | import com.hazelcast.config.Config; 8 | import com.hazelcast.config.XmlConfigBuilder; 9 | 10 | public class HazelcastConfigVariable { 11 | // XML配置文件存放路径 12 | final static String DEF_CONFIG_FILE = "xmlconfig/variable-config.xml"; 13 | // properties文件路径 14 | final static String DEF_PROPERTIES_FILE = "properties/variable-config.properties"; 15 | public static void main(String[] args) { 16 | try { 17 | // 获取配置文件磁盘路径 18 | final String path = Thread.currentThread().getContextClassLoader().getResource("").toString() + DEF_CONFIG_FILE; 19 | // 构建XML配置 20 | XmlConfigBuilder builder = new XmlConfigBuilder(path); 21 | // 配置对应的properties解析参数 22 | builder.setProperties(getProperties()); 23 | // 创建Config 24 | Config config = builder.build(); 25 | // 输出Config参数 26 | System.out.println(config.getGroupConfig().getName()); 27 | } catch (FileNotFoundException e) { 28 | e.printStackTrace(); 29 | } 30 | } 31 | 32 | // get Properties 33 | private static Properties getProperties() { 34 | Properties p = null; 35 | try (InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(DEF_PROPERTIES_FILE)) { 36 | if (null != in) { 37 | p = new Properties(); 38 | p.load(in); 39 | } 40 | } catch (Exception e) { 41 | e.printStackTrace(); 42 | System.exit(0); 43 | } 44 | return p; 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/getstart/HazelcastGetStartClient.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.getstart; 2 | 3 | import java.util.Map; 4 | import java.util.Queue; 5 | 6 | import com.hazelcast.client.HazelcastClient; 7 | import com.hazelcast.client.config.ClientConfig; 8 | import com.hazelcast.core.HazelcastInstance; 9 | 10 | /** 11 | * 启动客户端,从集群服务中读取数据。 12 | * @author chkui 13 | */ 14 | public class HazelcastGetStartClient { 15 | public static void main(String[] args) { 16 | ClientConfig clientConfig = new ClientConfig(); 17 | HazelcastInstance instance = HazelcastClient.newHazelcastClient(clientConfig); 18 | Map clusterMap = instance.getMap("MyMap"); 19 | Queue clusterQueue = instance.getQueue("MyQueue"); 20 | 21 | System.out.println("Map Value:" + clusterMap.get(1)); 22 | System.out.println("Queue Size :" + clusterQueue.size()); 23 | System.out.println("Queue Value 1:" + clusterQueue.poll()); 24 | System.out.println("Queue Value 2:" + clusterQueue.poll()); 25 | System.out.println("Queue Size :" + clusterQueue.size()); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/getstart/HazelcastGetStartServerMaster.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.getstart; 2 | 3 | import java.util.Map; 4 | import java.util.Queue; 5 | 6 | import com.hazelcast.core.Hazelcast; 7 | import com.hazelcast.core.HazelcastInstance; 8 | 9 | /** 10 | * 创建Hazelcast主服务 11 | * @author chkui 12 | */ 13 | public class HazelcastGetStartServerMaster { 14 | public static void main(String[] args) { 15 | // 创建一个 hazelcastInstance实例 16 | HazelcastInstance instance = Hazelcast.newHazelcastInstance(); 17 | // 创建集群Map 18 | Map clusterMap = instance.getMap("MyMap"); 19 | clusterMap.put(1, "Hello hazelcast map!"); 20 | 21 | // 创建集群Queue 22 | Queue clusterQueue = instance.getQueue("MyQueue"); 23 | clusterQueue.offer("Hello hazelcast!"); 24 | clusterQueue.offer("Hello hazelcast queue!"); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/getstart/HazelcastGetStartServerSlave.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.getstart; 2 | 3 | import java.util.Map; 4 | import java.util.Queue; 5 | 6 | import com.hazelcast.core.Hazelcast; 7 | import com.hazelcast.core.HazelcastInstance; 8 | 9 | /** 10 | * 创建hazelcast从服务 11 | * @author chkui 12 | */ 13 | public class HazelcastGetStartServerSlave { 14 | public static void main(String[] args) { 15 | //创建一个 hazelcastInstance实例 16 | HazelcastInstance instance = Hazelcast.newHazelcastInstance(); 17 | Map clusterMap = instance.getMap("MyMap"); 18 | Queue clusterQueue = instance.getQueue("MyQueue"); 19 | 20 | System.out.println("Map Value:" + clusterMap.get(1)); 21 | System.out.println("Queue Size :" + clusterQueue.size()); 22 | System.out.println("Queue Value 1:" + clusterQueue.poll()); 23 | System.out.println("Queue Value 2:" + clusterQueue.poll()); 24 | System.out.println("Queue Size :" + clusterQueue.size()); 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/config/StaticMapConfig.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.config; 2 | 3 | import com.hazelcast.config.InMemoryFormat; 4 | import com.hazelcast.config.MapConfig; 5 | import com.hazelcast.config.MapIndexConfig; 6 | 7 | /** 8 | * 代码设置Map 9 | * @author chkui 10 | */ 11 | public class StaticMapConfig { 12 | public static void main(String[] args) { 13 | MapConfig mapConfig = new MapConfig(); 14 | mapConfig.setName("cacheMap")// 设置Map名称 15 | .setInMemoryFormat(InMemoryFormat.BINARY)// 设置内存格式 16 | .setBackupCount(1);// 设置副本个数 17 | 18 | mapConfig.getMapStoreConfig()// 19 | .setWriteDelaySeconds(60)// 20 | .setWriteBatchSize(1000);// 设置缓存格式 21 | 22 | mapConfig.addMapIndexConfig(new MapIndexConfig().setAttribute("id").setOrdered(true));// 增加索引 23 | mapConfig.addMapIndexConfig(new MapIndexConfig().setAttribute("name").setOrdered(true)); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/config/mapConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | BINARY 5 | true 6 | 0 7 | 1 8 | true 9 | 0 10 | 0 11 | LRU 12 | 150 13 | 5000 14 | 25 15 | true 16 | 17 | true 18 | false 19 | 20 | 21 | 60 22 | 1000 23 | true 24 | 25 | 26 | id 27 | name 28 | 29 | 30 | 31 | com.hazelcast.examples.EntryListener 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/getStart/ClientNode.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.getStart; 2 | 3 | import java.util.Map; 4 | 5 | import com.hazelcast.core.Hazelcast; 6 | import com.hazelcast.core.HazelcastInstance; 7 | 8 | /** 9 | * 客户端节点 10 | * @author chkui 11 | */ 12 | public class ClientNode { 13 | public static void main(String[] args) { 14 | // 获取Hazelcast实例 15 | HazelcastInstance ins = Hazelcast.newHazelcastInstance(); 16 | 17 | // 从集群中读取Map实例 18 | Map map = ins.getMap("default map"); 19 | 20 | // 输出map中数据 21 | map.forEach((k,v)->{ 22 | System.out.println("Pos:" + k + ". name:" + v); 23 | }); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/getStart/ServiceNode.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.getStart; 2 | 3 | import java.util.Map; 4 | 5 | import com.hazelcast.core.Hazelcast; 6 | import com.hazelcast.core.HazelcastInstance; 7 | 8 | /** 9 | * 服务节点 10 | * @author chkui 11 | */ 12 | public class ServiceNode { 13 | public static void main(String[] args) { 14 | // 获取Hazelcast实例 15 | HazelcastInstance ins = Hazelcast.newHazelcastInstance(); 16 | 17 | // 从集群中读取Map实例 18 | Map map = ins.getMap("default map"); 19 | 20 | // 向集群中添加数据 21 | System.out.println("Begin insert data"); 22 | map.put(1, "Cait Cassiopeia"); 23 | map.put(2, "Annie"); 24 | map.put(3, "Evelynn"); 25 | map.put(4, "Ashe"); 26 | System.out.println("End"); 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/interceptor/InterceptorDemo.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.interceptor; 2 | 3 | import com.hazelcast.core.Hazelcast; 4 | import com.hazelcast.core.HazelcastInstance; 5 | import com.hazelcast.core.IMap; 6 | 7 | public class InterceptorDemo { 8 | public static void main(String[] args) { 9 | HazelcastInstance ins = Hazelcast.newHazelcastInstance(); 10 | IMap imap = ins.getMap(""); 11 | imap.addInterceptor(new InterceptorExample());// 添加拦截器 12 | imap.put(1, "Mei"); 13 | imap.put(1, "Tracer"); 14 | imap.put(1, "D.va"); 15 | imap.put(1, "Mercy"); 16 | 17 | imap.get(1); 18 | imap.remove(1); 19 | System.out.println(imap.get(1)); 20 | } 21 | } 22 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/interceptor/InterceptorExample.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.interceptor; 2 | 3 | import java.io.Serializable; 4 | 5 | import com.hazelcast.map.MapInterceptor; 6 | 7 | public class InterceptorExample implements MapInterceptor, Serializable { 8 | private static final long serialVersionUID = -7591859317633144192L; 9 | @Override 10 | /**拦截get。可以返回其他值来替换原有值。返回null将不影响原有操作。 */ 11 | public Object interceptGet(Object obj) { 12 | prinfln("get : " + obj); 13 | return obj; 14 | } 15 | 16 | @Override 17 | /**在get操作执行完毕后被调用。*/ 18 | public void afterGet(Object obj) { 19 | prinfln("after get : " + obj); 20 | } 21 | 22 | @Override 23 | /**拦截put。返回的值会设置到map中。返回null时原有的put数据不会发生任何改变。 抛出异常会取消put操作。 */ 24 | public Object interceptPut(Object oldValue, Object newValue ) { 25 | prinfln("put old value : " + oldValue); 26 | prinfln("put new value : " + newValue); 27 | return newValue; 28 | } 29 | 30 | @Override 31 | /**在put操作执行完毕后被调用。*/ 32 | public void afterPut(Object obj) { 33 | prinfln("after put : " + obj); 34 | } 35 | 36 | @Override 37 | /**拦截remove。返回被删除对象或null将继续执行删除。 抛出异常会取消remove操作。 */ 38 | public Object interceptRemove(Object obj) { 39 | prinfln("remove : " + obj); 40 | return null; 41 | } 42 | 43 | @Override 44 | /**在remove操作执行完毕后被调用。*/ 45 | public void afterRemove(Object obj) { 46 | prinfln("afrer remove : " + obj); 47 | } 48 | 49 | private void prinfln(Object obj){ 50 | System.out.println(obj); 51 | } 52 | 53 | } 54 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/listener/ListenerDemo.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.listener; 2 | 3 | import com.hazelcast.core.Hazelcast; 4 | import com.hazelcast.core.HazelcastInstance; 5 | import com.hazelcast.core.IMap; 6 | 7 | public class ListenerDemo { 8 | public static void main(String[] args) { 9 | HazelcastInstance ins = Hazelcast.newHazelcastInstance(); 10 | IMap map = ins.getMap(""); 11 | map.addEntryListener(new ListenerExample(), true);//添加自定义监听器 12 | map.put(1, "Grand Theft Auto"); 13 | map.put(1, "Final Fantasy"); 14 | map.put(2, "World Of Warcraft"); 15 | 16 | HazelcastInstance insex = Hazelcast.newHazelcastInstance(); 17 | IMap mapex = insex.getMap(""); 18 | 19 | System.out.println(mapex.get(1)); 20 | System.out.println(mapex.get(2)); 21 | mapex.remove(1); 22 | mapex.remove(2); 23 | System.exit(0); 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/listener/ListenerExample.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.listener; 2 | 3 | import com.hazelcast.core.EntryEvent; 4 | import com.hazelcast.map.listener.EntryAddedListener; 5 | import com.hazelcast.map.listener.EntryRemovedListener; 6 | import com.hazelcast.map.listener.EntryUpdatedListener; 7 | 8 | public class ListenerExample implements EntryAddedListener, EntryRemovedListener, EntryUpdatedListener { 9 | @Override 10 | public void entryUpdated(EntryEvent entry) {//监听更新数据 11 | print("put entry. key = " + entry.getKey() + ". value = " + entry.getValue()); 12 | } 13 | 14 | @Override 15 | public void entryRemoved(EntryEvent entry) {//监听移除数据 16 | print("remove entry. key = " + entry.getKey() + ". value = " + entry.getValue()); 17 | } 18 | 19 | @Override 20 | public void entryAdded(EntryEvent entry) {//监听新增数据 21 | print("add entry. key = " + entry.getKey() + ". value = " + entry.getValue()); 22 | } 23 | 24 | private void print(Object obj){ 25 | System.out.println(obj); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/store/MapStoreExample.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.store; 2 | 3 | import java.util.Collection; 4 | import java.util.HashMap; 5 | import java.util.Map; 6 | 7 | import com.hazelcast.core.MapStore; 8 | 9 | /** 10 | * Map持久数据读写 11 | * @author chkui 12 | */ 13 | public class MapStoreExample implements MapStore { 14 | 15 | Map store; 16 | 17 | public MapStoreExample(){ 18 | store = new HashMap(); 19 | store.put(1, "Azeroth"); 20 | store.put(2, "Duskwood"); 21 | store.put(3, "Elwynn Forest"); 22 | store.put(4, "Deadwind Pass"); 23 | store.put(5, "Dead Mines"); 24 | store.put(6, "Grand Hamlet"); 25 | store.put(7, "Dark Portal"); 26 | store.put(8, "Ashenvale"); 27 | store.put(9, "Felwood"); 28 | store.put(10, "Orgrimmar"); 29 | } 30 | 31 | @Override 32 | public String load(Integer key) {//读取 33 | if(store.size() < key){ 34 | key = 0; 35 | } 36 | return store.get(key); 37 | } 38 | 39 | @Override 40 | public Map loadAll(Collection arg0) {//读取所有 41 | return store; 42 | } 43 | 44 | @Override 45 | public Iterable loadAllKeys() {// 读取所有键值 46 | return store.keySet(); 47 | } 48 | 49 | @Override 50 | public void delete(Integer key) {// 删除键值 51 | store.remove(key); 52 | } 53 | 54 | @Override 55 | public void deleteAll(Collection list) {// 删除所有键值 56 | list.forEach(key->store.remove(key)); 57 | } 58 | 59 | @Override 60 | public void store(Integer key, String value) {// 存储键值 61 | store.put(key, value); 62 | } 63 | 64 | @Override 65 | public void storeAll(Map map) {// 存储所有键值 66 | store.putAll(map); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/store/MapStoreExampleMain.java: -------------------------------------------------------------------------------- 1 | package org.palm.hazelcast.map.store; 2 | 3 | import java.io.FileNotFoundException; 4 | import java.util.Map; 5 | 6 | import com.hazelcast.config.ClasspathXmlConfig; 7 | import com.hazelcast.config.Config; 8 | import com.hazelcast.core.Hazelcast; 9 | import com.hazelcast.core.HazelcastInstance; 10 | 11 | public class MapStoreExampleMain { 12 | public static void main(String[] args) throws FileNotFoundException { 13 | //加载配置 14 | Config config = new ClasspathXmlConfig("org/palm/hazelcast/map/store/mapStoreConfig.xml"); 15 | //创建Hazelcast实力 16 | HazelcastInstance ins = Hazelcast.newHazelcastInstance(config); 17 | //获取Map 18 | Map map = ins.getMap("demo"); 19 | println(map.get(1));//输出第一条数据 20 | 21 | map.put(11, "Moonbrook");//添加一条数据 22 | println(map.get(11));//输出第一条数据 23 | 24 | map.remove(11);//移除添加的数据 25 | println(map.get(11));//输出被移除的数据 26 | } 27 | 28 | static private void println(Object obj){ 29 | System.out.println(obj); 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /src/main/java/org/palm/hazelcast/map/store/mapStoreConfig.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | 7 | org.palm.hazelcast.map.store.MapStoreExample 8 | 60 9 | 1000 10 | true 11 | 12 | 13 | -------------------------------------------------------------------------------- /src/main/resources/properties/variable-config.properties: -------------------------------------------------------------------------------- 1 | group.name=demo 2 | group.password=demopasswd -------------------------------------------------------------------------------- /src/main/resources/xmlconfig/simple-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | default 7 | default 8 | 9 | http://localhost:8080/mancenter 10 | 11 | 12 | 7701 13 | 14 | 0 15 | 16 | 17 | 18 | 224.2.2.3 19 | 54327 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 16 29 | 0 30 | 31 | 32 | 2 33 | 604800 34 | 1800 35 | LFU 36 | 20 37 | 25 38 | com.hazelcast.map.merge.LatestUpdateMapMergePolicy 39 | 40 | 41 | com.palm.vert.cluster.listener.ClusterCacheListener 42 | 43 | 44 | 45 | -------------------------------------------------------------------------------- /src/main/resources/xmlconfig/variable-config.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 6 | ${group.name} 7 | ${group.password} 8 | 9 | 10 | 5701 11 | 12 | 13 | 224.2.2.3 14 | 54327 15 | 16 | 17 | 18 | 19 | --------------------------------------------------------------------------------