rura6502
rura6502

Reputation: 385

is there any library or solution for spring response with long time needed job

Some process in my project needed a few minutes(1~10min). and I provide the result of this process using spring boot web. so my API have to return the response with status(queueing/running/finished/failed). so I made kind of this attributes implement by ResponseEntity Class.

My request flow is

  1. User request
  2. javascript call the API with parameter
  3. server make job using request's attribute(parameter, user information)
  4. check the job already exist. if not, put the job to the queue. if exist, return the now job status(finished job has the result of the process).

QueueExecutor runs the new job to processing component and then make the thread for checking periodically.

  1. user's client request periodically. and get the job is queueing/running/finished using setInterval(). if not finished, pass. else, run the UI process.

In my flow, I have many little problems so I want to know that is there any general or useful library or solutions. please advice to me. thanks.

Upvotes: 1

Views: 247

Answers (1)

M-Razavi
M-Razavi

Reputation: 3467

I suggest using Push Technology is better than traditional polling method, more information.

First of all you need to create a message-handling controller in Spring:

@Controller
public class GreetingController {
    @MessageMapping("/hello")
    @SendTo("/topic/greetings")
    public Greeting greeting(HelloMessage message) throws Exception {
        Thread.sleep(1000); // simulated delay
        return new Greeting("Hello, " + HtmlUtils.htmlEscape(message.getName()) + "!");
    }
}

Then Configure Spring for STOMP messaging :

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        config.enableSimpleBroker("/topic");
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        registry.addEndpoint("/gs-guide-websocket").withSockJS();
    }
}

In client side based on which JS-Library you should register/subscribe for message.

Take a look on these example which use spring for push:

Upvotes: 1

Related Questions