使用多线程提高REST服务性能 异步处理rest服务

使用Runnable
使用DeferredResult
异步处理配置

异步处理

使用多线程提高REST服务性能 异步处理rest服务_第1张图片

Callable

//		Callable result = new Callable() {
//			@Override
//			public String call() throws Exception {
//				logger.info("副线程开始");
//				Thread.sleep(1000);
//				logger.info("副线程返回");
//				return "success";
//			}
//		};

由主线程调用 子线程

DeferredResult

使用多线程提高REST服务性能 异步处理rest服务_第2张图片

线程1,和线程2 是 隔离的,谁也不知道谁的存在

MockQueue

@Component
public class MockQueue {
	private String placeOrder; //提供get set
	private String completeOrder;
	private Logger logger = LoggerFactory.getLogger(getClass());
	public void setPlaceOrder(String placeOrder) throws Exception {
		new Thread(() -> {
			logger.info("接到下单请求, " + placeOrder);
			try {
				Thread.sleep(1000);
			} catch (Exception e) {
				e.printStackTrace();
			}
			this.completeOrder = placeOrder;
			logger.info("下单请求处理完毕," + placeOrder);
		}).start();
	}

DeferredResultHolder

@Component
public class DeferredResultHolder {
//提供get set
	private Map> map = new HashMap>();
}

QueueListener

@Component
public class QueueListener implements ApplicationListener {

	@Autowired
	private MockQueue mockQueue;

	@Autowired
	private DeferredResultHolder deferredResultHolder;
	
	private Logger logger = LoggerFactory.getLogger(getClass());

	@Override
	public void onApplicationEvent(ContextRefreshedEvent event) {
		new Thread(() -> {
			while (true) {

				if (StringUtils.isNotBlank(mockQueue.getCompleteOrder())) {
					//如果完成订单有值
					String orderNumber = mockQueue.getCompleteOrder();
					logger.info("返回订单处理结果:"+orderNumber);
					//获取 DeferredResult 设置 返回值
					deferredResultHolder.getMap().get(orderNumber).setResult("place order success");
					mockQueue.setCompleteOrder(null);
					
				}else{
					try {
						Thread.sleep(100);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}

			}
		}).start();
	}
}

AsyncController

@RestController
public class AsyncController {
	@Autowired
	private MockQueue mockQueue;
	@Autowired
	private DeferredResultHolder deferredResultHolder;
	private Logger logger = LoggerFactory.getLogger(getClass());
	
	@RequestMapping("/order")
	public DeferredResult order() throws Exception {
		logger.info("主线程开始");
		//随机生成一个8位的 号
		String orderNumber = RandomStringUtils.randomNumeric(8);
		//设置 
		mockQueue.setPlaceOrder(orderNumber);
		//创建 DeferredResult
		DeferredResult result = new DeferredResult<>();
		//设置到deferredResultHolder
		deferredResultHolder.getMap().put(orderNumber, result);
		
		return result;
		
	}

}

多线程的配置

@Configuration
public class WebConfig extends WebMvcConfigurerAdapter {
	
	@SuppressWarnings("unused")
	@Autowired
	private TimeInterceptor timeInterceptor;

	@Override
	public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
		configurer.registerCallableInterceptors();//需要用这两个方法,单独注册拦截器
		configurer.registerDeferredResultInterceptors();
		configurer.setDefaultTimeout(); //超时
		configurer.setTaskExecutor();//设置可重用的线程池,默认是不可重用线程池
		super.configureAsyncSupport(configurer);
	}

你可能感兴趣的:(工具类)