springmvc耗時處理的結果返回優化Callable

  1. 背景
    公司最近上了一個項目,其中要求用websocket去另一項目中獲取相應結果,返回給本項目中的control,由於websocket靠另外一個項目推送,所以時效性不能保證,需要使用一個線程,那麼如何在查詢接口中使用線程且不影響效率呢?Callable便被使用上了。

  2. 關於Callable是什麼
    而自從Java 1.5開始,就提供了Callable和Future,通過它們可以在任務執行完畢之後得到任務執行結果。如果要將一個流異步輸出需要藉助於Spring 4.2版本添加的一個新的接口StreamingResponseBody。

  3. 怎麼使用

@RestController
@RequestMapping("/demo")
public class DemoApi {
public static ConcurrentMap<Long, Student> studentMap = new ConcurrentHashMap<>();
    @PostMapping("/")
    public Callable<Student> index(@RequestBody School school){
         return ()->
        {
            long id = Thread.currentThread().getId();
            studentMap.remove(id);
            Map<String, Object> sendMessage = new HashMap<>(3,1);
            sendMessage.put("id", id);
            sendMessage.put("data", school);
            Gson gson = new Gson();
            SocketClient.send(gson.toJson(sendMessage));
            synchronized(Thread.currentThread()){
                Thread.currentThread().wait(60000);//超時自動喚醒
            }
            if(studentMap.containsKey(id)){
                Student result = studentMap.get(id);
                studentMap.remove(id);
                return result;
            }else {
                return null;
            }
        };
    }
}

喚醒線程

 @OnMessage
    public void onMessage(String message){
        if(EmptyUtil.isEmpty(message)){
            return;
        }
        Gson gson = new Gson();
        Student student= gson.fromJson(message, Student.class);
        long id = Long.parseLong(student.getThreadId());
        DemoApi.boqVOMap.put(id, student);
        Thread thread = findThread(id);
        if (thread != null) {
        synchronized(this){
           thread.notify();
        }
       }
      }
    }
    private  Thread findThread(long threadId) {
        ThreadGroup group = Thread.currentThread().getThreadGroup();
        while(group != null) {
            Thread[] threads = new Thread[(int)(group.activeCount() * 1.2)];
            int count = group.enumerate(threads, true);
            for(int i = 0; i < count; i++) {
                if(threadId == threads[i].getId()) {
                    return threads[i];
                }
            }
            group = group.getParent();
        }
        return null;
    }

使用過程中可能會報錯誤

An Executor is required to handle java.util.concurrent.Callable return value

這個有可能是項目中自己重寫了WebMvcConfigurationSupport 類需要重寫下面的方法

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {
    @Override
    public void configureAsyncSupport(final AsyncSupportConfigurer configurer) {
        configurer.setDefaultTimeout(60 * 1000L);
        configurer.registerCallableInterceptors(timeoutInterceptor());
        configurer.setTaskExecutor(threadPoolTaskExecutor());
   }
    @Bean
    public TimeoutCallableProcessingInterceptor timeoutInterceptor() {
        return new TimeoutCallableProcessingInterceptor();
    }
    @Bean
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor t = new ThreadPoolTaskExecutor();
        t.setCorePoolSize(10);
        t.setMaxPoolSize(50);
        t.setQueueCapacity(10);
        t.setThreadNamePrefix("WEB-Thread-");
        return t;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章