zoukankan      html  css  js  c++  java
  • 异步方法(九)

    创建工程

    在pom文件引入相关依赖:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
     
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-web</artifactId>
            </dependency>
            <dependency>
                <groupId>com.fasterxml.jackson.core</groupId>
                <artifactId>jackson-databind</artifactId>
            </dependency>

      

    创建一个接收数据的实体:

    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
    @JsonIgnoreProperties(ignoreUnknown=true)
    public class User {
     
        private String name;
        private String blog;
     
        public String getName() {
            return name;
        }
     
        public void setName(String name) {
            this.name = name;
        }
     
        public String getBlog() {
            return blog;
        }
     
        public void setBlog(String blog) {
            this.blog = blog;
        }
     
        @Override
        public String toString() {
            return "User [name=" + name + ", blog=" + blog + "]";
        }
     
    }

      

    创建一个请求的 githib的service:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    @Service
    public class GitHubLookupService {
     
        private static final Logger logger = LoggerFactory.getLogger(GitHubLookupService.class);
     
        private final RestTemplate restTemplate;
     
        public GitHubLookupService(RestTemplateBuilder restTemplateBuilder) {
            this.restTemplate = restTemplateBuilder.build();
        }
     
        @Async
        public Future<User> findUser(String user) throws InterruptedException {
            logger.info("Looking up " + user);
            String url = String.format("https://api.github.com/users/%s", user);
            User results = restTemplate.getForObject(url, User.class);
            // Artificial delay of 1s for demonstration purposes
            Thread.sleep(1000L);
            return new AsyncResult<>(results);
        }
     
    }

      

    通过,RestTemplate去请求,另外加上类@Async 表明是一个异步任务。

    开启异步任务:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    @SpringBootApplication
    @EnableAsync
    public class Application extends AsyncConfigurerSupport {
     
        public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
        }
     
        @Override
        public Executor getAsyncExecutor() {
            ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
            executor.setCorePoolSize(2);
            executor.setMaxPoolSize(2);
            executor.setQueueCapacity(500);
            executor.setThreadNamePrefix("GithubLookup-");
            executor.initialize();
            return executor;
        }
     
    }

      

    通过@EnableAsync开启异步任务;并且配置AsyncConfigurerSupport,比如最大的线程池为2.

  • 相关阅读:
    Ubuntu 出现 apt-get问题的解决方法
    Conversion to Dalvik format failed: Unable to execute dex: null
    java中判断是否为中文
    解决:Unable to connect to repository https://dl-ssl.google.com/android/eclipse/site.xml
    Android 获取本机WIFI及3G网络IP
    xp重装系统后恢复Linux启动
    Android中空格及换行
    ubuntu12.10设置thunderbird开机自启动
    web安全及防护
    回归基础: JavaScript 变量提升
  • 原文地址:https://www.cnblogs.com/MaxElephant/p/10231995.html
Copyright © 2011-2022 走看看