网站如何在百度做排名,如何给网站添加搜索关键字,中国新闻社领导名单,广州开发区交通投资集团有限公司一、异步任务
1、编写一个类AsyncService 异步处理还是非常常用的#xff0c;比如我们在网站上发送邮件#xff0c;后台会去发送邮件#xff0c;此时前台会造成响应不动#xff0c;直到邮件发送完毕#xff0c;响应才会成功#xff0c;所以我们一般会采用多线程的…一、异步任务
1、编写一个类AsyncService 异步处理还是非常常用的比如我们在网站上发送邮件后台会去发送邮件此时前台会造成响应不动直到邮件发送完毕响应才会成功所以我们一般会采用多线程的方式去处理这些任务。
package com.rk.service;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
Service
public class AsyncService {public void hello(){try {System.out.println(数据处理中~);Thread.sleep(3000);//停止三秒} catch (InterruptedException e) {e.printStackTrace();}}
} 2、编写一个AsyncController类
package com.rk.controller;
import com.rk.service.AsyncService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
RestController
public class AsyncController {AutowiredAsyncService asyncService;GetMapping(/hello)public String hello(){asyncService.hello();return success;}
} 现在启动项目进行测试三秒后才会出现success现在还不是异步
3、开启异步 Async//告诉spring这是一个异步方法public void hello(){try {System.out.println(数据处理中~);Thread.sleep(3000);//停止三秒} catch (InterruptedException e) {e.printStackTrace();}}EnableAsync//开启异步注解功能
SpringBootApplication
public class Springboot09TestApplication {public static void main(String[] args) {SpringApplication.run(Springboot09TestApplication.class, args);}
}二、邮件任务
1、引入依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-mail/artifactId/dependency 2、配置mail
#用户名
spring.mail.username6566243357qq.com
#密码
spring.mail.passwordyblyxhvmnsurbbci
#发送邮件服务器
spring.mail.hostsmtp.qq.com
#开启加密验证 ssl
spring.mail.properties.mail.smtp.ssl.enabletrue 3、测试
简单邮件 AutowiredJavaMailSenderImpl mailSender;Testvoid contextLoads() {SimpleMailMessage mailMessage new SimpleMailMessage();mailMessage.setSubject(你好rk);//邮件标题mailMessage.setText(测试邮件);//邮件内柔mailMessage.setTo(r544603357126.com);//收件人邮箱mailMessage.setFrom(6566243357qq.com);//发件人邮箱mailSender.send(mailMessage);} 复杂邮件 Testvoid contextLoads2() throws MessagingException {//一个复杂的邮件MimeMessage mimeMessage mailSender.createMimeMessage();//组装MimeMessageHelper helper new MimeMessageHelper(mimeMessage, true);//正文helper.setSubject(你好,rk);helper.setText(h1 stylecolor:red测试邮件/h1,true);//附件helper.addAttachment(1.png,new File(D:\\QLDownloadGame\\2\\1.png));helper.addAttachment(rk.docx,new File(E:\\桌面\\rk.docx));// 发/收件人helper.setTo(r1624603357126.com);helper.setFrom(1624603357qq.com);//发送mailSender.send(mimeMessage);}三、定时任务
1、编写一个ScheduledService类
Service
public class ScheduledService {//秒 分 时 日 月 周几//0 * * * * MON-FRI//注意cron表达式的用法 每天20:28 0秒执行该方法Scheduled(cron 0 28 20 * * 0-7)public void hello(){System.out.println(现在是20:28);System.out.println(hello.....);}
} 项目启动后每天20:28:00执行hello方法
2、添加注解
EnableAsync//开启异步注解功能
EnableScheduling//开启定时功能注解
SpringBootApplication
public class Springboot09TestApplication {public static void main(String[] args) {SpringApplication.run(Springboot09TestApplication.class, args);}
} cron表达式练习
/*
【0 0/5 14,18 * * ?】每天14点整和18点整每隔5分钟执行一次
【0 15 10 * 1-6】每个月的周一-周六10:15分执行一次
【0 0 2 * 6L】每个月的最后一个周六凌晨2点执行一次
【0 0 2 LW * 】每个月的最后一个工作日凌晨2点执行一次
【0 0 2-4 * 1#1】每个月的第一个周一凌晨2点到4点期间每个整点都执行一次
*/