【问题标题】:is there any library or solution for spring response with long time needed job是否有任何需要长时间工作的弹簧响应库或解决方案
【发布时间】:2018-10-07 07:37:31
【问题描述】:

我项目中的某些过程需要几分钟(1~10 分钟)。我使用 spring boot web 提供了这个过程的结果。所以我的 API 必须返回带有状态的响应(排队/运行/完成/失败)。所以我通过 ResponseEntity 类实现了这种属性。

我的请求流程是

  1. 用户请求
  2. javascript调用带参数的API
  3. 服务器使用请求的属性(参数,用户信息)制作作业
  4. 检查作业是否已存在。如果没有,请将作业放入队列。如果存在,则返回当前作业状态(已完成作业有处理结果)。

QueueExecutor 将新作业运行到处理组件,然后使线程定期检查。

  1. 用户的客户端定期请求。并使用setInterval() 获取作业正在排队/运行/完成。如果没有完成,通过。否则,运行 UI 进程。

在我的流程中,我有很多小问题,所以我想知道是否有任何通用或有用的库或解决方案。请给我建议。谢谢。

【问题讨论】:

    标签: spring spring-mvc spring-boot long-polling spring-web


    【解决方案1】:

    我建议使用推送技术比传统的轮询方法更好,more information

    首先你需要在 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()) + "!");
        }
    }
    

    然后为 STOMP 消息配置 Spring:

    @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();
        }
    }
    

    在客户端基于您应该注册/订阅消息的 JS-Library。

    看看这些使用spring进行推送的例子:

    【讨论】:

      猜你喜欢
      • 2019-12-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2023-03-22
      相关资源
      最近更新 更多