启动类
包
import org.springframework.scheduling.annotation.EnableAsync;
注解
@EnableAsync
整体
package com.ruoyi;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.scheduling.annotation.EnableAsync;
/**
* 启动程序
*
* @author ruoyi
*/
@SpringBootApplication(exclude = { DataSourceAutoConfiguration.class })
@EnableAsync
public class RuoYiApplication
{
public static void main(String[] args)
{
// System.setProperty("spring.devtools.restart.enabled", "false");
SpringApplication.run(RuoYiApplication.class, args);
System.out.println("(♥◠‿◠)ノ゙ 启动成功 ლ(´ڡ`ლ)゙ \n");
}
}
异步方法
异步类需要交给Spring管理 @Component
异步方法需要@Async修饰文章来源:https://www.toymoban.com/news/detail-508443.html
package com.ruoyi.common.async;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;
@Component
public class SysUserAsync {
@Async
public void JobOne() throws InterruptedException {
System.out.println("开始执行任务一");
long l1 = System.currentTimeMillis();
Thread.sleep(2000);
long l2 = System.currentTimeMillis();
System.out.println("任务一用时" + (l2 - l1));
}
@Async
public void JobTwo() throws InterruptedException {
System.out.println("开始执行任务二");
long l1 = System.currentTimeMillis();
Thread.sleep(2000);
long l2 = System.currentTimeMillis();
System.out.println("任务二用时" + (l2 - l1));
}
@Async
public void JobThree() throws InterruptedException {
System.out.println("开始执行任务三");
long l1 = System.currentTimeMillis();
Thread.sleep(2000);
long l2 = System.currentTimeMillis();
System.out.println("任务三用时" + (l2 - l1));
}
}
测试
package com.ruoyi.system.service.impl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.validation.Validator;
import com.ruoyi.common.async.SysUserAsync;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import com.ruoyi.common.annotation.DataScope;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.ruoyi.system.domain.SysPost;
import com.ruoyi.system.domain.SysUserPost;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysPostMapper;
import com.ruoyi.system.mapper.SysRoleMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserPostMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
/**
* 用户 业务层处理
*
* @author ruoyi
*/
@Service
public class SysUserServiceImpl implements ISysUserService {
private static final Logger log = LoggerFactory.getLogger(SysUserServiceImpl.class);
@Autowired
private SysUserMapper userMapper;
@Autowired
private SysUserAsync sysUserAsync;
/**
* 根据条件分页查询用户列表
*
* @param user 用户信息
* @return 用户信息集合信息
*/
@Override
@DataScope(deptAlias = "d", userAlias = "u")
public List<SysUser> selectUserList(SysUser user) throws InterruptedException {
System.out.println("开始访问");
long l1 = System.currentTimeMillis();
sysUserAsync.JobOne();
sysUserAsync.JobTwo();
sysUserAsync.JobThree();
long l2 = System.currentTimeMillis();
System.out.println("结束访问,用时" + (l2 - l1));
return userMapper.selectUserList(user);
}
}
文章来源地址https://www.toymoban.com/news/detail-508443.html
到了这里,关于SpringBoot--@Async异步的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!