BAEL-221 - Guide to Hazelcast

This commit is contained in:
nguyenminhtuanfit@gmail.com 2016-09-03 11:32:42 +07:00
parent 91d12fe986
commit 897e245524
6 changed files with 196 additions and 0 deletions

72
hazelcast/pom.xml Normal file
View File

@ -0,0 +1,72 @@
<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</modelVersion>
<groupId>com.baeldung</groupId>
<artifactId>hazelcast</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>hazelcast</name>
<dependencies>
<!-- hazelcast -->
<dependency>
<groupId>com.hazelcast</groupId>
<artifactId>hazelcast-all</artifactId>
<version>${hazelcast.version}</version>
</dependency>
<!-- utils -->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slf4j.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>${logback.version}</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>${logback.version}</version>
</dependency>
</dependencies>
<build>
<finalName>hazelcast</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<properties>
<!-- hazelcast -->
<hazelcast.version>3.7</hazelcast.version>
<!-- logging -->
<org.slf4j.version>1.7.21</org.slf4j.version>
<logback.version>1.1.7</logback.version>
<!-- maven plugins -->
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
</properties>
</project>

View File

@ -0,0 +1,26 @@
package com.baeldung.hazelcast.cluster;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.baeldung.hazelcast.listener.CountryEntryListener;
import com.hazelcast.client.HazelcastClient;
import com.hazelcast.client.config.ClientConfig;
import com.hazelcast.config.GroupConfig;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IMap;
public class NativeClient {
private static final Logger logger = LoggerFactory.getLogger(NativeClient.class);
public static void main(String[] args) throws InterruptedException {
ClientConfig config = new ClientConfig();
GroupConfig groupConfig = config.getGroupConfig();
groupConfig.setName("dev");
groupConfig.setPassword("dev-pass");
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
IMap<Long, String> countryMap = hazelcastInstanceClient.getMap("country");
countryMap.addEntryListener(new CountryEntryListener(), true);
logger.info("Country map size: " + countryMap.size());
}
}

View File

@ -0,0 +1,23 @@
package com.baeldung.hazelcast.cluster;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.core.IdGenerator;
public class ServerNode {
private static final Logger logger = LoggerFactory.getLogger(ServerNode.class);
public static void main(String[] args) {
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
Map<Long, String> countryMap = hazelcastInstance.getMap("country");
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid");
Long countryIdGenerator = idGenerator.newId() == 0L ? 1L : idGenerator.newId();
countryMap.put(countryIdGenerator, "Country1");
logger.info("Country map size: " + countryMap.size());
}
}

View File

@ -0,0 +1,41 @@
package com.baeldung.hazelcast.listener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.hazelcast.core.EntryEvent;
import com.hazelcast.core.MapEvent;
import com.hazelcast.map.listener.EntryAddedListener;
import com.hazelcast.map.listener.EntryEvictedListener;
import com.hazelcast.map.listener.EntryRemovedListener;
import com.hazelcast.map.listener.EntryUpdatedListener;
import com.hazelcast.map.listener.MapClearedListener;
import com.hazelcast.map.listener.MapEvictedListener;
public class CountryEntryListener implements EntryAddedListener<Long, String>, EntryRemovedListener<Long, String>, EntryUpdatedListener<Long, String>, EntryEvictedListener<Long, String>, MapEvictedListener, MapClearedListener {
private static final Logger logger = LoggerFactory.getLogger(CountryEntryListener.class);
public void entryAdded(EntryEvent<Long, String> event) {
logger.info("entryAdded:" + event);
}
public void entryUpdated(EntryEvent<Long, String> event) {
logger.info("entryUpdated:" + event);
}
public void entryRemoved(EntryEvent<Long, String> event) {
logger.info("entryRemoved:" + event);
}
public void entryEvicted(EntryEvent<Long, String> event) {
logger.info("entryEvicted:" + event);
}
public void mapCleared(MapEvent event) {
logger.info("mapCleared:" + event);
}
public void mapEvicted(MapEvent event) {
logger.info("mapEvicted:" + event);
}
}

View File

@ -0,0 +1,16 @@
<?xml version="1.0" encoding="UTF-8"?>
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
xmlns="http://www.hazelcast.com/schema/config"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<network>
<port auto-increment="true" port-count="20">5701</port>
<join>
<multicast enabled="false">
</multicast>
<tcp-ip enabled="true">
<member>machine1</member>
<member>localhost</member>
</tcp-ip>
</join>
</network>
</hazelcast>

View File

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
</layout>
</appender>
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
<appender-ref ref="STDOUT" />
</logger>
<root level="DEBUG">
<appender-ref ref="STDOUT" />
</root>
</configuration>