springboot--任务

本文详细介绍了在SpringBoot中如何实现异步任务,包括两种实现方式,并探讨了邮件发送的配置与问题,特别是发送带有图片的复杂邮件时遇到的报错信息。同时,还讲解了定时任务的设置方法。
摘要由CSDN通过智能技术生成

                            异步任务,定时任务,邮件发送

一,异步任务

方式一:创建一个class文件,名为AsyncService

package com.sqt.service;

import org.springframework.stereotype.Service;

/**
 * @author mypc
 */
@Service
public class AsyncService {
   
    public void hello()  {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("数据正在处理。。。。。。");
    }

}

创建一个class文件,名为AsyncController

package com.sqt.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author mypc
 */
@RestController
public class AsyncController {
    @Autowired
    AsyncService asyncService;

    @RequestMapping("/hello")
    public String hello(){
        asyncService.hello();//停止3s,转圈
        return "OK";
    }
}

然后在启动类调用

方式二(使用注解):创建一个class文件,名为AsyncService,使用Asyncz注解

package com.sqt.service;

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

/**
 * @author mypc
 */
@Service
public class AsyncService {
    //@Async告诉spring这是一个异步方法
    @Async
    public void hello()  {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("数据正在处理。。。。。。");
    }

}

然后,在启动类开启注解

@EnableAsync
package com.sqt;

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

/**
 * @author mypc
 */
//开启异步注解功能
@EnableAsync
@SpringBootApplication
public class Spring09TestApplication {

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

}

二,邮件任务

(1)导入依赖javax.mail依赖,需要进行配置

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

(2)配置application.properties文件

spring.mail.username=1738771095@qq.com
spring.mail.password=iupgkqczflncedea
spring.mail.host=smtp.qq.com
#??????
spring.mail.smtp.ssl.enable=true

(3)在测试启动类中编写(但是复杂邮件的图片找不到,文件确可以?

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值