github地址: https://github.com/showkawa/springBoot_2017/tree/master/spb-demo/spb-brian-query-service

1. 异步任务

方法名加上注解@Async,在启动类上加上@EnableAsync

    @Async //Async底层使用AOP技术,在运行时单独创建一个线程进行执行
public void brianAsync(){
try {
Thread.sleep(3000);
brianMail.sendEmail();
} catch (InterruptedException e) {
e.printStackTrace();
}
   logger.debug("异步任务");
}

2.定时器任务

方法名加上注解@Scheduled,在启动类上加上@EnableScheduling,最主要的掌握正则表达式的规则

@Scheduled(cron = "0/5 * * * * *")
public void brianScheduling() {
    logger.debug("定时任务");
 }

补充:

@Scheduled所支持的参数:

1.cron:cron表达式,指定任务在特定时间执行;
2.fixedDelay:表示上一次任务执行完成后多久再次执行,参数类型为long,单位ms;
3.fixedDelayString:与fixedDelay含义一样,只是参数类型变为String;
4.fixedRate:表示按一定的频率执行任务,参数类型为long,单位ms;
5.fixedRateString: 与fixedRate的含义一样,只是将参数类型变为String;
6.initialDelay:表示延迟多久再第一次执行任务,参数类型为long,单位ms;
7.initialDelayString:与initialDelay的含义一样,只是将参数类型变为String;
8.zone:时区,默认为当前时区,一般没有用到。 Cron表达式范例: 每隔5秒执行一次:*/5 * * * * ?
每隔1分钟执行一次:0 */1 * * * ?
每天23点执行一次:0 0 23 * * ?
每天凌晨1点执行一次:0 0 1 * * ?
每月1号凌晨1点执行一次:0 0 1 1 * ?
每月最后一天23点执行一次:0 0 23 L * ?
每周星期天凌晨1点实行一次:0 0 1 ? * L
在26分、29分、33分执行一次:0 26,29,33 * * * ?

3.定时器任务并行执行

SpringBoot对任务有比较好的支撑,我们只需要实现SchedulingConfigurer接口,并重写setSchedulerfang方法即可

/**
* 设置线程池大小
*
* newScheduledThreadPool 创建一个定长线程池,支持定时及周期性任务执行
*/
@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
taskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
}
}

测试结果:可以发现不是在同一个线程里面执行

关于Exetutors的使用可以参考这篇博客:java并发编程--Executor框架

4.邮件任务

package com.kawa.mail;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service; import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File; @Service
public class BrianMail { @Autowired
JavaMailSenderImpl javaMailSender; public void sendEmail() {
MimeMessage mimeMessage = javaMailSender.createMimeMessage();
try {
//multipart:true表示开启附件添加
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
//邮件设置
mimeMessageHelper.setSubject("测试邮件");
mimeMessageHelper.setText("<p style=\"background-color:rgb(255,255,0)\">\n" +
"通过 rbg 值设置背景颜色\n" +
"</p>\n" +
"<p style=\"background-color:rgba(255,255,0,0.25)\">\n" +
"通过 rbg 值设置背景颜色\n" +
"</p>\n" +
"<p style=\"background-color:rgba(255,255,0,0.5)\">\n" +
"通过 rbg 值设置背景颜色\n" +
"</p>\n" +
"<p style=\"background-color:rgba(255,255,0,0.75)\">\n" +
"通过 rbg 值设置背景颜色\n" +
"</p>",true);
mimeMessageHelper.setFrom("xxxxxxxxx@qq.com");
mimeMessageHelper.setTo("xxxxxxxxx@qq.com");
mimeMessageHelper.setCc("xxxxxxxxx@qq.com");
//附件
mimeMessageHelper.addAttachment("10086.jpg",new File("C:\\Users\\HYHGHHHH\\Desktop\\backup\\10086.jpg"));
javaMailSender.send(mimeMessage);
System.out.println("邮件发送成功...");
} catch (MessagingException e) {
e.printStackTrace();
} }
}

