springboot异步

一: 在 MyConfiguration.java 中开启注解

@Configuration//指明当前类是一个配置类;就是来替代之前的Spring配置文件
@EnableAsync//开启定时任务 public class MyConfiguration {
}

二: 在Service.java的方法上标注@Async

SpringBoot 定时任务

高频访问地址:

在线Cron表达式生成器==>http://cron.qqe2.com/

Cron表达式出现的顺序依次为:

字段

允许值

允许的特殊字符

0-59

, - * /

0-59

, - * /

小时

0-23

, - * /

日期

1-31

, - * ? / L W C

月份

1-12

, - * /

星期

0-7或SUN-SAT 0,7代表SUN , 1代表MON, 6代表SAT

, - * ? / L C #

特殊字符含意:

特殊字符

代表含义

举例

说明

,

枚举

0 0 1,2,3 * * ?

每天的1点,2点,3点执行一次

-

区间

0 0 2-4 * * ?

每天凌晨2点到4点期间,每个整点都执行一次;

*

任意

   

/

步长

0 0/5 14,18 * * ?

每天14点整,和18点整,每隔5分钟执行一次

?

代表未知,
日/星期冲突匹配
(即每4位和第6位不能同时为?未知)

0 0 1 1 * ?
0 0 1 ? * 1

  每月1号的1点执行
  每周一的1点执行

L

最后

0 0 2 ? * 6L

每个月的最后一个周六凌晨2点执行一次

W

工作日

0 0 2 LW * ?

每个月的最后一个工作日凌晨2点执行一次

C

和calendar联系后计算过的值

 未研究  

#

星期,4#2,第2个星期四

0 0 1 * * 4#2

每月的第二个星期4的1点执行一次

一: 在 MyConfiguration.java 中开启注解

@Configuration//指明当前类是一个配置类;就是来替代之前的Spring配置文件
@EnableScheduling//开启定时任务
public class MyConfiguration {
}

二: 在Service.java的方法上标注@Scheduled

/**
* 定时任务类
*/
@Service
public class ScheduledTaskService {
/*注解这是一个异步任务, 将无阻塞地返回结果*/ /**
* 在线Cron表达式地址: http://cron.qqe2.com/
* 我的笔记地址:
* second(秒), minute(分), hour(时), day of month(日), month(月), day of week(周几).
*
* @Scheduled(cron = "0 * * * * MON-SAT")//每个月的
* @Scheduled(cron = "0 0/5 14,18 * * ?")// 每天14点整,和18点整,每隔5分钟执行一次
* @Scheduled(cron = "0 15 10 ? * 1-6")// 每个月的周一至周六10:15分执行一次
* @Scheduled(cron = "0 0 2 ? * 6L")//每个月的最后一个周六凌晨2点执行一次
* @Scheduled(cron = "0 0 2 LW * ?")//每个月的最后一个工作日凌晨2点执行一次
* @Scheduled(cron = "0 0 2-4 * * ?")//每天凌晨2点到4点期间,每个整点都执行一次;
* @Scheduled(cron = "0 0 1,2,3 * * ?")//每天的1点,2点,3点执行一次
* @Scheduled(cron = "0 0 1 * * 4#2")//每月的第二个星期4的1点执行一次
*/
@Scheduled(cron = "0 * * * * ?") // 需要在 MyConfiguration.java中开启异步注解 @EnableScheduling
public String doTask() {
System.out.println("定时任务 started");
for (int i = 1; i < 2; i++) {
System.out.println("定时任务 working " + i + " s");
}
System.out.println("定时任务 finished");
return "ScheduledTaskService finished";
}
}

SpringBoot 邮件

邮件发送机制: 发件人@qq.com --> qq邮箱服务器 --> 163邮箱服务器 --> 收件人@163.com

1. pom.xml引入依赖

        <!--邮件发送starter, 内部用的是javax.mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.内部使用的是MailSenderAutoConfiguration自动配置类

