I have to convert the following code into the non-blocking(asynchronous) process.
EmpController class
@RestController
@RequestMapping("/emp")
public class EmpController {
private EmployeeRepository empRepo;
@Autowired
public EmpController(EmployeeRepository empRepo)
{
this.empRepo=empRepo;
}
@PostMapping("/save/all")
@Consumes({MediaType.APPLICATION_JSON})
public void saveEmpAll(@RequestBody List<Employee> emps)
{
empRepo.saveEmpAll(emps);
}
}
EmployeeRepository class
@Repository
public class EmployeeRepository implements EmployeeDAO {
@Override
public void saveEmpAll(List<Employee> emps) {
Map<Long,Employee> employees=new HashMap<>();
emps.forEach(emp->{
emp.setDate(new Date());
employees.put(emp.getId(),emp);
});
hashOperations.putAll("EMP",employees);
}
}
Use Reactive Programming with Spring Webflux
controller
@PostMapping("/save/all")
@Consumes({MediaType.APPLICATION_JSON})
public void saveEmpAll(@RequestBody Flux<Employee> emps)
{
emps.collectMap(e->{
e.setDate(new Date());
// todo.............( can I do like this? is it the right way?)
});
}
ropository
@Override
public void saveEmpAll(Map<Long,Employee> employees) {
hashOperations.putAll("EMP",employees);
}
is there any other way to create HashMap and put values into it in an asynchronous way with Flux?