spring异步读取mysql_Spring @Async之一:实现异步调用示例

什么是“异步调用”?

“异步调用”对应的是“同步调用”,同步调用指程序按照定义顺序依次执行,每一行程序都必须等待上一行程序执行完成之后才能执行;异步调用指程序在顺序执行时,不等待异步调用的语句返回结果就执行后面的程序。

同步调用

下面通过一个简单示例来直观的理解什么是同步调用:

定义Task类,创建三个处理函数分别模拟三个执行任务的操作,操作消耗时间随机取(10秒内)

packagecom.dxz.demo1;importjava.util.Random;importorg.springframework.stereotype.Component;importorg.springframework.web.bind.annotation.RequestMapping;/*** 定义3个任务*/@Componentpublic classTask1 {//定义一个随机对象.

public static Random random = newRandom();//任务一;

public void doTaskOne() throwsException {

System.out.println("开始做任务一");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");

}//任务二;

public void doTaskTwo() throwsException {

System.out.println("开始做任务二");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");

}//任务3;

public void doTaskThree() throwsException {

System.out.println("开始做任务三");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");

}

}

编写一个访问方法:

packagecom.dxz.demo1;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.SpringApplicationConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importcom.dxz.HelloApplication;

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes= HelloApplication.class)public classTask1Test {

@AutowiredprivateTask1 task1;//测试task1.

@Testpublic void task1() throwsException{

task1.doTaskOne();

task1.doTaskTwo();

task1.doTaskThree();

}

}

运行可以看到类似如下输出:

开始做任务一2017-04-28 18:02:57.397 WARN 11016 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart ifthe connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect2017-04-28 18:02:57.398 INFO 11016 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0完成任务一,耗时:7740毫秒

开始做任务二

完成任务二,耗时:723毫秒

开始做任务三2017-04-28 18:03:03.415 WARN 11016 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart ifthe connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect2017-04-28 18:03:03.415 INFO 11016 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0完成任务三,耗时:5047毫秒

异步调用

上述的同步调用虽然顺利的执行完了三个任务,但是可以看到执行时间比较长,若这三个任务本身之间不存在依赖关系,可以并发执行的话,同步调用在执行效率方面就比较差,可以考虑通过异步调用的方式来并发执行。

在Spring Boot中,我们只需要通过使用@Async注解就能简单的将原来的同步函数变为异步函数,Task类改在为如下模式:

packagecom.dxz.demo1;importjava.util.Random;importorg.springframework.scheduling.annotation.Async;importorg.springframework.stereotype.Component;importorg.springframework.web.bind.annotation.RequestMapping;/*** 定义3个任务*/@Componentpublic classTask2 {//定义一个随机对象.

public static Random random = newRandom();//任务一;

@Asyncpublic void doTaskOne() throwsException {

System.out.println("开始做任务一");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务一,耗时:" + (end - start) + "毫秒");

}//任务二;

@Asyncpublic void doTaskTwo() throwsException {

System.out.println("开始做任务二");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务二,耗时:" + (end - start) + "毫秒");

}//任务3;

@Asyncpublic void doTaskThree() throwsException {

System.out.println("开始做任务三");long start =System.currentTimeMillis();

Thread.sleep(random.nextInt(10000));long end =System.currentTimeMillis();

System.out.println("完成任务三,耗时:" + (end - start) + "毫秒");

}

}

为了让@Async注解能够生效,还需要在Spring Boot的主程序中配置@EnableAsync,如下所示:

packagecom.dxz;importorg.springframework.boot.SpringApplication;importorg.springframework.boot.autoconfigure.SpringBootApplication;importorg.springframework.scheduling.annotation.EnableAsync;

@EnableAsync

@SpringBootApplicationpublic classHelloApplication {public static voidmain(String[] args) {

SpringApplication.run(HelloApplication.class, args);

}

}

编写测试方法:

packagecom.dxz.demo1;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.SpringApplicationConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importorg.springframework.web.bind.annotation.RequestMapping;importcom.dxz.HelloApplication;

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes= HelloApplication.class)public classTask2Test {

@AutowiredprivateTask2 task2;//测试task1.

@Testpublic void task1() throwsException{

task2.doTaskOne();

task2.doTaskTwo();

task2.doTaskThree();

}

}

此时可以反复执行单元测试,您可能会遇到各种不同的结果,比如:

开始做任务一

开始做任务二

开始做任务三

修改下测试类:

packagecom.dxz.demo1;importjava.util.concurrent.TimeUnit;importorg.junit.Test;importorg.junit.runner.RunWith;importorg.springframework.beans.factory.annotation.Autowired;importorg.springframework.boot.test.SpringApplicationConfiguration;importorg.springframework.test.context.junit4.SpringJUnit4ClassRunner;importorg.springframework.web.bind.annotation.RequestMapping;importcom.dxz.HelloApplication;

@RunWith(SpringJUnit4ClassRunner.class)

@SpringApplicationConfiguration(classes= HelloApplication.class)public classTask2Test {

@AutowiredprivateTask2 task2;//测试task1.

@Testpublic void task1() throwsException {

task2.doTaskOne();

task2.doTaskTwo();

task2.doTaskThree();

System.out.println("i'm here");

TimeUnit.SECONDS.sleep(15);

System.out.println("over");

}

}

jieguo:

i'm here

开始做任务二

开始做任务一

开始做任务三

完成任务三,耗时:1280毫秒

2017-04-28 18:25:36.936 WARN 17848 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

2017-04-28 18:25:36.938 INFO 17848 --- [cTaskExecutor-1] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0

完成任务一,耗时:4951毫秒

完成任务二,耗时:7451毫秒

2017-04-28 18:25:42.971 WARN 17848 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Consumer raised exception, processing can restart if the connection factory supports it. Exception summary: org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connect

2017-04-28 18:25:42.972 INFO 17848 --- [cTaskExecutor-2] o.s.a.r.l.SimpleMessageListenerContainer : Restarting Consumer: tags=[{}], channel=null, acknowledgeMode=AUTO local queue size=0

over

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值