Reputation: 385
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
QueueExecutor runs the new job to processing component and then make the thread for checking periodically.
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
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