MailSenderAutoConfiguration

@Import({ MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class })
public class MailSenderAutoConfiguration {

MailSenderPropertiesConfiguration

@Configuration
@ConditionalOnProperty(prefix = "spring.mail", name = "host")
class MailSenderPropertiesConfiguration { private final MailProperties properties; MailSenderPropertiesConfiguration(MailProperties properties) {
this.properties = properties;
} @Bean
@ConditionalOnMissingBean
public JavaMailSenderImpl mailSender() {
JavaMailSenderImpl sender = new JavaMailSenderImpl();
applyProperties(sender);
return sender;
}
......

在application.properties中mail属性

配置服务器地址,用户名,邮箱密码等

spring.mail.username=jianyuan_5731@qq.com
# POP3/SMTP服务 gaaolyyinjcqbhbc , IMAP/SMTP服务 xyusabvixxdbjeh
spring.mail.password=xyusuoidbjeh
spring.mail.host=smtp.qq.com
# qq需要ssl支持,以前会报错,但现在就算没有也不会报错了
spring.mail.properties.mail.smtp.ssl.enable=true

自动包装JavaMailSender类

MailController.java

package com.example.demo.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File; @RequestMapping("/mail")
@Controller
public class MailController {
private static final Logger logger = LoggerFactory.getLogger(MailController.class); @Autowired
JavaMailSender javaMailSender; // address: http://localhost:8080/springbootdemo/mail/send?subject=hello&text=helloBeauty
//@Async//如果可以就尽量开启异步发送
@ResponseBody
@RequestMapping("/send")
public String mainSend(String subject , String text ,String from ) {
//SimpleMailMessage用于发送纯文本信息
SimpleMailMessage message = new SimpleMailMessage();
message.setSubject(subject);
message.setText(text);
message.setTo("1713930654@qq.com");//可以是字符串列表
message.setFrom("jianyuan_5731@qq.com");//这里的setFrom一定要和application.properties中配置的username一致, 不然会报mail from address must be same as authorization user错误
javaMailSender.send(message); return "mainSend邮件发送成功";
} //address: http://localhost:8080/springbootdemo/mail/sendHtmlAndFile?subject=hello&text=helloBeauty
//@Async//如果可以就尽量开启异步发送
@ResponseBody
@RequestMapping("/sendHtmlAndFile")
public String mainHtmlAndFile(String subject , String text ) throws MessagingException {
MimeMessage message = javaMailSender.createMimeMessage();
//包装MimeMessage和声明是multipart为 true 的邮件
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message,true);
mimeMessageHelper.setSubject(subject);
mimeMessageHelper.setText("<b style='color:red'>helloBoy</b>");
mimeMessageHelper.setTo("1713930654@qq.com");
mimeMessageHelper.setFrom("jianyuan_5731@qq.com");//mail from address must be same as authorization user
//添加附件到message中
mimeMessageHelper.addAttachment("shortMessage.txt",new File("D:\\file\\短信.txt"));
mimeMessageHelper.addAttachment("beauty.jpg",new File("D:\\file\\妮诺.jpg"));
javaMailSender.send(message); return "mainHtmlAndFile邮件发送成功";
}
}

SpringBoot 异步 定时任务 邮件的更多相关文章

  1. SpringBoot整合定时任务和异步任务处理 3节课

    1.SpringBoot定时任务schedule讲解   定时任务应用场景: 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类        ...

  2. SpringBoot整合定时任务和异步任务处理

    SpringBoot定时任务schedule讲解 简介:讲解什么是定时任务和常见定时任务区别 1.常见定时任务 Java自带的java.util.Timer类 timer:配置比较麻烦,时间延后问题, ...

  3. SpringBoot学习笔记(七):SpringBoot使用AOP统一处理请求日志、SpringBoot定时任务@Scheduled、SpringBoot异步调用Async、自定义参数

