阅读 164

springboot异步调用demo

springboot异步调用demo

异步调用是相对于同步调用而言的,同步调用是指程序按预定顺序一步步执行,每一步必须等到上一步执行完成之后才能执行,而异步调用则无需等待上一步程序执行完成即可执行。最好往数据库里多添加数据,效果明显。


1、IDEA初始化springboot项目,勾选相关工具mybatis

2、创建user数据表,往里面添加数据,越多越好,我添加了10w条,数据可以从GroupLens网站中下载



3、创建实体类,该实体类需要实现Serializable

package net.maple.springboot.model;


import java.io.Serializable;


public class User implements Serializable {

    private int id;

    private String username;

    private String password;


    public int getId() {

        return id;

    }


    public void setId(int id) {

        this.id = id;

    }


    public String getUsername() {

        return username;

    }


    public void setUsername(String username) {

        this.username = username;

    }


    public String getPassword() {

        return password;

    }


    public void setPassword(String password) {

        this.password = password;

    }

}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

4、创建dao,注意加@Mapper

package net.maple.springboot.dao;

import net.maple.springboot.model.User;

import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import java.util.concurrent.Future;

/**

 * dao

 * @author Administrator

 */

@Mapper

public interface UserDao {

    /**

     * 普通查询

     * @return

     */

    List<User> getAll();

    /**

     * 异步查询

     * @return

     */

    Future<List<User>> asynGetAll();


}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

5、创建service

package net.maple.springboot.service;

import net.maple.springboot.model.User;

import java.util.List;

import java.util.concurrent.Future;

/**

 * service

 * @author Administrator

 */

public interface UserService {

    /**

     * 普通查询

     * @return

     */

    List<User> getAll();

    /**

     * 异步查询

     * @return

     */

    Future<List<User>> asynGetAll();


}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

6、创建impl,注意加@Service,异步查询的方法上加@Async

package net.maple.springboot.service.impl;


import net.maple.springboot.dao.UserDao;

import net.maple.springboot.model.User;

import net.maple.springboot.service.UserService;

import org.springframework.scheduling.annotation.Async;

import org.springframework.scheduling.annotation.AsyncResult;

import org.springframework.stereotype.Service;

import javax.annotation.Resource;

import java.util.Collections;

import java.util.List;

import java.util.concurrent.Future;


@Service

public class UserServiceImpl implements UserService {

    @Resource

    private UserDao userDao;


    @Override

    public List<User> getAll() {

        try {

            System.out.println("开始做任务");

            long start=System.currentTimeMillis();

            List<User> userList=userDao.getAll();

            long end=System.currentTimeMillis();

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

            return userList;

        }catch (Exception e){

            return Collections.emptyList();

        }

    }


    @Override

    @Async

    public Future<List<User>> asynGetAll() {

        try {

            System.out.println("开始做任务");

            long start=System.currentTimeMillis();

            List<User> userList= (List<User>) userDao.asynGetAll();

            long end=System.currentTimeMillis();

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

            return new AsyncResult<>(null);

        }catch (Exception e){

            return new AsyncResult<>(null);

        }

    }

    

}


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

application.yml

spring:

  #数据源

  datasource:

    name: test

    url: jdbc:mysql://127.0.0.1:3306/test?serverTimezone=Asia/Shanghai

    username: 你的数据库用户名

    password: 你的数据库密码

    #druid

    type: com.alibaba.druid.pool.DruidDataSource

    driver-class-name: com.mysql.cj.jdbc.Driver


#mybatis

mybatis:

  mapper-locations: classpath:mapper/*.xml  #对应mapper映射xml文件的所在路径

  type-aliases-package: net.maple.springboot.model  #对应实体类的路径


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

Usermapper.xml

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"

        "http://mybatis.org/dtd/mybatis-3-mapper.dtd">


<mapper namespace="net.maple.springboot.dao.UserDao">


    <select id="getAll" resultType="net.maple.springboot.model.User">

        select id,username,password from user

    </select>



</mapper>

1

2

3

4

5

6

7

8

9

10

11

12

开始测试,先测试普通查询

@SpringBootTest

class SpringbootAsynApplicationTests {


    @Resource

    private UserService userService;


    @Test

    public void test(){

        long start=System.currentTimeMillis();

        System.out.println("第1次查询用户");

        List<User> userList1=userService.getAll();

        System.out.println("第2次查询用户");

        List<User> userList2=userService.getAll();

        System.out.println("第3次查询用户");

        List<User> userList3=userService.getAll();

        long end=System.currentTimeMillis();

        System.out.println("总共耗时:"+(end-start)+"毫秒");

    }


}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20



普通查询大概7s,再测试异步查询,注意添加@EnableAsync

@EnableAsync

@SpringBootTest

class SpringbootAsynApplicationTests {


    @Resource

    private UserService userService;


    @Test

    public void testAsync() throws InterruptedException {

        long start=System.currentTimeMillis();

        System.out.println("第1次查询用户");

        Future<List<User>> userList1=userService.asynGetAll();

        System.out.println("第2次查询用户");

        Future<List<User>> userList2=userService.asynGetAll();

        System.out.println("第3次查询用户");

        Future<List<User>> userList3=userService.asynGetAll();

        while(true){

            if(userList1.isDone()&&userList2.isDone()&&userList3.isDone()){

                break;

            }else{

                Thread.sleep(10);

            }

        }

        long end=System.currentTimeMillis();

        System.out.println("总共耗时:"+(end-start)+"毫秒");

    }


}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28



结果真是很amazing啊,只用了51ms!

————————————————

版权声明:本文为CSDN博主「绯色月下、狂咲ノ绝」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。

原文链接:https://blog.csdn.net/qq_44820964/article/details/114708215


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