异步任务,邮箱任务,定时任务
task~
- 异步任务
- 邮箱任务
- 定时任务
源码下载
异步任务
开启多线程,我飞了。
package cn.bitqian.service;import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;/*** 异步任务 目的 多线程,交给spring托管,带来良好的用户体验* @author echo lovely* @date 2020/10/30 8:55*/
@Service
public class AsyncService {// 开启了异步任务 多线程@Asyncpublic void hello() {try {Thread.sleep(3000);} catch (InterruptedException e) {e.printStackTrace();}System.out.println("任务进行中...");}}
启动函数EnableXXX
@EnableAsync // 开启了异步任务
上面的睡眠和打印,会同时进行。
如果不开启,程序会等三秒钟响应。
邮箱任务
邮箱附件,使用qq可以发送邮箱。
导入启动器
<!-- com.sun.mail --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-mail</artifactId></dependency>
这里使用qq邮箱发送,application.properties邮箱的配置
spring.mail.username=999999@qq.com
# 此密码为qq邮箱网页版的授权码,绑定了手机,会发验证码验证
spring.mail.password=abcdefg
spring.mail.host=smtp.qq.com
# 开启加密验证
spring.mail.properties.mail.smtp.ssl.enalbe=true
邮箱发送(发送普通文本,附件)
package cn.bitqian;import org.junit.jupiter.api.Test;
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 javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.FileNotFoundException;@SpringBootTest
class SpringbootTaskApplicationTests {@AutowiredJavaMailSenderImpl mailSender;@Testvoid contextLoads() {// 一封简单的mail~SimpleMailMessage mailMessage = new SimpleMailMessage();mailMessage.setSubject("hello bitqian");mailMessage.setText("send a simple email using springboot.. by qq");mailMessage.setFrom("999999@qq.com");mailMessage.setTo("999999@qq.com");mailSender.send(mailMessage);}@Testvoid test1() throws MessagingException, FileNotFoundException {// 发送带有附件 的邮件MimeMessage mimeMessage = mailSender.createMimeMessage();// 发送多个附件,并设置编码MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);// 发送图片helper.addAttachment("1.png", new File("C:\\Users\\Pictures\\Eibw4t2UMAEoosS.jfif"));// 发送音乐helper.addAttachment("Innocence.mp3", new File("F:\\music\\Avril Lavigne - Innocence.mp3"));helper.setSubject("发送了图片和音乐");helper.setText("<p style='color:red'>springboot 邮箱发送附件测试</p>");helper.setFrom("999999@qq.com");helper.setTo("999999@qq.com");mailSender.send(mimeMessage);}}
定时任务
quartz,这个。在boot里面更简单了。
- 启用定时任务
package cn.bitqian;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;@SpringBootApplication
@EnableScheduling // 开启定时任务
public class SpringbootTaskApplication {public static void main(String[] args) {SpringApplication.run(SpringbootTaskApplication.class, args);}}
- corn表达式
package cn.bitqian.service;import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;/*** 定时任务 springboot 已经集成* @author echo lovely* @date 2020/10/30 11:26*/
@Service
public class ScheduledService {// corn表达式: 秒 时 分 日 月 周几@Scheduled(cron = "0/1 0/1 0/1 * * ?")public void show() {System.out.println("执行123...");}}
spring实现定时任务
异步任务,邮箱任务,定时任务相关推荐
- django celery 异步发送邮箱
django + celery + redis 异步发送QQ邮箱 django一般的代码逻辑都需要一步一步执行,这样大大的占用了I/O资源,用户也得不到完美的体验,django-celery可以灵活的 ...
- android 开启一个定时线程_Android异步、延迟和定时任务的简易用法
异步多线程 延迟触发 循环定时触发 一.前言 项目开发中一定会用到网络请求,文件读写,开启子线程进行耗时操作,延迟返回或关闭提示框,轮询接口获取处理结果,子线程发送内容到主线程更新界面等等.碰到这些问 ...
- Spring定时任务并行(异步)处理
最近项目中遇到一个问题 , 在SpringBoot中设置了定时任务之后 , 在某个点总是没有执行 . 经过搜索研究发现 , spring 定时器任务scheduled-tasks默认配置是单线程串行执 ...
- crontab 每分钟一次_Celery实现定时任务crontab
定时任务的应用场景非常广,基本上所有的开发人员都会接触到定时任务,实现定时任务有很多方法,其中Celery里面的定时任务功能就非常强大,并且使用非常简单,只需要安装celery就行,下面就详细介绍ce ...
- spring-boot 定时任务
2019独角兽企业重金招聘Python工程师标准>>> 1.建立项目 @SpringBootApplication @EnableAsync @EnableScheduling @E ...
- 我的NopCommerce之旅(4): 定时任务之邮件
一.功能简介 用户购买物品生成订单后,系统将发送邮件提醒给用户 二.操作步骤 后台配置一个系统的默认发送邮箱 启动定时任务,这里包括多个任务,只需要启动邮件任务 查看邮件发送情况 三.数据库分析 [d ...
- Celery实现定时任务crontab
Celery实现定时任务crontab 一. 搭建celery定时任务架构 在项目中适合的位置新建一个定时任务目录celery_crontab,在目录下创建config.py,main.py,task ...
- spring定时每天早上八点_SpringBoot 如何实现并发定时任务?
一.在JAVA开发领域,目前可以通过以下几种方式进行定时任务 1.单机部署模式 Timer: jdk中自带的一个定时调度类,可以简单的实现按某一频度进行任务执行. 提供的功能比较单一,无法实现复杂的调 ...
- 定时任务重启后执行策略_定时任务时间过长会不会影响下次的执行? 第331篇...
相关历史文章(阅读本文前,您可能需要先看下之前的系列?) 国内最全的Spring Boot系列之三 2020上半年发文汇总「值得收藏」 立方阶时间复杂度怎么算-征战沙场- 第326篇 SpringBo ...
最新文章
- GeneratorSqlMapCustom(mybatis逆向工程)
- 服务器dbback文件夹,怎么让SQL 2000定时复制备份数据库到局域网中的指定电脑上? - SQL Server论坛 - 51CTO技术论坛_中国领先的IT技术社区...
- 怎么用mysql存储系统数据库_mysql数据库之基本操作和存储引擎
- Hadoop_NameNode_代码分析_目录树(2)
- ecshop php升级,升级-安装与升级- ECShop帮助
- 西贝莜面村员工手册_西贝那达慕草原美食节 引领文化生活新消费
- cpu开机就是60℃_注意,制冷机组开机前这些检查很重要
- 编写操作系统的平台之争:Windows还是*nix
- Luogu4885 灭顶之灾
- python 快速排名发包_SEO怎么快速排名?SEO快排发包技术原理是怎么实现的呀?(悬赏5元) - 搜外问答...
- Mac腾讯会议没声音
- 记录方维p2p项目后台登录自动化测试验证码问题
- 简易学籍管理系统(3)--插入结点并排序
- cad隐藏图层命令快捷键_Auto CAD如何快速隐藏图层,快捷键是什么?
- Hadoop集群搭建(所需软件安装包已上传发布)
- NBA表格_python告诉你关于NBA得分王中一些有趣的数据
- 安装webpack及使用
- 使用apktool解包和打包apk
- 主成分分析;主成分回归分析——Hald水泥问题;主成分分析案例——各地区普通高等教育发展水平综合评价;matlab
- 常用String方法大全