    SpringBoot使用AOP统一处理请求日志 这里就提到了我们Spring当中的AOP,也就是面向切面编程,今天我们使用AOP去对我们的所有请求进行一个统一处理.首先在pom.xml中引入我们需要的 ...

  4. SpringBoot之异步定时任务

    如果每个Scheduled方法是同步执行的,万一有一个发生死锁,那么其他任务就没法执行,下面介绍异步定时任务 异步定时任务 Spring为任务调度与异步方法执行提供了注解支持,即通过在方法上设置@As ...

  5. 你有没有觉得邮件发送人固定配置在yml文件中是不妥当的呢?SpringBoot 动态设置邮件发送人

    明月当天,不知道你有没有思念的人 前言 之前其实已经写过SpringBoot异步发送邮件,但是今天在一个小项目中要用到发送邮件时,我突然觉得邮件发送人只有一个,并且固定写在yml文件中,就是非常的不妥 ...

  6. 玩转SpringBoot之定时任务详解

    序言 使用SpringBoot创建定时任务非常简单,目前主要有以下三种创建方式: 一.基于注解(@Scheduled) 二.基于接口(SchedulingConfigurer) 前者相信大家都很熟悉, ...

  7. SpringBoot 配置定时任务

    SpringBoot启用定时任务,其内部集成了成熟的框架,因此我们可以很简单的使用它. 开启定时任务 @SpringBootApplication //设置扫描的组件的包 @ComponentScan ...

  8. SpringBoot - 添加定时任务

    SpringBoot 添加定时任务 EXample1: import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.spri ...

  9. springboot之定时任务

    定时线程 说到定时任务,通常会想到JDK自带的定时线程来执行,定时任务. 回顾一下定时线程池. public static ScheduledExecutorService newScheduledT ...

随机推荐

  1. RN 开发工具及发布release版本

    2.1.开发工具推荐visual studio code https://code.visualstudio.com/docs/?dv=win 选择安装react native tool 就可以了 2 ...

  2. Mysql--数据表碎片优化方法

    碎片产生原因: 大量批量插入和删除操作数据库,基于线性表的顺序存储结构的特点,出现了大量的空间碎片.一.优化步骤: 1.查看整库的情况 2.方便优化 3.整库所有表, 包含行数 索引长度 碎片空间 二 ...

  3. 常用的git操作命令

    整理来源于廖雪峰的git教程https://www.liaoxuefeng.com git: 分布式版本控制系统  本地有完整的代码库,还有远程代码库 svn: 集中式版本控制系统 必须联网时才可提交 ...

  4. [转]js模块化(一)

    java有类文件.Python有import关键词.Ruby有require关键词.C#有using关键词.PHP有include和require.CSS有@import关键词,但是对ES5版本的ja ...

  5. httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:

    正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除

  6. Http post请求案例

    public RmiRespBase sendHttpRes(String jsonParamStr, String url, String apiName,String systemId,Strin ...

  7. GULP入门之API(二)

    GULP的API gulp.src(globs[, options]) 输出(Emits)符合所提供的匹配模式(glob)或者匹配模式的数组(array of globs)的文件. 将返回一个 Vin ...

  8. Promise的源码实现(符合Promise/A+规范)

    我们手写一个Promise/A+规范,然后安装测试脚本,以求通过这个规范. //Promise/A+源代码 // new Promise时,需要传递一个executor执行器,执行器立即执行 // e ...

  9. 解释性语言和非解释性语言,GIL锁

    解释性语言:python写的代码就被称为程序,cpu硬件能运行二进制代码指令.demo.py需要经过python解释器编译才做才能执行. 非解释性语言:例如c语言程序,同样需要写代码.demo.c这个 ...

  10. SQL _join on 和where的执行顺序

    left join :左连接,返回左表中所有的记录以及右表中连接字段相等的记录. right join :右连接,返回右表中所有的记录以及左表中连接字段相等的记录. inner join: 内连接,又 ...