Skip to content

Commit 5899165

Browse files
committed
Add hazelcast.yml file
1 parent 7680af1 commit 5899165

File tree

10 files changed

+219
-5
lines changed

10 files changed

+219
-5
lines changed

pom.xml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,11 +39,11 @@
3939
<artifactId>spring-boot-starter-web</artifactId>
4040
</dependency>
4141

42-
<dependency>
43-
<groupId>com.hazelcast</groupId>
44-
<artifactId>hazelcast</artifactId>
45-
<version>${hazelcast.version}</version>
46-
</dependency>
42+
<!-- <dependency>
43+
<groupId>com.hazelcast</groupId>
44+
<artifactId>hazelcast</artifactId>
45+
<version>${hazelcast.version}</version>
46+
</dependency>-->
4747
<dependency>
4848
<groupId>com.hazelcast</groupId>
4949
<artifactId>hazelcast-spring</artifactId>

src/main/java/com/pj/hazelcastdemo/HazelcastDemoApplication.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.pj.hazelcastdemo;
22

3+
import com.hazelcast.client.HazelcastClient;
4+
import com.hazelcast.client.config.ClientConfig;
35
import org.springframework.boot.SpringApplication;
46
import org.springframework.boot.autoconfigure.SpringBootApplication;
57

@@ -8,6 +10,14 @@ public class HazelcastDemoApplication
810
{
911
public static void main(String[] args)
1012
{
13+
test();
1114
SpringApplication.run(HazelcastDemoApplication.class, args);
1215
}
16+
17+
private static void test()
18+
{
19+
ClientConfig clientConfig = new ClientConfig();
20+
//clientConfig.getNetworkConfig().addAddress("127.0.0.1");
21+
HazelcastClient.newHazelcastClient(clientConfig);
22+
}
1323
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.pj.hazelcastdemo.config;
2+
3+
import com.hazelcast.client.HazelcastClient;
4+
import com.hazelcast.client.config.ClientConfig;
5+
import com.hazelcast.core.HazelcastInstance;
6+
7+
import java.util.concurrent.BlockingQueue;
8+
9+
public class Client
10+
{
11+
12+
public static void main(String[] args) throws Exception
13+
{
14+
ClientConfig clientConfig = new ClientConfig();
15+
clientConfig.getNetworkConfig().addAddress("127.0.0.1");
16+
17+
HazelcastInstance client = HazelcastClient.newHazelcastClient(clientConfig);
18+
System.out.println(clientConfig.toString());
19+
20+
BlockingQueue<String> queue = client.getQueue("queue");
21+
queue.put("Hello!");
22+
System.out.println("Message sent by Hazelcast Client!");
23+
24+
HazelcastClient.shutdownAll();
25+
}
26+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.pj.hazelcastdemo.config;
2+
3+
import com.hazelcast.client.config.ClientConfig;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
@Configuration
7+
public class HazelCastConfig
8+
{
9+
//@Bean
10+
public ClientConfig hazelCastConfig()
11+
{
12+
ClientConfig clientConfig = new ClientConfig();
13+
return clientConfig;
14+
//Hazelcast.newHazelcastInstance(cfg);
15+
}
16+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.pj.hazelcastdemo.config;
2+
3+
import com.hazelcast.config.Config;
4+
import com.hazelcast.core.Hazelcast;
5+
import com.hazelcast.core.HazelcastInstance;
6+
import com.hazelcast.core.HazelcastInstanceNotActiveException;
7+
8+
import java.util.concurrent.BlockingQueue;
9+
10+
public class Member
11+
{
12+
13+
public static void main(String[] args) throws Exception
14+
{
15+
Config config = new Config();
16+
config.getUserCodeDeploymentConfig().setEnabled(true);
17+
HazelcastInstance hz = Hazelcast.newHazelcastInstance(config);
18+
System.out.println("Hazelcast Member instance is running!");
19+
20+
BlockingQueue<String> queue = hz.getQueue("queue");
21+
try
22+
{
23+
for (; ; )
24+
{
25+
System.out.println(queue.take());
26+
}
27+
}
28+
catch (HazelcastInstanceNotActiveException e)
29+
{
30+
System.err.println("Unable to take from the queue. Hazelcast Member is probably going down!");
31+
}
32+
}
33+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.pj.hazelcastdemo.domain;
2+
3+
import lombok.Data;
4+
import org.hibernate.annotations.Cache;
5+
import org.hibernate.annotations.CacheConcurrencyStrategy;
6+
7+
import javax.persistence.Entity;
8+
import javax.persistence.Id;
9+
10+
@Data
11+
@Entity
12+
@Cache(region = "employeeCache", usage = CacheConcurrencyStrategy.READ_WRITE)
13+
public class Employee
14+
{
15+
@Id
16+
private String empId;
17+
private String empName;
18+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package com.pj.hazelcastdemo.repository;
2+
3+
import com.pj.hazelcastdemo.domain.Employee;
4+
import org.springframework.data.jpa.repository.JpaRepository;
5+
6+
public interface EmployeeRepository extends JpaRepository<Employee, String>
7+
{
8+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package com.pj.hazelcastdemo.web;
2+
3+
import com.hazelcast.client.HazelcastClient;
4+
import com.hazelcast.core.DistributedObject;
5+
import com.hazelcast.core.HazelcastInstance;
6+
import com.hazelcast.map.IMap;
7+
import com.pj.hazelcastdemo.domain.Employee;
8+
import com.pj.hazelcastdemo.repository.EmployeeRepository;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PathVariable;
11+
import org.springframework.web.bind.annotation.RequestMapping;
12+
import org.springframework.web.bind.annotation.RestController;
13+
14+
import java.util.Collection;
15+
import java.util.List;
16+
import java.util.Optional;
17+
import java.util.Random;
18+
19+
@RestController
20+
@RequestMapping("/api/v1/employee")
21+
public class EmployeeController
22+
{
23+
private final EmployeeRepository employeeRepository;
24+
25+
public EmployeeController(EmployeeRepository employeeRepository)
26+
{
27+
this.employeeRepository = employeeRepository;
28+
}
29+
30+
@GetMapping("/find/all")
31+
public List<Employee> findAll()
32+
{
33+
return employeeRepository.findAll();
34+
}
35+
36+
@GetMapping("/find/{empId}")
37+
public Optional<Employee> findById(@PathVariable String empId)
38+
{
39+
return employeeRepository.findById(empId);
40+
}
41+
42+
@GetMapping("/create")
43+
public List<Employee> createNewEmployee()
44+
{
45+
Employee employee = new Employee();
46+
String empId = "emp" + new Random().nextInt();
47+
employee.setEmpId(empId);
48+
employee.setEmpName(empId);
49+
employeeRepository.saveAndFlush(employee);
50+
51+
return employeeRepository.findAll();
52+
}
53+
54+
@GetMapping("/clear")
55+
public void clear()
56+
{
57+
HazelcastInstance hazelcastInstance = HazelcastClient.newHazelcastClient();
58+
Collection<DistributedObject> distributedObjects = hazelcastInstance.getDistributedObjects();
59+
for (DistributedObject object : distributedObjects)
60+
{
61+
if (object instanceof IMap)
62+
{
63+
hazelcastInstance.getMap(object.getName()).destroy();
64+
System.out.println("Map destroyed=" + hazelcastInstance.getMap(object.getName()).getName());
65+
}
66+
}
67+
hazelcastInstance.shutdown();
68+
}
69+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
11

2+
server.port=8080
3+
spring.jpa.show-sql=true
4+
spring.cache.type=hazelcast
5+
spring.jpa.properties.hibernate.cache.use_query_cache=true
6+
spring.jpa.properties.hibernate.cache.use_second_level_cache=true
7+
spring.jpa.properties.hibernate.cache.hazelcast.use_native_client=true
8+
spring.jpa.properties.hibernate.cache.region.factory_class=com.hazelcast.hibernate.HazelcastCacheRegionFactory
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: '3'
2+
services:
3+
hazelcast1:
4+
image: hazelcast/hazelcast
5+
environment:
6+
- 'JAVA_OPTS=-Dhazelcast.local.publicAddress=192.168.1.157:5701 -Dhazelcast.mancenter.url=http://mancenter:8090/mancenter -Dgroup.name=hz-compose -Dgroup.password=s3crEt'
7+
ports:
8+
- '5701:5701'
9+
links:
10+
- 'management-center:mancenter'
11+
hazelcast2:
12+
image: hazelcast/hazelcast
13+
environment:
14+
- 'JAVA_OPTS=-Dhazelcast.local.publicAddress=192.168.1.157:5702 -Dhazelcast.mancenter.url=http://mancenter:8090/mancenter -Dgroup.name=hz-compose -Dgroup.password=s3crEt'
15+
ports:
16+
- '5702:5701'
17+
links:
18+
- 'management-center:mancenter'
19+
management-center:
20+
image: hazelcast/management-center
21+
volumes:
22+
- '~/mancenter3.8:/mancenter-3.8'
23+
environment:
24+
- MANCENTER_DATA=/mancenter-3.8
25+
- JAVA_OPTS=-Dhazelcast.mc.rest.enabled=true
26+
ports:
27+
- '8090:8080'

0 commit comments

Comments
 (0)