chapter13:springboot与任务

Spring Boot与任务视频

1. 异步任务

使用注解 @Async 开启一个异步线程任务, 需要在主启动类上添加注解@EnableAsync开启异步配置;

@Service
public class AsyncService {

    @Async
    public void hello() {
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }

        System.out.println("处理数据中。。。");

    }
}

2. 定时任务

Spring提供了异步任务调度的方式,比如TaskExecutor,TaskScheduler接口。

也可以配合两个注解使用, 分别是@EnableScheduling、@Scheduled

cron表达式生成器
chapter13:springboot与任务_第1张图片
0,7 表示周日; 1-6表示周一至周六;

quartz框架不一样,1表示周日;2表示周一;

@Service
public class ScheduledService {

    /**
     * 秒,分,时,日, 月, 星期, 年
     */
//    @Scheduled(cron = "0 * * * * MON-SAT")
//    @Scheduled(cron = "0,1,2,3,4 * * * * MON-SAT")
//    @Scheduled(cron = "0-4 * * * * MON-SAT")
    @Scheduled(cron = "0/4 * * * * MON-SAT")
    public void scheduled() {
        System.out.println("scheduled...");
    }
}

3. 邮件任务

邮件发送需要引入spring-boot-starter-mail;

<dependency>
    <groupId>org.springframework.bootgroupId>
    <artifactId>spring-boot-starter-mailartifactId>
dependency>

springboot自动配置 MailSenderAutoConfiguration, 里面提供了JavaMailSenderImpl来发送邮件。

MailProperties中封装了mail相关的配置属性,可以在application.properties配置文件中修改配置。

@Configuration
@ConditionalOnClass({ MimeMessage.class, MimeType.class })
@ConditionalOnMissingBean(MailSender.class)
@Conditional(MailSenderCondition.class)
@EnableConfigurationProperties(MailProperties.class)
@Import(JndiSessionConfiguration.class)
public class MailSenderAutoConfiguration {

	private final MailProperties properties;

	private final Session session;

	public MailSenderAutoConfiguration(MailProperties properties,
			ObjectProvider<Session> session) {
		this.properties = properties;
		this.session = session.getIfAvailable();
	}

	@Bean
	public JavaMailSenderImpl mailSender() {
		JavaMailSenderImpl sender = new JavaMailSenderImpl();
		if (this.session != null) {
			sender.setSession(this.session);
		}
		else {
			applyProperties(sender);
		}
		return sender;
	}
    // ....
}    

定义MailProperties内容,配置在application.properties中;密码需要在邮箱官网登录后生成第三方客户端登录的授权码。

[email protected]
spring.mail.password=xxxxxxxxxxx
spring.mail.host=smtp.qq.com
spring.mail.properties.mail.smtp.ssl.enable=true

并不是直接从[email protected]发送到[email protected],中间需要经过各自的邮箱服务器的通信才能发送邮件。
chapter13:springboot与任务_第2张图片

测试邮件发送;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Springboot04TaskApplicationTests {

    @Autowired
    private JavaMailSenderImpl javaMailSender;

    @Test
    public void contextLoads() {
        // 发送简单的邮件
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("通知-今晚7:00开会");
        message.setText("讨论项目进度");
        message.setFrom("[email protected]");
        message.setTo("[email protected]");
        javaMailSender.send(message);
    }

    @Test
    public void test02() throws MessagingException {
        // 创建一个复杂的邮件
        MimeMessage mimeMailMessage = javaMailSender.createMimeMessage();
        MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMailMessage, true);
        mimeMessageHelper.setSubject("通知-早上9:30开会");
        mimeMessageHelper.setText("投产评审及代码走查,请提前准备好投产包");
        // 添加附件
        mimeMessageHelper.addAttachment("pom.xml", new File("pom.xml"));
        mimeMessageHelper.setFrom("[email protected]");
        mimeMessageHelper.setTo("[email protected]");

        javaMailSender.send(mimeMailMessage);
    }
}

你可能感兴趣的:(SpringBoot,spring,boot,java,后端)