简体   繁体   English

如何在后端触发作业时立即从 rest api 发送响应?

[英]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.

相关问题 如何发送 Stream<string> 作为来自 REST API 的客户的响应?</string> - How to send Stream<String> as response to client from REST API? 从我的 Spring Boot Rest API 返回的响应与提供给我的 rest API 的后端 API 不同 - Response returned from my Spring Boot Rest API is not same as the backend API provided to my rest API 如何从其余Web服务发送响应 - How to send response from rest webservice 如何将数据从服务器(REST api 响应)推送/发送到 jsp/html 表单 - How to push/send data from server (REST api response) to a jsp/html form 如何立即发送异步请求响应 - How to send immediately asynchronous response for request 从Java后端上的NSstring读取REST响应 - Reading REST response from NSstring on Java Backend 如何将数据从servlet发送到rest api - how to send data from servlet to rest api 使用mmc rest API将日志文件从后端发送到UI-仅发送文件更改的逻辑 - send log file from backend to UI using mmc rest API- logic for sending only the changes in file java - 当响应代码为 4xx 时,如何从 REST API 解析响应正文 - How to parse reponse body from REST API when response code is 4xx in java 在Angular 8应用中上传媒体文件时,如何从rest api返回响应? - How to return response from rest api when uploading media file in an Angular 8 app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM