package com.wang.service;import org.springframework.scheduling.annotation.Async;import org.springframework.stereotype.Service;@Servicepublic class AsyncService { //告诉Spring这是异步的方法(多线程) @Async public voID hello() { try { Thread.sleep(3000); } catch (InterruptedException e) { e.printstacktrace(); } System.out.println("数据正在处理......"); }}
@Async
2. 在main方法上开启异步功能package com.wang;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.scheduling.annotation.EnableAsync;@SpringBootApplication//开启异步方法的注解@EnableAsyncpublic class Springboot07TaskApplication { public static voID main(String[] args) { SpringApplication.run(Springboot07TaskApplication.class,args); }}
@EnableAsync
效果: 跳转不会受到异步任务的影响
2. 邮件任务1. 添加依赖<dependency> <groupID>org.springframework.boot</groupID> <artifactID>spring-boot-starter-mail</artifactID></dependency>
2. 配置文件spring: mail: username: 715180879@qq.com password: XXXXXX host: smtp.qq.com #开启加密验证(QQ邮箱要求) mail.smtp.ssl.enable: true propertIEs: mail: smtp: ssl: enable: true
3. 邮件发送在SpringBoot中,邮件发送已经被封装成JavaMailSenderImpl
1. 简单的邮件发送@SpringBoottestclass Springboot07TaskApplicationTests { //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法,自动装配即可使用 @autowired JavaMailSenderImpl mailSender; @Test voID contextLoads() { //一个简单的邮件 (只有文字) SimpleMailMessage simpleMailMessage = new SimpleMailMessage(); //标题 simpleMailMessage.setSubject("Hello My QQ Mail!"); //正文 simpleMailMessage.setText("这里是正文!"); //设置收发地址 simpleMailMessage.setTo("715180879@qq.com"); simpleMailMessage.setFrom("715180879@qq.com"); //发送邮件 mailSender.send(simpleMailMessage); }}
2. 复杂邮件的发送@SpringBoottestclass Springboot07TaskApplicationTests { //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法,自动装配即可使用 @autowired JavaMailSenderImpl mailSender; @Test voID contextLoads2() throws MessagingException { //一个复杂的邮件,可以通过mailSender的createMimeMessage方法创建一个MimeMessage MimeMessage message = mailSender.createMimeMessage(); //组装,利用SpringBoot提供的 MimeMessageHelper,boolean可以设置是否支持多文件 MimeMessageHelper helper = new MimeMessageHelper(message,true); helper.setSubject("Hello My QQ Mail! Again!"); //设置True支持HTML标签 helper.setText("<p style='color:red'>这里是正文!</p>",true); //附件,使用绝对路径 helper.addAttachment("pic.jpg",new file("C:\Users\Wang\Pictures\Saved Pictures\pic.jpg")); //设置收发地址 helper.setTo("715180879@qq.com"); helper.setFrom("715180879@qq.com"); mailSender.send(message); }}
3. 邮件发送功能的封装与测试@SpringBoottestclass Springboot07TaskApplicationTests { //在SpringBoot中JavaMailSenderImpl封装了邮件发送的方法,自动装配即可使用 @autowired JavaMailSenderImpl mailSender; /** * * @param HTML : whether enable use HTML in text * @param multipart : whether enable send multifiles * @param Subject : Title of the mail * @param text : content of the mail * @param filename : name for the attachment files * @param filePath : path for the attachment files,use abstract path * @param to : send the mail to someone * @param from : the mail from someone * @author Wang Sky */ public voID sendMail(Boolean HTML,Boolean multipart,String Subject,String text,String[] filename,String[] filePath,String to,String from) throws MessagingException { //一个复杂的邮件,multipart); helper.setSubject(Subject); //设置True支持HTML标签 helper.setText(text,HTML); //附件,使用绝对路径 for (int i = 0; i < filename.length; i++) { helper.addAttachment(filename[i],new file(filePath[i])); } //设置收发地址 helper.setTo(to); helper.setFrom(from); mailSender.send(message); } @Test voID contextLoads3() throws MessagingException { String subject = "Hello My QQ Mail! Again!"; String text = "<p style='color:red'>这里是正文!</p>"; String filename1 = "pic.jpg"; String filePath1 = "C:\Users\Wang\Pictures\Saved Pictures\pic.jpg"; String[] filename = {filename1}; String[] filePath = {filePath1}; String to = "715180879@qq.com"; String from = "715180879@qq.com"; sendMail(true,true,subject,text,filename,filePath,to,from); }}
3. 定时任务两个核心接口
TaskScheduler 任务调度TaskExecutor 任务执行核心注解
@EnableScheduling 放在main方法上@Scheduled 表示什么时候执行,在定时任务的方法上利用cron表达式设定执行时间
package com.wang.service;import org.springframework.scheduling.annotation.Scheduled;import org.springframework.stereotype.Service;@Servicepublic class ScheduleService { //在一个特定的时间执行这个方法 //cron表达式 sec min hour day month week 每天的10:43:00执行一次 @Scheduled(cron = "0 43 10 * * ?") public voID hello() { System.out.println("hello,你被执行了!"); }}
总结 以上是内存溢出为你收集整理的SpringBoot-任务全部内容,希望文章能够帮你解决SpringBoot-任务所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)