7、spring boot + Maven + Restful 模拟异步处理Rest服务 提高服务器吞吐量

本文通过对比同步和异步处理,探讨了如何使用Spring Boot结合Maven和Restful创建异步服务,以提高服务器性能。文中详细介绍了使用Callable和DeferredResult的方法,并模拟了消息队列的处理,通过AsyncController和QueueListener等组件实现复杂的企业级应用场景。
摘要由CSDN通过智能技术生成

同步异步处理对比
在这里插入图片描述

  1. 使用Callable进行异步处理,副线程写在主线程里面的 ,符合企业级开发的一般应用场景!AsyncController.java
package com.imooc.web.asnyc;

import java.util.concurrent.Callable;

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;

@RestController
public class AsyncController {
	private Logger logger = LoggerFactory.getLogger(getClass());
	@RequestMapping("/order")
	public Callable<String> order() throws Exception {
	logger.info("主线程开启");
		Callable<String> result = new Callable<String>() {
			@Override
			public String call() throws Exception {
				logger.info("副线程开启");
				Thread.sleep(1000);
				logger.info("副线程关闭");
				return "success";
			}
		};
		logger.info("主线程关闭");
		return result;
	}
}

运行结果:
在这里插入图片描述

  1. 使用DefferedResult异步处理Rest服务,符合企业级开发的复杂应用场景!
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值