SpringBoot:任务

1、异步任务

  • 创建一个AsyncService.java类
package com.wust.springbootstartertest.service;

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

@Service
public class AsyncService {
    public void asyncTask(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("这里是异步任务");
    }
}
  • 创建一个AsyncController.java
package com.wust.springbootstartertest.controller;

import com.wust.springbootstartertest.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 {
    @Autowired
    private AsyncService service;

    @GetMapping("/async")
    public String async(){
        service.asyncTask();
        return "success";
    }
}
  • 测试访问localhost:8080/async

在访问过程中,会发现无法立即返回"success"字符串,在等待三秒后才会返回。如果我们想立刻见到"success"字符串,可以自己手动创建一个线程,在线程中调用service中的方法。但是手动编写线程是相对比较麻烦的。因此SpringBoot提供了注解版本的异步任务。

  • 在asyncTask方法sh添加@Async注解
package com.wust.springbootstartertest.service;

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

@Service
public class AsyncService {
    @Async
    public void asyncTask(){
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("这里是异步任务");
    }
}

为了使@Async注解生效,需要在启动类上添加@EnableAsync注解

@EnableAsync
@SpringBootApplication
public class SpringBootStarterTestApplication {

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

}
  • 最终测试:最终会发现网页会立即响应"success",而不需要等待。 3秒后控制台会输出“这里是异步任务”的字符串

2、定时任务

  • Spring为我们提供了异步执行任务调度的方式,提供了两个接口:
  1. TaskExecutor接口
  2. TaskScheduler接口
  • 以及两个注解:
  1. @EnableScheduling
  2. @Scheduled
  • cron表达式

  Cron表达式是一个字符串,字符串以5或6个空格隔开,分为6或7个域,每一个域代表一个含义,Cron有如下两种语法格式:

  1.   Seconds Minutes Hours DayofMonth Month DayofWeek Year
  2.  Seconds Minutes Hours DayofMonth Month DayofWeek
  • 各字段的含义
字段允许值允许的特殊字符
秒(Seconds)0~59的整数, - * /    四个字符
分(Minutes 0~59的整数
小时(Hours 0~23的整数
日期(DayofMonth1~31的整数(但是你需要考虑你月的天数),- * ? / L W C     八个字符
月份(Month1~12的整数或者 JAN-DEC, - * /    四个字符
星期(DayofWeek1~7的整数或者 SUN-SAT (1=SUN), - * ? / L C #     八个字符
年(可选,留空)(Year1970~2099, - * /    四个字符
  • 创建一个ScheduledService.java
package com.wust.springbootstartertest.service;

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

@Service
public class ScheduledService {
    //秒   分   时     日   月   周几
    //0    *    *      *    *    0-7 (任何时候)
    @Scheduled(cron = "0/3 * * * * 0-7")//每隔3秒执行一次
    public void task(){
        System.out.println("这是一个定时任务");
    }
}
  • 在启动类上增加@EnableScheduling注解
@EnableScheduling
@EnableAsync
@SpringBootApplication
public class SpringBootStarterTestApplication {

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

}
  • cron相关表达式及其含义
(1)0/2 * * * * ?   表示每2秒 执行任务
(1)0 0/2 * * * ?   表示每2分钟 执行任务
(1)0 0 2 1 * ?   表示在每月的1日的凌晨2点调整任务
(2)0 15 10 ? * MON-FRI   表示周一到周五每天上午10:15执行作业
(3)0 15 10 ? 6L 2002-2006   表示2002-2006年的每个月的最后一个星期五上午10:15执行作
(4)0 0 10,14,16 * * ?   每天上午10点,下午2点,4点
(5)0 0/30 9-17 * * ?   朝九晚五工作时间内每半小时
(6)0 0 12 ? * WED   表示每个星期三中午12点
(7)0 0 12 * * ?   每天中午12点触发
(8)0 15 10 ? * *   每天上午10:15触发
(9)0 15 10 * * ?     每天上午10:15触发
(10)0 15 10 * * ?   每天上午10:15触发
(11)0 15 10 * * ? 2005   2005年的每天上午10:15触发
(12)0 * 14 * * ?     在每天下午2点到下午2:59期间的每1分钟触发
(13)0 0/5 14 * * ?   在每天下午2点到下午2:55期间的每5分钟触发
(14)0 0/5 14,18 * * ?     在每天下午2点到2:55期间和下午6点到6:55期间的每5分钟触发
(15)0 0-5 14 * * ?   在每天下午2点到下午2:05期间的每1分钟触发
(16)0 10,44 14 ? 3 WED   每年三月的星期三的下午2:10和2:44触发
(17)0 15 10 ? * MON-FRI   周一至周五的上午10:15触发
(18)0 15 10 15 * ?   每月15日上午10:15触发
(19)0 15 10 L * ?   每月最后一日的上午10:15触发
(20)0 15 10 ? * 6L   每月的最后一个星期五上午10:15触发
(21)0 15 10 ? * 6L 2002-2005   2002年至2005年的每月的最后一个星期五上午10:15触发
(22)0 15 10 ? * 6#3   每月的第三个星期五上午10:15触发

3、邮件任务

  • 引入相关依赖
<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-mail</artifactId>
</dependency>
  • 相关源码

MailSenderlAutoConfiguration.java


@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({MimeMessage.class, MimeType.class, MailSender.class})
@ConditionalOnMissingBean({MailSender.class})
@Conditional({MailSenderAutoConfiguration.MailSenderCondition.class})
@EnableConfigurationProperties({MailProperties.class})
@Import({MailSenderJndiConfiguration.class, MailSenderPropertiesConfiguration.class})
public class MailSenderAutoConfiguration {
    .........................
}

JavaMailSenderImpl.java



@Configuration(
    proxyBeanMethods = false
)
@ConditionalOnClass({Session.class})
@ConditionalOnProperty(
    prefix = "spring.mail",
    name = {"jndi-name"}
)
@ConditionalOnJndi
class MailSenderJndiConfiguration {
    private final MailProperties properties;

    MailSenderJndiConfiguration(MailProperties properties) {
        this.properties = properties;
    }

    @Bean
    JavaMailSenderImpl mailSender(Session session) {
        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setDefaultEncoding(this.properties.getDefaultEncoding().name());
        sender.setSession(session);
        return sender;
    }

    @Bean
    @ConditionalOnMissingBean
    Session session() {
        String jndiName = this.properties.getJndiName();

        try {
            return (Session)JndiLocatorDelegate.createDefaultResourceRefLocator().lookup(jndiName, Session.class);
        } catch (NamingException var3) {
            throw new IllegalStateException(String.format("Unable to find Session in JNDI location %s", jndiName), var3);
        }
    }
}

根据源码可知发送邮件相关的bean:       JavaMailSenderImpl

MailProperties.java



@ConfigurationProperties(
    prefix = "spring.mail"
)
public class MailProperties {
    private static final Charset DEFAULT_CHARSET;
    private String host;
    private Integer port;
    private String username;
    private String password;
    private String protocol = "smtp";
    private Charset defaultEncoding;
    private Map<String, String> properties;
    private String jndiName;

    public MailProperties() {
        this.defaultEncoding = DEFAULT_CHARSET;
        this.properties = new HashMap();
    }

   ..........................................
    static {
        DEFAULT_CHARSET = StandardCharsets.UTF_8;
    }
}

根据MailProperties.java,可以在配置文件中配置相关属性。

3.1、编写测试

  • 配置文件
spring.mail.username=xxxxxx@qq.com
spring.mail.password=qq授权码
spring.mail.host=smtp.qq.com//qq邮箱地址
# qq需要额外配置ssl,网易邮箱等不需要
spring.mail.properties.mail.smtp.ssl.enable=true
  • 开启POP3/SMTP服务,获得QQ授权码 

 

  • 测试
@Autowired
JavaMailSenderImpl mailSender;

@Test
public void contextLoads() {
   //邮件设置1:一个纯文本的邮件
   SimpleMailMessage message = new SimpleMailMessage();
   message.setSubject("Hello,World");
   message.setText("你好,世界");

   message.setTo("xxxxx@qq.com");//设置邮箱接收方
   message.setFrom("24736743@qq.com");//设置邮箱发送方
   mailSender.send(message);
}

@Test
public void contextLoads2() throws MessagingException {
   //邮件设置2:一个带附件的邮件
   MimeMessage mimeMessage = mailSender.createMimeMessage();
   MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);

   helper.setSubject("Hello,World");
   helper.setText("<b style='color:red'>你好,世界</b>",true);

   //发送附件
   helper.addAttachment("1.jpg",new File("xxx"));//设置附件的本地地址
   helper.addAttachment("2.jpg",new File("xxxx"));

   helper.setTo("xxxxx@qq.com");
   helper.setFrom("xxxxx@qq.com");

   mailSender.send(mimeMessage);
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值