我正在开发工作执行器软件。到目前为止,这是按照以下方式设计的:
请注意,WorkExecutor从FIFO Q获取请求并执行相同的操作并存储在DB中。 客户端必须通过调用不同的REST(用于检索工作状态)来检查以后的工作状态(使用步骤#3中收到的引用工作项)。
我在异步方法调用方面几乎没有任何知识。 我想知道对#1中的REST控制器或任何其他相关类进行哪些更改以使REST调用异步?这样客户端异步调用REST,获得接受响应,在AQ中推送请求,“WorkExecutor”从AQ弹出请求并执行工作,存储在DB中并在客户端上回调成功/失败。
请注意我的代码段。 bean在spring配置bean xml文件中配置。
任何有关样品的想法都将受到高度赞赏。提前谢谢,请原谅我的长篇文章。
@RestController
public class WorkExecutorRESTController {
@RequestMapping(value = "/workexecutor/{workdetails}", method = RequestMethod.GET)
public ResponseEntity<Integer> executeWork( @PathVariable String workdetails )
{
WorkAcceptor l_WorkAcceptor = WorkerBeanFactory.getBeanForId("WorkAcceptor"); //configured as prototype bean
Integer result = l_WorkAcceptor.sendWork(workdetails);
return new ResponseEntity<Integer>(result,HttpStatus.OK);
}
public class WorkAcceptor {
public Integer sendWork(String workdetails)
{
WorkQInserter l_WorkQInserter = WorkerBeanFactory.getBeanForId("WorkQInserter"); //configured as prototype bean
// Here generate the work id
if(l_WorkQInserter.enqueueTasks(workdetails, workid) == SUCCESSFUL)
{
return workid; //this goes back to client as reference id
}
else
return -1;
}
}
public class WorkQInserter {
public Integer WorkQInserter(String workdetails, Integer workid)
{
//PUSH THE DATA IN ORACLE AQ
//RETURN SUCCESS OR FAILURE
}
}
//This class is configured as singleton in bean xml. During initialization it runs a dedicated thread
//which periodically pops out request from the AQ and process
public class WorkExecutor {
public Integer execute()
{
//POPS OUT THE REQUEST FROM ORACLE AQ
//EXECUTE WORK
//RETURN SUCCESS OR FAILURE
}
}
答案 0 :(得分:0)
总结上述评论:
工作由 WorkExecutor 异步完成。客户端立即获得 workId 的句柄,如果回调丢失,它可用于查询状态。 WorkExectutor 通过已配置的回调端点通知客户端有关工作的最终状态。
即。 WorkExecutor 类似于:
public class WorkExecutor {
public Integer execute()
{
//POPS OUT THE REQUEST FROM ORACLE AQ
//EXECUTE WORK
// invoke client callback passing the workId
// workId should be available form the queue
// callback endpoint is static / configurable fix for all requests
// successStatus is the status whether the request was fulfilled successfully or even not.
notifyClient(workId, successStatus);
}
}
你的 RestController :
@RestController
public class WorkExecutorRESTController {
@RequestMapping(value = "/workexecutor/{workdetails}", method = RequestMethod.GET)
public ResponseEntity<Integer> executeWork( @PathVariable String workdetails )
{
WorkAcceptor l_WorkAcceptor = WorkerBeanFactory.getBeanForId("WorkAcceptor"); //configured as prototype bean
Integer result = l_WorkAcceptor.sendWork(workdetails);
return new ResponseEntity<Integer>(result,HttpStatus.ACCEPTED); // accepted rather than 200 / OK
}
}
无需使REST服务本身异步。