Skip to content

Commit ef7c2cb

Browse files
authored
Merge pull request eugenp#661 from nguyenminhtuanfit/master
BAEL-221 - Guide to Hazelcast
2 parents bde6c18 + 97d0c71 commit ef7c2cb

5 files changed

Lines changed: 154 additions & 0 deletions

File tree

‎hazelcast/pom.xml‎

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.baeldung</groupId>
5+
<artifactId>hazelcast</artifactId>
6+
<version>0.0.1-SNAPSHOT</version>
7+
<name>hazelcast</name>
8+
9+
<dependencies>
10+
<dependency>
11+
<groupId>com.hazelcast</groupId>
12+
<artifactId>hazelcast</artifactId>
13+
<version>${hazelcast.version}</version>
14+
</dependency>
15+
16+
<dependency>
17+
<groupId>com.hazelcast</groupId>
18+
<artifactId>hazelcast-client</artifactId>
19+
<version>3.7.2</version>
20+
</dependency>
21+
22+
<!-- utils -->
23+
<dependency>
24+
<groupId>org.slf4j</groupId>
25+
<artifactId>slf4j-api</artifactId>
26+
<version>${org.slf4j.version}</version>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>ch.qos.logback</groupId>
31+
<artifactId>logback-classic</artifactId>
32+
<version>${logback.version}</version>
33+
</dependency>
34+
35+
<dependency>
36+
<groupId>ch.qos.logback</groupId>
37+
<artifactId>logback-core</artifactId>
38+
<version>${logback.version}</version>
39+
</dependency>
40+
41+
</dependencies>
42+
43+
<build>
44+
<finalName>hazelcast</finalName>
45+
<resources>
46+
<resource>
47+
<directory>src/main/resources</directory>
48+
<filtering>true</filtering>
49+
</resource>
50+
</resources>
51+
52+
<plugins>
53+
<plugin>
54+
<groupId>org.apache.maven.plugins</groupId>
55+
<artifactId>maven-compiler-plugin</artifactId>
56+
<version>${maven-compiler-plugin.version}</version>
57+
<configuration>
58+
<source>1.8</source>
59+
<target>1.8</target>
60+
</configuration>
61+
</plugin>
62+
</plugins>
63+
</build>
64+
65+
<properties>
66+
<!-- hazelcast -->
67+
<hazelcast.version>3.7.2</hazelcast.version>
68+
69+
<!-- logging -->
70+
<org.slf4j.version>1.7.21</org.slf4j.version>
71+
<logback.version>1.1.7</logback.version>
72+
73+
<!-- maven plugins -->
74+
<maven-compiler-plugin.version>3.5.1</maven-compiler-plugin.version>
75+
</properties>
76+
77+
</project>
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.baeldung.hazelcast.cluster;
2+
3+
import java.util.Map.Entry;
4+
5+
import com.hazelcast.client.HazelcastClient;
6+
import com.hazelcast.client.config.ClientConfig;
7+
import com.hazelcast.config.GroupConfig;
8+
import com.hazelcast.core.HazelcastInstance;
9+
import com.hazelcast.core.IMap;
10+
11+
public class NativeClient {
12+
13+
public static void main(String[] args) throws InterruptedException {
14+
ClientConfig config = new ClientConfig();
15+
GroupConfig groupConfig = config.getGroupConfig();
16+
groupConfig.setName("dev");
17+
groupConfig.setPassword("dev-pass");
18+
HazelcastInstance hazelcastInstanceClient = HazelcastClient.newHazelcastClient(config);
19+
IMap<Long, String> map = hazelcastInstanceClient.getMap("data");
20+
for (Entry<Long, String> entry : map.entrySet()) {
21+
System.out.println(String.format("Key: %d, Value: %s", entry.getKey(), entry.getValue()));
22+
}
23+
}
24+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.baeldung.hazelcast.cluster;
2+
3+
import java.util.Map;
4+
5+
import com.hazelcast.core.Hazelcast;
6+
import com.hazelcast.core.HazelcastInstance;
7+
import com.hazelcast.core.IdGenerator;
8+
9+
public class ServerNode {
10+
11+
public static void main(String[] args) {
12+
HazelcastInstance hazelcastInstance = Hazelcast.newHazelcastInstance();
13+
Map<Long, String> map = hazelcastInstance.getMap("data");
14+
IdGenerator idGenerator = hazelcastInstance.getIdGenerator("newid");
15+
for (int i = 0; i < 10; i++) {
16+
map.put(idGenerator.newId(), "message" + 1);
17+
}
18+
}
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<hazelcast xsi:schemaLocation="http://www.hazelcast.com/schema/config hazelcast-config-3.7.xsd"
3+
xmlns="http://www.hazelcast.com/schema/config"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
5+
<network>
6+
<port auto-increment="true" port-count="20">5701</port>
7+
<join>
8+
<multicast enabled="false">
9+
</multicast>
10+
<tcp-ip enabled="true">
11+
<member>machine1</member>
12+
<member>localhost</member>
13+
</tcp-ip>
14+
</join>
15+
</network>
16+
</hazelcast>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<configuration>
3+
4+
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
5+
<layout class="ch.qos.logback.classic.PatternLayout">
6+
<pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg %n</pattern>
7+
</layout>
8+
</appender>
9+
10+
<logger name="com.baeldung.hazelcast" level="INFO" additivity="false">
11+
<appender-ref ref="STDOUT" />
12+
</logger>
13+
14+
<root level="DEBUG">
15+
<appender-ref ref="STDOUT" />
16+
</root>
17+
18+
</configuration>

0 commit comments

Comments
 (0)