使用多线程提高REST服务器性能
2020-12-13 15:52
标签:moc author uri 行业 listener listen 监听 comm trace 异步处理REST服务 1、使用Runnable异步处理Rest服务 释放主线程,启用副线程进行处理,副线程处理完成后直接返回请求 主要代码 2、使用DeferredResult异步处理Rest服务 释放主线程,启用副线程1进行前处理,副线程2进行后处理,副线程2处理完后返回请求 模拟业务场景 主线程调用副线程1进行业务处理,将任务放于消息队列,副线程2监听消息队列,并处理队列的任务,在使用DeferredResult获取队列返回的结果,返回给前端 Controller类 模拟消息队列类 监听消息队列并处理类 异步处理结果类 3、异步处理配置 使用多线程提高REST服务器性能 标签:moc author uri 行业 listener listen 监听 comm trace 原文地址:https://www.cnblogs.com/maple92/p/11616472.htmlimport java.util.concurrent.Callable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
/**
*
* @author hzc
*
*/
@RestController
public class AsyncController {
private Logger logger = LoggerFactory.getLogger(AsyncController.class);
@RequestMapping("/order")
public Callable
package com.maple.security.web.async;
import org.apache.commons.lang.RandomStringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.async.DeferredResult;
/**
*
* @author hzc
*
*/
@RestController
public class AsyncController {
private Logger logger = LoggerFactory.getLogger(AsyncController.class);
@Autowired
private MockQueue mockQueue;
@Autowired
private DeferredResultHolder deferredResultHolder;
@RequestMapping("/order")
public DeferredResult
package com.maple.security.web.async;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
/**
*
* @author hzc
*
*/
@Component
public class MockQueue {
private Logger logger = LoggerFactory.getLogger(MockQueue.class);
// 生成下单
private String placeOrder;
// 完成下单
private String completeOrder;
public String getPlaceOrder() {
return placeOrder;
}
public void setPlaceOrder(String placeOrder) {
new Thread(() -> {
logger.info("接到下单请求");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.completeOrder = placeOrder;
logger.info("下单请求处理完毕," + placeOrder);
}).start();
}
public String getCompleteOrder() {
return completeOrder;
}
public void setCompleteOrder(String completeOrder) {
this.completeOrder = completeOrder;
}
}
package com.maple.security.web.async;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
@Component
public class QueueListener implements ApplicationListener
/**
*
*/
package com.maple.security.web.async;
import java.util.HashMap;
import java.util.Map;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.async.DeferredResult;
/**
* @author hzc
*
*/
@Component
public class DeferredResultHolder {
private Map
下一篇:java 比较时间的几种方法