36、springboot——异步任务、定时任务、邮件任务
2021-02-17 00:19
标签:add idt day pen async xxx python ice asio 测试如下 1、不是异步方法的时候: 进行等待三秒再进行应答 controller类: 访问链接服务器3秒之后才给出响应; 2、是异步方法的时候 要想@Async注解起效果还需开启允许异步注解 定时任务方法(这里的例子是星期一到星期六的每天的每小时的每分钟打印一次定时任务开启) 开启允许定时任务的注解 启动服务之后没隔一分钟就打印一次 在任意分钟的0-4s进行打印 查看springboot的mail的相关自动配置 注入的邮件操作类 相关的配置属性 我们是通过中间服务器来发送邮箱的 授权密码的获取: 进入qq邮箱,点击设置
点击账户 先开启smtp服务,然后点击获取授权码(需要发送短信) 测试成功 测试成功 36、springboot——异步任务、定时任务、邮件任务 标签:add idt day pen async xxx python ice asio 原文地址:https://www.cnblogs.com/lyh233/p/12702329.html一、异步任务
@Service
public class AsynService {
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据.....");
}
}
@RestController
public class AsyncController {
@Autowired
AsyncService asyncService;
@GetMapping("/hello")
public String hello(){
asyncService.hello();
return "success";
}
}
@Service
public class AsynService {
//告诉spring这是一个异步的方法
@Async
public void hello(){
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("处理数据.....");
}
}
@EnableAsync //开启异步注解
@SpringBootApplication
public class Springboot04TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot04TaskApplication.class, args);
}
}
二、定时任务
@Service
public class ScheduleService {
//定时任务
//cron:second、minute、hour、day of month、day of week
@Scheduled(cron = "0 * * * * MON-SAT")
public void hello(){
System.out.println("定时任务开启...");
}
}
@EnableScheduling //开启基于注解的定时任务
@SpringBootApplication
public class Springboot04TaskApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot04TaskApplication.class, args);
}
}
@Scheduled(cron = "0-4 * * * * 0-7")
public void hello(){
System.out.println("定时任务开启...");
}
三、邮件任务
1、引入邮件的依赖
dependency>
groupId>org.springframework.bootgroupId>
artifactId>spring-boot-starter-mailartifactId>
dependency>
@Bean
JavaMailSenderImpl mailSender(Session session) {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
sender.setSession(session);
return sender;
}
@ConfigurationProperties(
prefix = "spring.mail"
)
public class MailProperties {
private static final Charset DEFAULT_CHARSET;
private String host;
private Integer port;
private String username;
private String password;
private String protocol = "smtp";
private Charset defaultEncoding;
private Map
2、测试发送简单邮件
2.1.配置文件中进行文件配置
spring.mail.username=xxxxxxxxx@qq.com
//注意是授权密码,非登录密码
spring.mail.password=rmdwtmbkrwpibddd
spring.mail.host=smtp.qq.com
//配置ssl安全连接
spring.mail.properties.mail.smtp.ssl.enable=true
2.2 写测试方法
@Autowired
JavaMailSenderImpl mailSender;
@Test
void contextLoads() {
SimpleMailMessage mailMessage = new SimpleMailMessage();
//邮件设置
//标题
mailMessage.setSubject("通知,下午开会");
//内容
mailMessage.setText("3点钟");
//收件人邮箱
mailMessage.setTo("xxxxxxxxxx@qq.com");
//发件人邮箱
mailMessage.setFrom("xxxxxxxxx@qq.com");
mailSender.send(mailMessage);
}
2.3、测试发送复杂邮件(带附件,html代码片段。。。)
@Test
public void test02() throws MessagingException {
//创建一个复杂的消息邮件
MimeMessage mimeMessage = mailSender.createMimeMessage();
//用mimeMessageHelper对邮箱进行设置
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage,true);
//标题
mimeMessageHelper.setSubject("通知,下午开会");
//内容(可含html代码片段的话在后面添加参数true)
mimeMessageHelper.setText("4点钟",true);
//收件人邮箱
mimeMessageHelper.setTo("1372365122@qq.com");
//发件人邮箱
mimeMessageHelper.setFrom("973951192@qq.com");
//上传附件
mimeMessageHelper.addAttachment("小黄人",new File("E:\\pythonTest\\alien_invasion\\images\\icon.jpg"));
mailSender.send(mimeMessage);
}
文章标题:36、springboot——异步任务、定时任务、邮件任务
文章链接:http://soscw.com/index.php/essay/56330.html