springboot2 配置 异步, 定时和邮件

springboot2 配置 异步, 定时和邮件

1. 结构

在这里插入图片描述

Springboot09Application

package com.ykenan.ykenan;

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

/**
 * EnableAsync: Open the asynchronous annotation function.
 * EnableScheduling: Open the scheduling annotation function.
 */
@EnableScheduling
@EnableAsync
@SpringBootApplication
public class Springboot09Application {

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

}

1.1 异步

AsyncService

package com.ykenan.ykenan.service;

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

@Service
public class AsyncService {

    private static final Integer THREAD_SLEEP_TIME = 3000;

    /**
     * Async: This method is an asynchronous process.
     */
    @Async
    public void hello() {
        try {
            /* Sleep 3s */
            Thread.sleep(THREAD_SLEEP_TIME);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("处理数据中...");
    }

}

AsyncController

package com.ykenan.ykenan.controller;

import com.ykenan.ykenan.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 {

    private AsyncService asyncService;

    @Autowired
    public AsyncController(AsyncService asyncService) {
        this.asyncService = asyncService;
    }

    @GetMapping("/hello")
    public String hello() {
        asyncService.hello();
        return "success";
    }

}

1.2 定时

scheduledService

package com.ykenan.ykenan.service;

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

@Service
public class scheduledService {

    /**
     * minute, hour, day of month, month and day of week.
     * 0 * * * * MON-FRI: Running once a minute.
     */
//    @Scheduled(cron = "0 * * * * MON-FRI")
//    @Scheduled(cron = "0,1,2,3,4,5 * * * * MON-FRI")
//    @Scheduled(cron = "0-5 * * * * MON-FRI")
    @Scheduled(cron = "0/3 * * * * 0-7")
    public void hello() {
        System.out.println("Hello...");
    }

}

1.3 邮件

导包

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

application.properties
为保证安全: spring.mail.password 提供的是授权码, 并非是密码.

# spring mail
spring.mail.username=2451603124@qq.com
# Authorization code: Ensuring safety
spring.mail.password=ooskeyleoxyulsge
spring.mail.host=smtp.qq.com
spring.mail.port=465
spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.ssl.enable=true

Springboot09ApplicationTests

package com.ykenan.ykenan;

import org.junit.Test;
import org.junit.runner.RunWith;
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 org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot09ApplicationTests {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Test
    public void contextLoads() {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("Java -- mail");
        message.setText("The first mailbox sent to someone on the Java platform.");
        message.setTo("3236170161@qq.com");
        message.setFrom("3236170161@qq.com");
        javaMailSender.send(message);
    }

    @Test
    public void mail() throws MessagingException {
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
        mimeMessageHelper.setSubject("Java -- mail");
        mimeMessageHelper.setText("The first <strong style='color: yellow'>complex</strong> mailbox sent to someone on the Java platform.", true);
        mimeMessageHelper.setTo("2451603124@qq.com");
        mimeMessageHelper.setFrom("2451603124@qq.com");
        mimeMessageHelper.addAttachment("Diagrams.png", new File("C:/springboot09.png"));
        javaMailSender.send(mimeMessage);
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值