|
| 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 | +} |
0 commit comments