SpringBoot学习笔记-12:第十二章-SpringBoot 与任务和邮件

简介: SpringBoot学习笔记-12:第十二章-SpringBoot 与任务和邮件

第十二章-SpringBoot 与任务和邮件

异步任务

package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
@SpringBootApplication
@EnableAsync // 开启异步任务
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
package com.example.demo.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
@Service
public class AsyncService {
    @Async // 异步任务
    public void task(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("task success");
    }
}
package com.example.demo.controller;
import com.example.demo.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;
    @GetMapping("/task")
    public String task(){
        asyncService.task();
        return "success";
    }
}

定时任务

@EnableScheduling

@Scheduled

Cron 表达式


image.png


特殊字符含义


,  枚举

-  区间

*  任意

/  步长

?  日/星期冲突匹配

L 最后

W 工作日

C 和Calendar联系后计算过的值

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



Cron 示例


# 秒 分 时 日 月 周

0 0/5 14,18 * * ?  # 每天14点和18点整,每隔5分钟执行一次

0 15 10 ? * 1-6   # 每个月的周一至周六,10:15执行一次

0 0 2 ? * 6L       # 每个月的最后一个周六凌晨2点执行一次

0 0 2 LW * ?       # 每隔月的最后一个工作日凌晨2点执行一次

0 0 2-4 ? * 1#1    # 每个月的第一个周一凌晨2点到4点,每个整点执行一次



@EnableScheduling // 开启定时任务
@SpringBootApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}
package com.example.demo.service;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@Service
public class ScheduleService {
    /**
     * second, minute, hour, day of month, month, day of week.
     */
    @Scheduled(cron = "0 * * * * MON-FRI")
    public void task(){
        System.out.println("run task");
    }
}

发送邮件

增加依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

配置账号信息

spring:
  # 163邮箱发送邮件
  mail:
    host: smtp.163.com
    port: 25
    username: xxx@163.com
    password: xxx

发送邮件

package com.example.demo;
package com.example.demo;
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.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
@SpringBootTest
public class MailTest {
    @Autowired
    private JavaMailSenderImpl mailSender;
    // 发送简单邮件
    @Test
    public void testSimpleMail(){
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("邮件主题");
        message.setText("邮件内容");
        message.setFrom("xxx@163.com");
        message.setTo("xxx@qq.com");
        mailSender.send(message);
    }
    // 发送复杂邮件
    @Test
    public void testMail() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        helper.setSubject("邮件主题");
        helper.setText("<span style='color:red;'>邮件内容</span>", true);
        helper.setFrom("xxx@163.com");
        helper.setTo("xxx@qq.com");
        // 上传文件
        helper.addAttachment("timg.jpeg", new File("timg.jpeg"));
        mailSender.send(mimeMessage);
    }
}
相关文章
|
22天前
|
前端开发 JavaScript Java
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
本文介绍了一个结合SpringBoot和Python的实用功能,旨在监控iPhone 15的库存状态并通过邮件提醒用户。系统采用SpringBoot监听苹果官网API,解析JSON数据判断是否有货,并展示最近的库存记录。此外,还能自动触发Selenium+Python脚本实现自动化购买。文中详细介绍了技术栈、接口分析、邮件配置及自动化脚本的设置方法。该项目不仅适用于熟悉后端开发的人员,也适合回顾Layui和Jquery等前端技术。
39 0
【实操】SpringBoot监听Iphone15邮件提醒,Selenium+Python自动化抢购脚本
消息中间件 缓存 监控
29 0
|
1月前
|
Java 数据安全/隐私保护
SpringBoot 自定义初始化任务 Runner
SpringBoot 自定义初始化任务 Runner
10 0
|
2月前
|
SQL Java 调度
实时计算 Flink版产品使用问题之使用Spring Boot启动Flink处理任务时,使用Spring Boot的@Scheduled注解进行定时任务调度,出现内存占用过高,该怎么办
实时计算Flink版作为一种强大的流处理和批处理统一的计算框架,广泛应用于各种需要实时数据处理和分析的场景。实时计算Flink版通常结合SQL接口、DataStream API、以及与上下游数据源和存储系统的丰富连接器,提供了一套全面的解决方案,以应对各种实时计算需求。其低延迟、高吞吐、容错性强的特点,使其成为众多企业和组织实时数据处理首选的技术平台。以下是实时计算Flink版的一些典型使用合集。
|
2月前
|
JavaScript Java 测试技术
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
基于SpringBoot+Vue+uniapp的邮件过滤系统的详细设计和实现(源码+lw+部署文档+讲解等)
|
2月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp的邮件过滤系统附带文章源码部署视频讲解等
25 0
|
2月前
|
JavaScript Java 测试技术
基于springboot+vue.js+uniapp小程序的校园悬赏任务平台附带文章源码部署视频讲解等
基于springboot+vue.js+uniapp小程序的校园悬赏任务平台附带文章源码部署视频讲解等
28 0
|
2月前
|
Java 数据处理 数据库
Spring Boot中的批处理任务实现
Spring Boot中的批处理任务实现
|
2月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能
|
3月前
|
Java API Spring
Spring Boot中如何实现邮件发送功能
Spring Boot中如何实现邮件发送功能