hazelcast入门教程_Hazelcast入门指南第7部分
hazelcast入門教程
這是解釋如何使用Hazelcast的系列文章的續篇。 如果一個人沒有閱讀其他六個帖子,請轉到目錄并閱讀其他帖子。
不同的地圖種類
Hazelcast的MultiMap打破了以前使用java.util.Collection接口的常規方式。 實際上,我認為MultiMap的概念完全打破了地圖的概念。 雖然法線貼圖將一個鍵關聯到一個值,但是MultiMaps可以將多個值映射到同一鍵 。 這是一個非常重要的概念,同一鍵有多個值。 值可以存儲在兩個不同的集合(集合或列表)中。 這些集合的行為類似于java.util.Collections庫的集合。
安全嗎?
MultiMap有一種瘋狂的方法。 在法線映射中,每個鍵可以存儲多個值,但是必須手動完成。 這意味著將集合從存儲中取出,進行任何更改,然后將集合放回存儲中。 對于線程安全而言,這可能是個問題,因為先前的步驟需要原子完成,否則其他線程可能會讀取陳舊或不一致的數據。 MultiMaps通過提供以下服務來幫助解決此問題:
- 可以通過一個put操作添加一個值。
- 一個人可以用鑰匙鎖定一個條目。 這是關鍵(雙關語),因為這意味著開發人員不必跟蹤每個條目的單獨鎖。
例
這個示例有些不同,因為在運行示例時,我使用Maven的failsafe插件作為主要引擎。 是的,我寫了兩個示例,因為我想展示使用MultiMap的兩種不同方式。 一種方法是,每個線程都擁有自己的游樂場,被分配一個唯一的密鑰,或者被分配一個共享的游樂場,或者所有線程共享相同的密鑰。 這也是如何將Hazelcast的IdGenerator用作在應用程序中創建線程安全性的方法的示例。
Pom文件
請記住,此示例代碼利用了Apache的Failsafe Maven插件 。 故障安全插件通過在第一次失敗時不終止構建來幫助進行自動集成測試。 它是surefire插件的分支。 我也一直在嘗試使用Maven提供的報告。 在命令行中輸入“ mvn site”,它將生成一個網站。
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0><groupId>com.darylmathison><artifactId>hazelcast-multimap-example><version>1.0-SNAPSHOT><description>Examples of using Hazelcast Multimap><developers><developer><name>Daryl Mathison><id>dmathison><roles><role>developer>>>><scm><connection>scm:git:https://github.com/darylmathison/hazelcast-multimap-example.git><url>https://github.com/darylmathison/hazelcast-multimap-example>><properties><maven.compiler.source>1.8><maven.compiler.target>1.8><project.build.sourceEncoding>UTF-8>><dependencies><dependency><groupId>com.hazelcast><artifactId>hazelcast><version>3.4.2>><dependency><groupId>junit><artifactId>junit><version>4.12><scope>test>>><build><plugins><plugin><groupId>org.apache.maven.plugins><artifactId>maven-failsafe-plugin><version>2.18.1><executions><execution><goals><goal>integration-test><goal>verify>>>>>>><reporting><plugins><plugin><groupId>org.apache.maven.plugins><artifactId>maven-project-info-reports-plugin><version>2.7><reportSets><reportSet><reports><report>dependencies><report>index><report>project-team><report>scm>>>>><plugin><groupId>org.apache.maven.plugins><artifactId>maven-javadoc-plugin><version>2.10.3><reportSets><reportSet><reports><report>javadoc><report>test-javadoc>>>>><plugin><groupId>org.apache.maven.plugins><artifactId>maven-surefire-report-plugin><version>2.18.1>><plugin><groupId>org.apache.maven.plugins><artifactId>maven-jxr-plugin><version>2.5><configuration><linkJavadoc>true>><reportSets><reportSet><reports><report>jxr><report>test-jxr>>>>>>> >MultimapAccessThread
這是每個訪問類型線程的基類。
package com.darylmathison.multimap;import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.HazelcastInstanceAware; import com.hazelcast.core.MultiMap;import java.io.Serializable; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock;/*** Abstract class to access MultiMap.*/ public abstract class MultiMapAccessThread implements Serializable, Runnable, HazelcastInstanceAware {protected com.hazelcast.core.HazelcastInstance instance;protected MultiMap<Long, Long> map;protected String mapName;protected Lock l = new ReentrantLock();public void setHazelcastInstance(HazelcastInstance instance) {l.lock();try {this.instance = instance;if (mapName != null && !mapName.isEmpty()) {map = instance.getMultiMap(mapName);}} finally {l.unlock();}}public String getMapName() {return mapName;}public void setMapName(String mapName) {l.lock();try {this.mapName = mapName;} finally {l.unlock();}} }IdMultiMapAccessThread
package com.darylmathison.multimap;/*** This thread accesses only one "slot" in a multimap.*/ public class IdMultiMapAccessThread extends MultiMapAccessThread {private Long id;@Overridepublic void run() {l.lock();boolean shouldRun = (map != null && id != null);l.unlock();if(shouldRun) {for (long i = 0; i < 10; i++) {map.put(id, i);}}}public Long getId() {return id;}public void setId(Long id) {this.id = id;} }GroupMultiMapAccessThread
package com.darylmathison.multimap;/*** Thread designed to share the same "slot" on a MultiMap.*/ public class GroupMultiMapAccessThread extends MultiMapAccessThread {private static final long MAX = 10;private Long groupId;/*** When an object implementing interface Runnable is used* to create a thread, starting the thread causes the object's* run method to be called in that separately executing* thread.** The general contract of the method run is that it may* take any action whatsoever.** @see Thread#run()*/@Overridepublic void run() {l.lock();boolean shouldRun = (groupId != null && map != null);l.unlock();if(shouldRun) {map.lock(groupId);try {if (map.get(groupId).isEmpty()) {System.out.println("adding to list");for (long i = 0; i < MAX; i++) {map.put(groupId, i);}} else {System.out.println("nothing to add");}} finally {map.unlock(groupId);}}}public void setGroupId(Long groupId) {l.lock();this.groupId = groupId;l.unlock();} }HazelcastInstanceResource
此規則啟動并關閉運行線程所需的Hazelcast實例。
package com.darylmathison.multimap.test.rule;import com.hazelcast.core.Hazelcast; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IExecutorService; import org.junit.rules.ExternalResource;/*** Created by Daryl on 4/27/2015.*/ public class HazelcastInstanceResource extends ExternalResource {public static final String SERVICE_NAME = "Charlotte";HazelcastInstance instance;IExecutorService service;@Overrideprotected void before() throws Throwable {super.before();instance = Hazelcast.newHazelcastInstance();service = instance.getExecutorService(SERVICE_NAME);}@Overrideprotected void after() {super.after();service.shutdown();instance.shutdown();}public HazelcastInstance getInstance() {return instance;}public IExecutorService getService() {return service;} }IdMultiMapAccessIT
這是一個使用IdGenerator為線程放置數據的新“操場”或鍵的示例。
package com.darylmathison.multimap;import com.darylmathison.multimap.test.rule.HazelcastInstanceResource; import com.hazelcast.core.HazelcastInstance; import com.hazelcast.core.IExecutorService; import com.hazelcast.core.IdGenerator; import org.junit.ClassRule; import org.junit.Test;import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future;/*** Integration test for IdMultiMapAccessThread*/ public class IdMultiMapAccessThreadIT {public static final String MAP_NAME = "idAccessMap";public static final String GEN_NAME = "singleAccess";public static final int NUM_THREADS = 10;@ClassRulepublic static HazelcastInstanceResource hazelcastInstanceResource = new HazelcastInstanceResource();@Testpublic void testIdThreads() {List threads = generateThreads(hazelcastInstanceResource.getInstance());List<Future<?>> futures = new ArrayList<>(NUM_THREADS);IExecutorService spinner = hazelcastInstanceResource.getService();for(IdMultiMapAccessThread thread: threads) {futures.add(spinner.submit(thread));}for(Future<?> future: futures) {try {future.get();} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}}}private List generateThreads(HazelcastInstance instance) {IdGenerator gen = instance.getIdGenerator(GEN_NAME);List threads = new ArrayList<>(NUM_THREADS);for(int i = 0; i < NUM_THREADS; i++) {IdMultiMapAccessThread thread = new IdMultiMapAccessThread();thread.setMapName(MAP_NAME);thread.setId(gen.newId());threads.add(thread);}return threads;} }GroupMultiMapAccessThreadIT
這是使用IdGenerator創建共享游樂場或插槽的示例。
package com.darylmathison.multimap;import com.darylmathison.multimap.test.rule.HazelcastInstanceResource; import com.hazelcast.core.IExecutorService; import com.hazelcast.core.IdGenerator; import org.junit.ClassRule; import org.junit.Test;import java.util.ArrayList; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.Future;/*** GroupMultiMapAccessThread Integration Test*/ public class GroupMultiMapAccessThreadIT {public static final int NUM_THREADS = 10;public static final String GEN_NAME = "groupIdGenerator";public static final String MAP_NAME = "groupMap";@ClassRulepublic static HazelcastInstanceResource hazelcastInstanceResource = new HazelcastInstanceResource();@Testpublic void testGroupMultiMapAccessThread() {List threads = createThreads();IExecutorService service = hazelcastInstanceResource.getService();List<Future<?>> futures = new ArrayList<>(NUM_THREADS);for(GroupMultiMapAccessThread thread: threads) {futures.add(service.submit(thread));}for(Future<?> future: futures) {try {future.get();} catch (InterruptedException | ExecutionException e) {e.printStackTrace();}}}private List createThreads() {List ret = new ArrayList<>(NUM_THREADS);IdGenerator gen = hazelcastInstanceResource.getInstance().getIdGenerator(GEN_NAME);Long groupId = gen.newId();for(int i = 0; i < NUM_THREADS; i++) {GroupMultiMapAccessThread thread = new GroupMultiMapAccessThread();thread.setMapName(MAP_NAME);thread.setGroupId(groupId);ret.add(thread);}return ret;} }結論
在這篇文章中,對Hazelcast的MultiMap進行了分析。 結果表明,MultiMaps可以為給定鍵存儲多個值。 還顯示了線程如何使用IdGenerator作為可能的密鑰生成器來共享MultiMap中的數據或如何為其自身存儲數據。 該代碼可以在GitHub上找到 。
參考資料
- http://www.hazelcast.com
- http://www.hazelcast.org
- https://github.com/hazelcast/hazelcast
翻譯自: https://www.javacodegeeks.com/2015/04/beginners-guide-to-hazelcast-part-7.html
hazelcast入門教程
總結
以上是生活随笔為你收集整理的hazelcast入门教程_Hazelcast入门指南第7部分的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux命令vi的使用(linux 命
- 下一篇: 改色同色系不用备案(改同色备案)