ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot中的任务处理

2022-07-12 10:35:09  阅读:166  来源: 互联网

标签:异步 springboot 处理 spring org springframework 任务 import mail


springboot中的任务处理

一.异步任务

在开发中有时用户提交的数据,后台需要一定时间才能做出响应,此时用户在前台也不能在等待中,此时就应该先开启异步请求处理,利用多线程,先给前台反馈,后台另一线程去处理数据。

1.创建异步处理请求

package com.springboot.assigment.service;

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

/**
 * @author panglili
 * @create 2022-07-12-8:19
 */

//异步请求注解,表示此类开启异步请求
@Async
@Service
public class AsyncService {
    public void Asycn(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("数据处理中……");
    }
}

此程序中,后台需要三秒等待才能处理好请求。

2.controller调用异步业务请求的方法

package com.springboot.assigment.controller;

import com.springboot.assigment.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author panglili
 * @create 2022-07-12-8:23
 */
@Controller
public class AsycnController {

    @Autowired
    AsyncService asyncService;

    @RequestMapping("/asycn")
    @ResponseBody
    public String asycn(){
        asyncService.Asycn();
        return "ok";
    }
}

在执行完对异步业务的调用之后才会返回给前台一个ok!

3.主程序开启异步处理,创建多线程池!

@EnableAsync 
//开启异步处理,底部开启了一个线程池存放异步请求的处理

总结:实现异步处理只需要加上两个注解,在异步请求服务上加上@Async在主程序上加上@EnableAsync 即可!

二.邮件任务

1.导包

<!--邮件任务-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

2.配置文件properties

spring.mail.username=2558008051@qq.com
spring.mail.password=lswpwkcyalcsdhjc


#开启加密验证
spring.mail.properties.mail.smtp.ssl.enable=true

spring.mail.host=smtp.qq.com

spring.mail.default-encoding=UTF-8
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=false
spring.mail.properties.mail.smtp.starttls.required=false

3.邮件发送方法

class SpringbootApplicationTests {

    @Autowired
    JavaMailSender mailSender;
    @Test
    void contextLoads() {

        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("你好");
        message.setText("这是发送内容~");
        message.setTo("2558008051@qq.com");
        message.setFrom("2558008051@qq.com");
        mailSender.send(message);

    }

}

简单的邮件发送功能完成!

三.定时执行任务

1.写一个需要定时执行的任务

package com.springboot.assigment.service;

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

/**
 * @author panglili
 * @create 2022-07-12-10:00
 */
@Service
public class ScheduledService {


    //cron表达式定义时间
    //注解定时任务的时间
    @Scheduled(cron="0 15 10 * * ?")
    public void hello(){
        System.out.println("hello,你被执行了~");
    }
}

2.主程序开启定时调用

@EnableScheduling//开启定时功能支持

只需要两个简单的注解

@Scheduled://注解定时任务的时间

@EnableScheduling://开启定时功能支持

ok!

标签:异步,springboot,处理,spring,org,springframework,任务,import,mail
来源: https://www.cnblogs.com/lumanmanqixiuyuanxi/p/16469086.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有