我已经安排了每个时间间隔的员工职位更新列表。
如果有任何提取员工的请求,我需要等到员工加载,最多我可以等待10秒。有些人可以帮助我在多线程环境中如何做到最好。
我的控制器看起来像这样
@Component
@Path("/employee")
public class Retriever {
@Autowired
private Loader loader;
private List<Employee> getAllEmployee(){
// wait unitl emaployees are loaded and get employee
return loader.getEmployeeList();
}
}
实际服务
@Component
public class Loader {
private List<String> employeeList;
@Autowired
private Backend backend;
@Scheduled(fixedRate=5000)
public void loadEmployee(){
//call DB other system etc
employeeList= backend.fetchEmployee();
}
public List<String> getEmployeeList() {
//wait until employees are loaded
return employeeList;
}
}