springboot定时发送短信_SpringBoot---任务管理(包括异步、定时、邮件发送)

本文介绍了如何在SpringBoot项目中实现异步任务、定时任务和邮件发送。通过添加相关依赖,配置邮件服务,并编写Service和Controller,展示了使用@Async注解实现异步任务,以及使用@Scheduled注解设置定时任务的fixedRate、fixedDelay和cron表达式。同时,详细阐述了如何发送简单邮件和模板邮件,提供了完整的代码示例。
摘要由CSDN通过智能技术生成

前期准备

1、创建一个spring-web工程,在pom.xml中添加依赖

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-quartz

org.springframework.boot

spring-boot-starter-mail

net.bytebuddy

byte-buddy

2、项目结构

3、启动类中添加注解

异步任务调度

1、编写异步任务调度方法,包括有返回值和无返回值的方法(注意,类前面一定要记得写Service),本来是实现类的,这里为了简便,直接写了。

@Servicepublic class MyAsyncServiceImpl{

@Asyncpublic void sendSMS() throwsException{

System.out.println("调用短信验证码业务方法。。。");

Long startTime=System.currentTimeMillis();

Thread.sleep(5000);

Long endTime=System.currentTimeMillis();

System.out.println("短信业务执行完成,耗时:"+(endTime-startTime));}

@Asyncpublic Future processA() throwsException {

System.out.println("开始分析统计业务A的数据。。。");

Long startTime=System.currentTimeMillis();

Thread.sleep(4000);int count=12345;

Long endTime=System.currentTimeMillis();

System.out.println("业务A数据统计耗时:"+(endTime-startTime));return new AsyncResult<>(count);}

@Asyncpublic Future processB() throwsException {

System.out.println("开始分析统计业务B的数据。。。");

Long startTime=System.currentTimeMillis();

Thread.sleep(5000);int count=165432;

Long endTime=System.currentTimeMillis();

System.out.println("业务B数据统计耗时:"+(endTime-startTime));return new AsyncResult<>(count);}

}

2、编写MyAsncController控制类

@RestControllerpublic classMyAsyncController {

@Autowired

MyAsyncService myAsyncService;

@GetMapping("/sendSms")public String sendSMS()throwsException{

Long startTime=System.currentTimeMillis(); //开始时间

myAsyncService.sendSMS();

Long endTime=System.currentTimeMillis(); //结束时间

System.out.println("主流程耗时:"+(endTime-startTime));return "success"; }

@GetMapping("/statistics")public String statistics() throwsException{

Long startTime=System.currentTimeMillis(); //开始时间

Future futureA=myAsyncService.processA();

Future futureB=myAsyncService.processB();int total=futureA.get()+futureB.get();

System.out.println("异步任务数据统计汇总结果:"+total);

Long endTime=System.currentTimeMillis(); //结束时间

System.out.println("主流程耗时:"+(endTime-startTime));return "success";}

}

3、运行结果

分析结果:

1、调用的短信业务无回调,主流程无需等待短信业务完成,所以主流程耗时(15秒)与短信业务耗时无关。

2、数据统计方法有回调,主流程需要等待返回值才会往下执行,还有业务A和业务B的耗时是4秒、5秒,由于他们是异步执行的(sleep),所以总耗时也就5秒左右

定时任务调度

测试fixedRate,

fixedDelay, cron等三种方式的定时任务调度

1、编写服务类,添加fixedRate的服务方法

@Servicepublic class ScheduledTaskServiceImpl {static final DateTimeFormatter dtf=DateTimeFormatter.ofPattern("yyyy-MM-dd hh:mm:ss");int count1=1;int count2=1;int count3=1;

@Scheduled(fixedRate= 5000) //每5秒执行一次 ,含执行时间public voidscheduledFixRate() {

System.out.println(String.format("fixeRate第 %s 次执行,当前时间为 %s",count1++,dtf.format(LocalDateTime.now())));}

}

启动工程,查看控制台输出

2、添加fixedDelay的服务方法,同时将fixedRate的方法进行注释

@Scheduled(fixedDelay = 6000) //间隔6秒执行一次,不含执行时间public void scheduleTaskImmediately() throwsException{

System.out.println(String.format("fixeDelay第 %s 次执行,当前时间为 %s",count2++,dtf.format(LocalDateTime.now())));

Thread.sleep(4000);

}

启动工程,查看控制台输出

3、添加基于cron的任务调度方法,同时将fixedRate、fixedDelay的方法进行注释

@Scheduled(cron="0 * * * * *") //每分钟定时执行public voidscheduledTaskCorn(){

System.out.println(

String.format("cron第%s 次执行,当前时间为 %s",count3++,dtf.format(LocalDateTime.now())) );

}

启动工程,查看控制台输出

邮件任务

1、注册一个163或者QQ或者其他的电子邮箱账号,并开通客户端的smtp服务,同时获得注册码。

2、编写application配置文件

#邮箱服务器

spring.mail.host=smtp.163.com#登录邮箱的账号(替换成你的邮箱账号)

spring.mail.username=*****@163.com#这里填的是你刚才客户端授权的密码,而不是你登录邮箱的密码(替换成你的授权码)

spring.mail.password=**************#协议

spring.mail.protocol=smtps#邮箱服务器端口

spring.mail.port=465

#编码设置

spring.mail.default-encoding=UTF-8spring.mail.properties.mail.smtp.auth=true

spring.mail.properties.mail.smtp.starttls.enable=true

spring.mail.properties.mail.smtp.starttls.required=true#设置超时时间

spring.mail.properties.mail.smtp.connectiontimeout=5000spring.mail.properties.mail.smtp.timeout=3000spring.mail.properties.mail.smtp.writetimeout=5000

3、编写邮件发送服务类SendEmailService

@Servicepublic class SendEmailServiceImpl{

@Autowired

JavaMailSenderImpl javaMailSender;

@Value("${spring.mail.username}")privateString sender;

@Asyncpublic voidsendEmail(String receiver, String subject, String content) {

SimpleMailMessage message=newSimpleMailMessage();

message.setFrom(sender);

message.setTo(receiver);

message.setSubject(subject);

message.setText(content);try{

javaMailSender.send(message);

}catch(MailException e){

System.out.println("邮件发送失败");

e.printStackTrace(); } }

}

4、编写控制器EmailController

@RestControllerpublic classEmailController {

@Autowired

SendEmailService sendEmailService;

@Autowired

TemplateEngine templateEngine;

@GetMapping("/sendSimpleEmail")publicString sendSimpleEmail(){

String receiver="****@qq.com";

String subject="springboot 邮件发送测试";

String content="一封来自SpringBoot发送的163的邮件,异步发送模式";

sendEmailService.sendEmail(receiver,subject,content);return "邮件发送成功,请检查邮箱";}

}

5、打开浏览器,访问控制器地址进行测试,并记录测试结果,同时观察控制台输出。检查目标邮箱,看是否接收到邮件。

模板的邮件发送

1、在SendEmailService中添加下面代码

@Asyncpublic voidsendTemplateEmail(String receiver,String subject,String content){

MimeMessage message=javaMailSender.createMimeMessage();try{

MimeMessageHelper helper=new MimeMessageHelper(message,true);

helper.setFrom(sender);

helper.setTo(receiver);

helper.setSubject(subject);

helper.setText(content,true);

javaMailSender.send(message);

System.out.println("邮件发送成功");

}catch(Exception e){

System.out.println("邮件发送失败");

e.printStackTrace();

}

}

2、emailTemplate.html文件

用户校验码
***女士/先生,您好

你的用户验证码为:

请妥善保管,不要泄露给其他人.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值