码迷,mamicode.com
首页 > 编程语言 > 详细

2020-03-1811:29:37springboot与任务

时间:2020-03-18 13:39:23      阅读:62      评论:0      收藏:0      [点我收藏+]

标签:engine   word   top   ping   tac   conf   rup   ons   project   

异步任务:

@EnableAysnc、@Aysnc

定时任务:

@EnableScheduling、@Scheduled

邮件任务:

pom:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.study.springboot-job</groupId>
    <artifactId>study-job</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>study-job</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

yml

server:
  port: 9999
spring:
  mail:
    username: XXX@qq.com#自己的邮箱
    password: aaaedijj#自己在qq邮箱中设置里开启smtp发送短息得到的内容
    host: smtp.qq.com
    properties.mail.smtp.ssl.enable: true#发附件时需要将此项配置

主启动类:

package com.study.springboot.job;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableAsync //开启异步任务
@EnableScheduling//开启定时任务
@SpringBootApplication
public class StudyJobApplication {

    public static void main(String[] args) {
        SpringApplication.run(StudyJobApplication.class, args);
    }

}

核心代码:

controller

package com.study.springboot.job.controller;


import com.study.springboot.job.service.SyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class AsyncController {

    @Autowired
    private SyncService syncService;

    @GetMapping("/async/test")
    @ResponseBody
    public String getMsg(){
        syncService.syncJob();
        return "async test ...";
    }
}

service

package com.study.springboot.job.service;


import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

@Service
public class SyncService {

    //异步任务
    @Async
    public void syncJob(){
        System.out.println("异步任务开始执行。");
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("异步任务执行完毕。");
    }

    /*字段 允许值                  允许的特殊字符
    秒   0-59                    , - * /
    分   0-59                    , - * /
    小时 0-23                     , - * /
    日期 1-31                     , - * ? / L W C
    月份 1-12                     , - * /
    星期 0-7或SUN-SAT 0,7是SUN  , - * ? / L C #

    特殊字符 代表含义
    ,       枚举
    -       区间
    *       任意
    /       步长
    ?       日/星期冲突匹配
    L       最后
    W       工作日
    C       和calendar联系后计算过的值
    #       星期,4#2,第2个星期四*/

    //定时任务
    @Scheduled(cron = "0 * * * * 0-7")
    public void schedulJob(){
        System.out.println("定时任务开始执行。"+new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(new Date()));
    }
}

测试邮件发送:

package com.study.springboot.job;

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.internet.MimeMessage;
import java.io.File;

@SpringBootTest
class StudyJobApplicationTests {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    private String fromMailAddress = "xxxx284@qq.com";//自己的qq邮箱
    private String toMailAddress = "xxxxx77@163.com";//自己的163邮箱

    @Test
    void contextLoads() {
    }

    @Test
    public void testMail(){

        SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
        simpleMailMessage.setSubject("邮件服务测试");
        simpleMailMessage.setText("测试发送邮件到163邮箱");
        simpleMailMessage.setFrom(fromMailAddress);
        simpleMailMessage.setTo(toMailAddress);

        javaMailSender.send(simpleMailMessage);
    }

    @Test
    public void testFuZaMail() throws Exception{

        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();

        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage,true);

        mimeMessageHelper.setSubject("带附件的邮件测试");
        mimeMessageHelper.setText("<b style=‘color:green‘>正文部分</b>",true);

        mimeMessageHelper.addAttachment("1.jpg",new File("C:\\Users\\25017\\Desktop\\springcloud\\技术介绍\\热部署.png"));//发送文件的本地地址
        mimeMessageHelper.addAttachment("2.jpg",new File("C:\\Users\\25017\\Desktop\\springcloud\\技术介绍\\热部署2.png"));

        mimeMessageHelper.setFrom(fromMailAddress);
        mimeMessageHelper.setTo(toMailAddress);

        javaMailSender.send(mimeMailMessage);
    }
}

 

2020-03-1811:29:37springboot与任务

标签:engine   word   top   ping   tac   conf   rup   ons   project   

原文地址:https://www.cnblogs.com/lxw-all/p/12516727.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!