博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
springboot 与 任务(异步任务,邮件任务,定时任务)
阅读量:4165 次
发布时间:2019-05-26

本文共 4166 字,大约阅读时间需要 13 分钟。

springboot 与 任务

一、异步任务

@EnableAsync  +  @Async  // 开启异步任务注解功能    @EnableAsync   标识在启动类上面@Async         标识在方法上面,标识异步方法

二、定时任务

定时任务:

  • 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息。
  • Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。
  • 两个注解:@EnableScheduling、 @Scheduled

@EnableScheduling 标识在启动类上面

@Scheduled 标识在方法上面

package cn.mesmile.task.service;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;import java.text.SimpleDateFormat;import java.util.Date;/** * @author zb * @date 2019/12/3 21:05 * @Description: */@Servicepublic class ScheduledService {
/** * cron: * second(秒), minute(分), hour(时), day of month (日), month(月), and day of week(周几) * * 0 * * * * MON-FRI * 周一到周五 每分钟整数秒 * hello world......2019-12-03 09:41:00 * hello world......2019-12-03 09:42:00 * hello world......2019-12-03 09:43:00 * * 1,2,3 * * * * MON-FRI * 1-3 * * * * MON-FRI * hello world......2019-12-03 09:52:01 * hello world......2019-12-03 09:52:02 * hello world......2019-12-03 09:52:03 * * 0/3 * * * * MON-FRI * hello world......2019-12-03 09:54:33 * hello world......2019-12-03 09:54:36 * hello world......2019-12-03 09:54:39 */ @Scheduled(cron = "0/3 * * * * MON-FRI") public void hello() {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); System.out.println("hello world......"+simpleDateFormat.format(new Date())); } /** * 特殊字符 代表含义 * , 枚举 * - 区间 * * 任意 * / 步长 * ? 日/星期冲突匹配L最后 * W 工作日 * C 和calendar联系后计算过的值 * # 星期,4#2,第2个星期三 * * 字段 允许值 允许的特殊字符 * 秒 0-59 ,* / * 分 0-59 ,* / * 小时 0-23 ,* / * 日期 1-31 ,*?/LWC * 月份 1-12 ,* / * 星期 0-7或SUN-SAT0,7是SUN ,-*?/LC# */}

三、邮件任务

导入spring支持的邮件jar包:

org.springframework.boot
spring-boot-starter-mail
package cn.mesmile.task;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.mail.SimpleMailMessage;import org.springframework.mail.javamail.JavaMailSender;import org.springframework.mail.javamail.MimeMessageHelper;import javax.mail.MessagingException;import javax.mail.internet.MimeMessage;import java.io.File;import java.nio.charset.MalformedInputException;@SpringBootTestclass TaskApplicationTests {
@Autowired JavaMailSender javaMailSender; @Test void contextLoads() {
System.out.println("hello world"); } @Test void testSendMail(){
// 发送简单消息 SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); // 接收方邮箱 simpleMailMessage.setTo("s@aliyun.com"); simpleMailMessage.setSubject("这是文章的标题。。。。"); simpleMailMessage.setText("这是文本内容哦.........."); // 发送方邮箱 simpleMailMessage.setFrom("10@qq.com"); javaMailSender.send(simpleMailMessage); } @Test void testSendMails() throws MessagingException {
MimeMessage mimeMessage = javaMailSender.createMimeMessage(); MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true); mimeMessageHelper.setSubject("通知--今晚开会"); mimeMessageHelper.setText("今天 7:30 开会",true); // 接收方邮箱 mimeMessageHelper.setTo("s@aliyun.com"); // 发送方邮箱 mimeMessageHelper.setFrom("1047@qq.com"); // 上传文件 mimeMessageHelper.addAttachment("docker容器.txt",new File("C:\\Users\\SuperZheng\\Desktop\\demo\\docker容器.txt")); javaMailSender.send(mimeMessage); }}

# password 为 授权码# ssl:enable 允许 sslspring:  mail:    password: lntcjmgj    username: 10@qq.com    host: smtp.qq.com    properties:      mail:        smtp:          ssl:            enable: true

转载地址:http://hqxxi.baihongyu.com/

你可能感兴趣的文章
Ubuntu Navicat for MySQL安装以及破解方案
查看>>
HTTPS那些事 用java实现HTTPS工作原理
查看>>
oracle函数trunc的使用
查看>>
MySQL 存储过程或者函数中传参数实现where id in(1,2,3,...)IN条件拼接
查看>>
java反编译
查看>>
Class.forName( )你搞懂了吗?——转
查看>>
jarFile
查看>>
EJB与JAVA BEAN_J2EE的异步消息机制
查看>>
数学等于号是=那三个横杠是什么符
查看>>
HTTP协议详解
查看>>
java多线程中的join方法详解
查看>>
二叉树的最近公共祖先LCA
查看>>
数组中累加和为定值K的最长子数组长度
查看>>
素数对--腾讯2017校招编程
查看>>
JAVA集合--ArrayList实现原理
查看>>
idea添加gradle模块报错The project is already registered
查看>>
在C++中如何实现模板函数的外部调用
查看>>
HTML5学习之——HTML 5 拖放
查看>>
HTML5学习之——HTML 5 Canvas vs. SVG
查看>>
HTML5学习之——HTML 5 应用程序缓存
查看>>