异步任务
- 使用注解@Async标注异步方法
@Async("asyncServiceExecutor")
public void secKillOrderAysn(Integer productId) {
//从redis的任务列表中取出任务执行
SeckillOrderBO seckillOrderBO = (SeckillOrderBO) redisUtil.lGet(SeckillOrderBO.class.getSimpleName() + productId);
if (seckillOrderBO != null) {
Integer userId = seckillOrderBO.getUserId();
productId = seckillOrderBO.getProductId();
//生成秒杀订单
MmallOrder secKillOrder = this.assembleOrder(userId, null, Constant.ORDER_TYPE_SECKILL);
//将订单插入数据库中
mmallOrderDao.insertSelective(secKillOrder);
//生成order_item信息
//查询某个商品在redis中的信息
MmallSeckillGoods seckillGoods = (MmallSeckillGoods) redisUtil.hget(MmallSeckillGoods.class.getSimpleName(), productId + "");
MmallOrderItem mmallOrderItem = this.assembleOrderItem(secKillOrder, seckillGoods);
//将订单项信息存入数据库中
mmallOrderItemDao.insertSelective(mmallOrderItem);
synchronized (SecKillOrderAysnServiceImpl.class) {
//将秒杀商品库存减一
int stock = seckillGoods.getStock() - 1;
seckillGoods.setStock(stock);
if (stock <= 0) {
//如果库存为0,则更新数据库,并删除redis中该秒杀商品信息
seckillGoodsDao.updateByPrimaryKey(seckillGoods);
redisUtil.hdel(MmallSeckillGoods.class.getSimpleName(), productId + "");
} else {
//如果不为0,则更新redis
redisUtil.hset(MmallSeckillGoods.class.getSimpleName(), productId + "", seckillGoods);
}
}
}
}
- 使用@EnableAsync开启异步任务
@EnableAsync
@SpringbootApplication
public class SpringbootLearning2Application {
public static void main(String[] args) {
//System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(SpringbootLearning2Application.class, args);
}
}
- 自定义配置线程池
@Configuration
public class ThreadPoolConfig {
@Bean
public Executor asyncServiceExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
//配置核心线程数
executor.setCorePoolSize(5);
//配置最大线程数
executor.setMaxPoolSize(5);
//配置队列大小
executor.setQueueCapacity(99999);
//配置线程池中的线程的名称前缀
executor.setThreadNamePrefix("async-service-");
// rejection-policy:当pool已经达到max size的时候,如何处理新任务
// CALLER_RUNS:不在新线程中执行任务,而是有调用者所在的线程来执行
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
//执行初始化
executor.initialize();
return executor;
}
}
定时任务
- 使用@Scheduled(cron="秒 分 时 日 月 周 年") 来标注定时任务
/**
* 每天凌晨将秒杀商品同步到redis中
*/
@Scheduled(cron = "0 0 0 * * ?")
public void synchronizedSecKillProductToRedis() {
System.out.println("进入定时任务。。。");
Date currentTime = new Date();
long planDateMill = currentTime.getTime() + 3 * 24 * 60 * 60 * 1000L;
System.out.println(planDateMill);
Date planDate = new Date();
planDate.setTime(planDateMill);
System.out.println(planDate);
List<MmallSeckillGoods> seckillGoodsList = seckillGoodsDao.selectSecKillProducts(planDate);
for (MmallSeckillGoods seckillGoods : seckillGoodsList) {
//将商品信息加入缓存中
redisUtil.hset(MmallSeckillGoods.class.getSimpleName(), seckillGoods.getId() + "", seckillGoods);
//创建队列表示某个商品有多少个
for (int i = 0; i < seckillGoods.getStock(); i++) {
redisUtil.lSet(Constant.SECKILL_PRODUCT_PREFIX + seckillGoods.getId(), seckillGoods.getId());
}
}
}
- 使用@EnableScheduling注解来开启定时任务
@SpringBootApplication
@EnableScheduling
public class SpringbootLearning2Application {
public static void main(String[] args) {
//System.setProperty("es.set.netty.runtime.available.processors", "false");
SpringApplication.run(SpringbootLearning2Application.class, args);
}
}
邮件任务
- 导包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
- 配置
#邮件配置
#发送方的邮箱地址
spring.mail.username=2268313664@qq.com
#发送方的授权码,由邮箱设置中获得
spring.mail.password=xyaczjkchgyddibc
#smtp服务器地址
spring.mail.host=smtp.qq.com
#开启安全认证
spring.mail.properties.mail.smtp.ssl.enable=true
- 使用
@Autowired
private JavaMailSenderImpl mailSender;
//发送普通邮件
@Test
public void sendMail() {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setSubject("通知");
simpleMailMessage.setText("今晚8点开会");
simpleMailMessage.setTo("xxx@qq.com");
simpleMailMessage.setFrom("yyy@qq.com");
mailSender.send(simpleMailMessage);
}
//发送带附件的邮件
@Test
public void sendMail2() {
MimeMessage mimeMessage = mailSender.createMimeMessage();
MimeMessageHelper helper = null;
try {
helper = new MimeMessageHelper(mimeMessage, true);
helper.setSubject("通知");
helper.setText("<html><body><h1>快点滚回来</h1></body></html>", true);
helper.setFrom("xxx@qq.com");
helper.setTo("yyy@qq.com");
helper.addAttachment("a.png", new File("C:\\Users\\lyl\\Desktop\\微信图片_20190822171706.png"));
mailSender.send(mimeMessage);
} catch (MessagingException e) {
e.printStackTrace();
}
}