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

springboot(十一)SpringBoot任务

时间:2018-10-15 20:48:51      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:ipa   catch   frame   print   tle   try   font   表达   att   

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

1. 异步任务

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

    @Async
    public void brianAsync(){
        try {
            Thread.sleep(3000);
            brianMail.sendEmail();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("异步任务");
    }

2.定时器任务

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

    @Scheduled(cron = "0 * * * * *")
    public void brianScheduling() {
        System.out.println("定时任务");
    }

3.邮件任务

 

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任务

标签:ipa   catch   frame   print   tle   try   font   表达   att   

原文地址:https://www.cnblogs.com/hlkawa/p/9792717.html

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