欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

springboot异步调用demo

程序员文章站 2022-05-03 17:39:41
...
第一步建立一个类
package com.zys.async;

import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

@Component
@EnableAsync //开启异步调用
public class AsyncDemo {


@Async
    public void sendSms(){
        System.out.println("####sendSms####   2");
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        System.out.println("####sendSms####   3");
    }
}


第二步:测试即可
@Autowired

private AsyncDemo asyncDemo;



在controller 测试即可
asyncDemo.sendSms();
相关标签: Async springboot