SpringBoot高级整合系列 任务(异步,定时,邮件 任务)

本文介绍SpringBoot中异步任务、定时任务及邮件发送的实现方式。包括如何使用@Async注解实现异步处理,利用@Scheduled注解进行定时任务调度,并提供发送简单邮件和复杂邮件的示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

SpringBoot高级整合系列 任务(异步,定时,邮件 任务)

新建SpringBoot项目

在这里插入图片描述

异步任务

新建service
通过线程让他睡3秒钟

package com.luyi.service;

import org.springframework.stereotype.Service;

/**
 * 异步任务
 * @author 卢意
 * @create 2020-11-03 16:39
 */
@Service
public class AsyncService {
	public void hello(){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("处理数据中");
	}
}

新建controller类

package com.luyi.controller;

import com.luyi.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author 卢意
 * @create 2020-11-03 16:41
 */
@RestController
public class AsyncController {

	@Autowired
	AsyncService asyncService;

	@GetMapping("/hello")
	public String hello(){
		asyncService.hello();
		return "success";
	}
}

启动测发现会等待三秒钟
在这里插入图片描述
加入@Async注解

package com.luyi.service;

import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

/**
 * 异步任务
 * @author 卢意
 * @create 2020-11-03 16:39
 */
@Service
public class AsyncService {
	//告诉spring这是一个异步的方法  spring就会开一个线程池对她进行调用
	@Async
	public void hello(){
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println("处理数据中");
	}
}

主启动加入注解

package com.luyi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;


@EnableAsync //开启异步注释功能
@SpringBootApplication
public class SpringbootTaskApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootTaskApplication.class, args);
	}

}

启动发现不用等三秒了

定时任务

在这里插入图片描述
cron参数
在这里插入图片描述

demo1

新建service类

package com.luyi.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 卢意
 * @create 2020-11-03 16:52
 */
@Service
public class ScheduleService {

	@Scheduled(cron = "0 * * * * *")
	public void hello(){
		System.out.println("hello----");
	}
}

主启动类加入注解

package com.luyi;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;


@EnableAsync //开启异步注释功能
@EnableScheduling// 开启基于注解的定时任务
@SpringBootApplication
public class SpringbootTaskApplication {

	public static void main(String[] args) {
		SpringApplication.run(SpringbootTaskApplication.class, args);
	}

}

每分钟的整点会执行一下该方法 如16:00
在这里插入图片描述

demo2

在每分钟的0,1 ,2 ,3 ,4 五个时刻执行方法

package com.luyi.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 卢意
 * @create 2020-11-03 16:52
 */
@Service
public class ScheduleService {

	//@Scheduled(cron = "0 * * * * *")
	@Scheduled(cron = "0,1,2,3,4 * * * * *")
	public void hello(){
		System.out.println("hello----");
	}
}

@Scheduled(cron = "0-4 * * * * *") 区间的方式  也可以实现

demo3

每4秒执行一次

package com.luyi.service;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

/**
 * @author 卢意
 * @create 2020-11-03 16:52
 */
@Service
public class ScheduleService {

	//@Scheduled(cron = "0 * * * * *")
	//@Scheduled(cron = "0,1,2,3,4 * * * * *")
	//@Scheduled(cron = "0-4 * * * * *")
	@Scheduled(cron = "0/4 * * * * *")

	public void hello(){
		System.out.println("hello----");
	}
}

?冲突匹配

如果选择了每个月1号执行

在周的选项只能使用?
反之也一样 如果打算 每个星期1执行方法
在日期参数也要写?

星期参数

可以用英文 也可以用数字

注意 0-7 0和7都是表示周日

例子

在这里插入图片描述

邮件任务

在这里插入图片描述
流程

简单邮件

加入依赖
在这里插入图片描述
配置application.properties
在这里插入图片描述
在添加 一个配置 spring.mail.properties.mail.stmp.ssl.enable=true
测试类

package com.luyi;

import org.junit.jupiter.api.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest
class SpringbootTaskApplicationTests {

	@Autowired
	JavaMailSenderImpl JavaMailSender;
	@Test
	void contextLoads() {
		SimpleMailMessage simpleMailMessage=new SimpleMailMessage();
		//邮件设置
		simpleMailMessage.setSubject("哈哈哈");
		simpleMailMessage.setText("我是卢意");
		simpleMailMessage.setTo("xxx@qq.com");
		simpleMailMessage.setFrom("xxx@qq.com");
		JavaMailSender.send(simpleMailMessage);
	}

}

不懂为什么显示JavaMailSenderImpl 没有注入Bean

参考网上方法 自己写一个配置类 再注入bean

package com.luyi.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 * @author 卢意
 * @create 2020-11-03 17:30
 */
@Configuration
public class MailConfig {


	@Bean
	public JavaMailSenderImpl JavaMailSender() {
		JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
		mailSender.setHost("smtp.qq.com");
		mailSender.setUsername("****@qq.com");
		mailSender.setPassword("xmmwrfoepzc");
		return mailSender;
	}
}


复杂邮件邮件

@Test
	void contextLoads2() throws MessagingException {
		//复杂邮件
		//创建一个复杂的邮件

		MimeMessage mimeMessage=JavaMailSender.createMimeMessage();
		MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

		//邮件设置
		helper.setSubject("哈哈哈");
		helper.setText("<b style='color:red'>我是卢意</b>",true);// 开启html 
		helper.setTo("XXXX@qq.com");
		helper.setFrom("XXXX@qq.com");
		//helper 上传文件
		helper.addAttachment("luyi.jpg", new File("C:\\Users\\71871\\Desktop\\luyi.jpg"));
		JavaMailSender.send(mimeMessage);
	}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值