spring boot 集成Async 注解 异步处理
来源:互联网 发布:ios11.2 软件打不开 编辑:程序博客网 时间:2024/06/06 06:39
有些时候 需要先返回数据 ,入库 以及其他操作可以异步去执行
定义一个task 任务类 :
@Component
public class Task {
@Autowired
public RedisDao redisDao;
@Autowiredpublic UserOperatorService userOperatorService;@Asyncpublic void AsyncTokenInfo(String token,JSONObject jsonObject) throws ParseException { //提取手机号码 查询该用户是否存在 String phone = jsonObject.getString("phone"); Long user_id = 0L; Long user_child_id = 0L; //不存在则解析注册用戶 user_id = userOperatorService.addOrUpdateUser(jsonObject);; //注冊小孩 学生 user_child_id = userOperatorService.addUserChild(jsonObject, user_id, user_child_id); //注册老师 userOperatorService.addUserTeacher(jsonObject, user_id); //保存token的key String ybbLoginPhone = RedisKeys.getRedisKey(RedisKeys.RootKey.ybbLoginPhone, phone); boolean phone_exists = redisDao.exists(ybbLoginPhone); if (phone_exists) { String token_old = (String) redisDao.get(ybbLoginPhone); String ybbLoginToken = RedisKeys.getRedisKey(RedisKeys.RootKey.ybbLoginToken, token_old); redisDao.remove(ybbLoginToken); } JSONObject ybbJson = new JSONObject(); ybbJson.put("phone", phone); ybbJson.put("user_id", user_id); ybbJson.put("user_child_id", user_child_id); redisDao.set(RedisKeys.getRedisKey(RedisKeys.RootKey.ybbLoginToken, token), ybbJson.toJSONString(), 24 * 60 * 60 * 30); redisDao.set(ybbLoginPhone, token);}/***======================**///定义一个随机对象.public static Random random =new Random();@Async //加入"异步调用"注解public void doTaskOne() throws InterruptedException { System.out.println("开始执行任务一"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");}@Asyncpublic void doTaskTwo() throws InterruptedException { System.out.println("开始执行任务二"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");}@Asyncpublic void doTaskThree() throws InterruptedException { System.out.println("开始执行任务三"); long start = System.currentTimeMillis(); Thread.sleep(random.nextInt(10000)); long end = System.currentTimeMillis(); System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");}
}
定义一个controller 调试这个task 或者写个单元测试
现在写个controller
@ResponseBody
@RequestMapping(“/task”)
public String task() throws Exception {
System.out.println(“开始执行Controller任务”);
long start = System.currentTimeMillis();
task.doTaskOne();
task.doTaskTwo();
task.doTaskThree();
long end = System.currentTimeMillis();
System.out.println(“完成Controller任务,耗时:” + (end - start) + “毫秒”);
return “success”;
}
通过访问 localhost:8088/task
多次执行 可以发现 想返回success
阅读全文
0 0
- spring boot 集成Async 注解 异步处理
- Spring中的@Async注解 异步任务处理
- Spring中异步处理@Async注解
- spring boot 使用注解@async实现异步调用
- spring 异步处理@Async
- spring boot(17)-@Async异步
- spring boot(17)-@Async异步
- Spring boot 使用异步@Async
- Spring Boot Async异步执行
- spring中使用@Async注解进行异步处理
- spring中使用@Async注解进行异步处理
- spring @Async 注解异步执行
- Spring异步方法注解@Async
- Spring异步方法注解 @Async
- spring boot异步(Async)任务调度
- Spring的好用注解之异步注解@Async
- @Async异步注解
- 56. spring boot中使用@Async实现异步调用
- malloc calloc new 区别
- Thread类的知识整理
- 动态代理与AOP(3)
- 纠结了好久的递归
- win10 eclipse jdk1.7配置Hanlp
- spring boot 集成Async 注解 异步处理
- netty4.x初试,简单的通讯
- Adapter模式
- C/C++调用lua的table,table包含函数,table和变量
- 浏览器访问sp页面出现文件下载的情况
- node
- 怎么在官网上下载java连接mysql的驱动jar包?
- Python翻转字符串(reverse string)详解
- 58