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. Ionic JPush极光推送二

    1.看图解决问题   2.解决出现统计代码提示问题 修改这个java 文件 导入命名空间 import cn.jpush.android.api.JPushInterface; 添加方法 @Overr ...

  2. 【arc075f】AtCoder Regular Contest 075 F - Mirrored

    题意 给定一个数x,问有多少个正整数y,使得rev(y)-y==x 其中rev(x)表示x按位翻转之后得到的数. x<=1e9 做法 首先通过打表发现,这个答案不会很大. 这就说明解相当地松弛. ...

  3. 《DSP using MATLAB》Problem 7.29

    代码: %% ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ %% Output In ...

  4. videojs使用的常见问题

    1.报错The play() request was interrupted by a new load request 我在动态更换video的url时会报这个错.修改一下原来的代码如下,就正常了 ...

  5. KOA 学习(六)superAgent

    原文地址 http://www.2cto.com/kf/201611/569080.html 基本请求 初始化一个请求可以通过调用request模块中适当的方法,然后使用.end()来发送请求,例如一 ...

  6. consul理解

    假设consul软件安装在电脑ComputerA上,那么需要注册的服务ServiceA1也需要安装在电脑ComputerA上, 一个服务就是一个提供了ip+port(或者域名)的应用程序. 服务: 服 ...

  7. 通过gevent实现单线程下的多socket并发

    #通过gevent实现单线程下的多socket并发 服务器 #server side import sys import socket import time import gevent from g ...

  8. Faster RCNN 的细节补充

    一.faster rcnn的结构 通过上面的结构,我们知道该faster rcnn前面以VGG16为框架,加入RPN层,最后做分类层. 采用VGG16相对ZF来说慢一点,但是精度也高一点. 二.RPN ...

  9. SSM-6nginx Linux下的安装

    1.下载nginx: 官方网站: http://nginx.org/download/ 2. 要求的安装环境 yum install gcc-c++ yum -y install pcre-devel ...

  10. html文档加载顺序简单理解

    html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF- ...