[英]how to send response immediately from rest api when it triggers a job in the backend?
I need to trigger a job using the api, I dont want the api give response only after the completion of job, would like to respond back with "your job has been created" and let the job run in the backend and complete after couple of minutes.我需要使用 api 触发作业,我不希望 api 仅在作业完成后才给出响应,我想回复“您的作业已创建”并让作业在后端运行并在几次后完成分钟。
As am newbie to jaxrs, I couldn't able to find solution to this problem.作为 jaxrs 的新手,我找不到解决这个问题的方法。 All i can see is send a 201 response.
我只能看到发送 201 响应。 yes but how?
是的,但是怎么?
Below is the code am using:下面是我使用的代码:
@Path("{triggerjob}")
public Response ClassA {
ClassB objB = new ClassB();
objB.methodOne(jsonObject);
//what to do for response message as soon as job submitted?
return Response.status(201).build();
}
public void Class B {
ClassC objC = new ClassC();
objC.methodOne(jsonObject); //This triggers the job and job runs for 1/2 hour
}
So the classB will be return to Class A only after the job is completed, I would like to the user to know that the job is triggered and will be completed after half hour.所以作业完成后classB才会返回Class A,我想让用户知道作业被触发,半小时后完成。 How to achieve this?
如何做到这一点? How to jump to return statement and provide the update to the user who triggered this job?
如何跳转到返回语句并向触发此作业的用户提供更新?
You should run your job in async way.你应该以异步方式运行你的工作。
For example via EJB @Asynchronous例如通过 EJB @Asynchronous
@Stateless
public class B {
@Asynchronous
public void doAsynchronousWork() {
}
}
By the way for the http response the best response code here would be 202 accepted
顺便说一下,对于 http 响应,这里的最佳响应代码是
202 accepted
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.