springboot高级篇(任务)

1,异步任务

1,创建springboot项目,引入web依赖,使用springboot2.0.1版本

2,创建service.AsyncService

package com.atguigu.task.service;

import org.springframework.stereotype.Service;

@Service
public class AsyncService {
    public void hello(){
        try {
            Thread.sleep(3000);
            System.out.println("数据处理中");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

3,创建controller.AsynController

package com.atguigu.task.controller;

import com.atguigu.task.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 Asyncontroller {
    @Autowired
    AsyncService asyncService;
    @GetMapping("/hello")
    public String hello(){
        asyncService.hello();
        return "success";
    }
}

4,访问接口方法会明显延迟,有没有什么解决方法?使用异步方法

5,service 的hello方法上加@Async注解。在启动类上加@EnableAsync注解,这就可以愉快的访问了。

2,定时任务

详情见我另外一篇博客单独写出来的

3,邮件任务

步骤:1,引入spring-boot-starter-mail
2,springboot自动配置MailSenderAutoConfiguration
3,定义Mailproperties内容,application.yml中
4,自动装配JavaMailSender

1,pom

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>

//qq邮箱跟163发送邮件
1,来到qq邮箱,账户,设置,开启pop3,IMAP,EXCHAGE,CardDAV四个服务
2,点击下面生成授权码。将其生成的复制
3,点击SMTp那链接会教你怎么设置smtp(这里不用设置)

2,application.properties

spring.mail.username=你的qq邮箱
spring.mail.password=??????????
spring.mail.host=smtp.qq.com

3,test类

package com.atguigu.task;


import org.junit.Test;
import org.junit.runner.RunWith;
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 org.springframework.test.context.junit4.SpringRunner;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;

@SpringBootTest
@RunWith(SpringRunner.class)
public class Springboot04TaskApplicationTests {
@Autowired
    JavaMailSenderImpl mailSender;
    @Test
   public void contextLoads() {
      //邮件设置
        SimpleMailMessage message=new SimpleMailMessage();
        message.setSubject("天气预报");
        message.setText("今天天晴");
        message.setFrom("xxxxxxxx");
        message.setTo("xxxxxxxx");
        mailSender.send(message);
    }

发送简单邮件

想发送复杂邮件用下面方法、可以发送附件。指定正文为html格式

//发送复杂邮件
    @Test
    public void test02(){
        MimeMessage mimeMessage=mailSender.createMimeMessage();
        //创建builer是否传递文件
        try {
            MimeMessageHelper helper=new MimeMessageHelper(mimeMessage,true);
            helper.setSubject("今天晚上开会");
            helper.setText("<b style='color:red'>8.00开会</b>",true);
            helper.setTo("xxxxxxxxxxxx");
            helper.setFrom("xxxxxxxxxxxx");
            //发送附件
            helper.addAttachment("test", new File("D:\\fedu.sql"));
            mailSender.send(mimeMessage);
        } catch (MessagingException e) {
            e.printStackTrace();
        }
    }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值