Loading

SpringBoot和任务

1 异步任务

1.1 概述

  • 在Java应用中,绝大多数情况下都是通过同步的方式来实现交互处理的;但是在处理和第三方系统交互的时候,容易造成响应迟缓的情况,之前大部分都是使用多线程来完成此类任务,其实,在Spring3.x之后,就已经内置了@Async来完美解决这个问题。

1.2 使用步骤

  • 使用@EnableAysnc注解开启异步功能。
  • 将@Async注解标注在需要异步的类或方法上。

1.3 应用示例

  • 导入所需要的jar包的Maven坐标:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 使用@EnableAysnc注解开启异步功能:
package com.sunxiaping.springboot;

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

@SpringBootApplication
@EnableAsync //开启异步注解功能
public class SpringbootApplication {

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

}
  • AsyncService.java:
package com.sunxiaping.springboot.service;

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

@Service
public class AsyncService {

    @Async
    public void hello(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("。。。。数据处理中。。。。");
    }
}
  • AsyncController.java:
package com.sunxiaping.springboot.web;

import com.sunxiaping.springboot.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping(value = "/async")
public class AsyncController {

    @Autowired
    private AsyncService asyncService;

    @GetMapping(value = "/hello")
    public String hello() {
        asyncService.hello();
        return "成功";
    }

}

2 定时任务

2.1 概述

  • 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨的时候,分析一次前一天的日志信息。Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor、TaskScheduler接口。

2.2 使用步骤

  • 使用@EnableScheduling注解开启定时任务功能。
  • 将@Scheduled注解标注需要定时的类或方法上。

2.3 应用示例

  • 导入所需要的jar包的Maven坐标:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
  • 使用@EnableScheduling注解开启定时任务功能:
package com.sunxiaping.springboot;

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

@SpringBootApplication
@EnableScheduling
public class SpringbootApplication {

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

}
  • ScheduledService.java:
package com.sunxiaping.springboot.service;

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

@Service
public class ScheduledService {

    @Scheduled(cron = "*/5 * * * * ?")
    public void hello(){
        System.out.println("hello");
    }
}

3 邮件任务

3.1 导入相关jar包的Maven坐标

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

3.2 配置application.yml

spring:
 mail:
    username: 1900919313@qq.com
    password: unwcjvjwuknlbffb #授权码
    host: smtp.qq.com
    properties:
      mail:
        smpt:
          ssl:
            enable: true

3.3 测试

package com.sunxiaping.springboot;

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.JavaMailSender;
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;

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

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 简单邮件
     */
    @Test
    public void test(){

        SimpleMailMessage message = new SimpleMailMessage();
        //设置邮件
        message.setSubject("通知--开会啦");
        message.setText("一会开会啊");
        message.setTo("1975356467@qq.com");
        message.setFrom("1900919313@qq.com");

        mailSender.send(message);

    }

    /**
     * 复杂邮件
     */
    @Test
    public void test2() throws MessagingException {
        MimeMessage mimeMessage = mailSender.createMimeMessage();

        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);

        //设置邮件
        helper.setSubject("通知--开会啦");
        helper.setText("<b style='color:red'>今天开会啦</b>",true);
        helper.setTo("1975356467@qq.com");
        helper.setFrom("1900919313@qq.com");

        helper.addAttachment("1.jpg", new File("C:\\Users\\Administrator\\Pictures\\1.png"));

        mailSender.send(mimeMessage);
    }
    
}
posted @ 2020-09-23 09:12  许大仙  阅读(233)  评论(0编辑  收藏  举报