springboot(十一)SpringBoot任务的更多相关文章

  1. 【SpringBoot】SpringBoot 入门示例

    参考资料: http://www.tuicool.com/articles/mqeee2A http://www.cnblogs.com/suncj/p/4065589.html http://spr ...

  2. springBoot系列-->springBoot注解大全

    一.注解(annotations)列表 @SpringBootApplication:包含了@ComponentScan.@Configuration和@EnableAutoConfiguration ...

  3. [SpringBoot] - 了解什么是SpringBoot,使用SpringBoot的配置文件

    首先明白Spring是什么,Spring是Java开发的一个框架,为了方便简化Java开发. 什么是注解(注解式开发)? Spring的常用注解有哪些? 假如用SpringBoot构建一个网站程序,应 ...

  4. Springboot】Springboot整合邮件服务(HTML/附件/模板-QQ、网易)

    介绍 邮件服务是常用的服务之一,作用很多,对外可以给用户发送活动.营销广告等:对内可以发送系统监控报告与告警. 本文将介绍Springboot如何整合邮件服务,并给出不同邮件服务商的整合配置. 如图所 ...

  5. 【SpringBoot】SpringBoot 国际化(七)

    本周介绍SpringBoot项目的国际化是如何处理的,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 国际化原理 1.在Spring中有国际化Lo ...

  6. 【SpringBoot】SpringBoot与SpringMVC自动配置(五)

    本文介绍SpringBoot对Spring MVC自动配置,SpringBoot自动配置原理可以参考:[SpringBoot]SpringBoot配置与单元测试(二) 首先新建一个SpringBoot ...

  7. 【SpringBoot】SpringBoot配置与单元测试(二)

    SpringBoot项目创建参考[SpringBoot]SpringBoot快速入门(一) 本文介绍SpringBoot项目的POM文件.配置与单元测试 POM文件 1.SpringBoot的pom文 ...

  8. SpringBoot(四) -- SpringBoot与Web开发

    一.发开前准备 1.创建一个SpringBoot应用,引入我们需要的模块 2.SpringBoot已经默认将这些场景配置好了,只需要在配置文件中指定少量配置,就能运行起来 3.编写业务代码 二.静态资 ...

  9. 【SpringBoot】SpringBoot Web开发(八)

    本周介绍SpringBoot项目Web开发的项目内容,及常用的CRUD操作,阅读本章前请阅读[SpringBoot]SpringBoot与Thymeleaf模版(六)的相关内容 Web开发 项目搭建 ...

随机推荐

  1. 10-JS的函数学习

    <html> <head> <title>js的函数学习</title> <meta charset="UTF-8"/> ...

  2. java 并发基础,及案例分析

    对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了,然而并发问题是令我们大多数程序员头疼的问题,但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们深入研 ...

  3. 黑黛增发罗林川:如何三年开1000家连锁店?_深度案例_i黑马

    黑黛增发罗林川:如何三年开1000家连锁店?_深度案例_i黑马 黑黛增发

  4. 附加数据库时,提示“Microsoft SQL Server,错误: 5120”, 解决方案

    错误的提示内容为:

  5. SQLAlchemy的group_by和order_by的区别

    1.官网解释: group_by(*criterion) apply one or more GROUP BY criterion to the query and return the newly ...

  6. 实例 tar备份以日期命名

    tar备份以日期命名****************************************************************************************#v ...

  7. [NPM] Create a new project using the npm init <initializer> command

    Historically, the npm init command was solely use to create a new package.json file. However, as of ...

  8. hdu 3549 Flow Problem(最大流模板题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known ...

  9. HTML的简单学习

    <html>与</html>之间的部分用来描述网页. <body>与</body>之间是页面的可见的内容. <h1>与</h1> ...

  10. 手把手教你_怎么找android应用的包名和启动activity

    自己主动化測试中常常遇到这个问题,关于这个题目,方法众多,咱的目的是找个比較简单靠谱的: 方法一: 先进入cmd窗体,adb shell 后: cd /data/data ls 能够看到包名了吧,缺点 ...