标签:切面.调度.邮件.监控;

一、简介

在上篇《SpringBoot3根底》中已经完结入门事例的开发和测验,在这篇内容中再来看看进阶功用的用法;

主要涉及如下几个功用点:

调度使命:在运用中供给一定的轻量级的调度才能,比方办法按指定的定时规矩履行,或许异步履行,然后完结相应的代码逻辑;

邮件发送:邮件作为消息系统中的途径,是常用的功用;

运用监控:实时或定期监控运用的健康状况,以及各种关键的指标信息;

切面编程:经过预编译方式和运行期动态代理完成程序中部分功用一致保护的技能,能够将业务流程中的部分逻辑解耦处理,提高可复用性;

二、工程搭建

1、工程结构

SpringBoot3进阶用法

2、依靠办理

<!-- 根底框架依靠 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<!-- 运用监控组件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<!-- 切面编程组件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
    <version>${spring-boot.version}</version>
</dependency>
<!-- 邮件发送组件 -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
    <version>${spring-boot.version}</version>
</dependency>

这里再细致的检查一下各个功用的组件依靠系统,SpringBoot只是供给了强大的集成才能;

SpringBoot3进阶用法

3、发动类

注意在发动类中运用注解开启了异步EnableAsync和调度EnableScheduling的才能;

@EnableAsync
@EnableScheduling
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

三、切面编程

1、界说注解

界说一个办法级的注解;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@Documented
public @interface DefAop {
    /**
     * 模块描述
     */
    String modelDesc();
    /**
     * 其他信息
     */
    String otherInfo();
}

2、注解切面

在切面中运用Around环绕通知类型,会阻拦到DefAop注解标记的办法,然后解析获取各种信息,进而嵌入自界说的流程逻辑;

@Component
@Aspect
public class LogicAop {
    private static final Logger logger = LoggerFactory.getLogger(LogicAop.class) ;
    /**
     * 切入点
     */
    @Pointcut("@annotation(com.boot.senior.aop.DefAop)")
    public void defAopPointCut() {
    }
    /**
     * 环绕切入
     */
    @Around("defAopPointCut()")
    public Object around (ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        Object result = null ;
        try{
            // 履行办法
            result = proceedingJoinPoint.proceed();
        } catch (Exception e){
            e.printStackTrace();
        } finally {
            // 处理逻辑
            buildLogicAop(proceedingJoinPoint) ;
        }
        return result ;
    }
    /**
     * 构建处理逻辑
     */
    private void buildLogicAop (ProceedingJoinPoint point){
        // 获取办法
        MethodSignature signature = (MethodSignature) point.getSignature();
        Method reqMethod = signature.getMethod();
        // 获取注解
        DefAop defAop = reqMethod.getAnnotation(DefAop.class);
        String modelDesc = defAop.modelDesc() ;
        String otherInfo = defAop.otherInfo();
        logger.info("DefAop-modelDesc:{}",modelDesc);
        logger.info("DefAop-otherInfo:{}",otherInfo);
    }
}

四、调度使命

1、异步处理

1.1 办法界说

经过Async注解标识两个办法,办法在履行时会休眠10秒,其中一个注解指定异步履行运用asyncPool线程池;

@Service
public class AsyncService {
    private static final Logger log = LoggerFactory.getLogger(AsyncService.class);
    @Async
    public void asyncJob (){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("async-job-01-end...");
    }
    @Async("asyncPool")
    public void asyncJobPool (){
        try {
            TimeUnit.SECONDS.sleep(10);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        log.info("async-job-02-end...");
    }
}

1.2 线程池

界说一个ThreadPoolTaskExecutor线程池对象;

@Configuration
public class PoolConfig {
    @Bean("asyncPool")
    public Executor asyncPool () {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        // 线程池命名前缀
        executor.setThreadNamePrefix("async-pool-");
        // 中心线程数5
        executor.setCorePoolSize(5);
        // 最大线程数10
        executor.setMaxPoolSize(10);
        // 缓冲履行使命的队列50
        executor.setQueueCapacity(50);
        // 线程的空闲时刻60秒
        executor.setKeepAliveSeconds(60);
        // 线程池对回绝使命的处理策略
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        // 线程池关闭的时等候一切使命都完结再持续毁掉其他的Bean
        executor.setWaitForTasksToCompleteOnShutdown(true);
        // 设置线程池中使命的等候时刻
        executor.setAwaitTerminationSeconds(300);
        return executor;
    }
}

1.3 输出信息

从输出的日志信息中能够发现,两个异步办法所运用的线程池不一样,asyncJob选用默许的cTaskExecutor线程池,asyncJobPool办法选用的是async-pool线程池;

[schedule-pool-1] c.boot.senior.schedule.ScheduleService   : async-job-02-end...
[cTaskExecutor-1] c.boot.senior.schedule.ScheduleService   : async-job-01-end...

2、调度使命

2.1 调度装备

经过完成SchedulingConfigurer接口,来修改调度使命的装备,这里重新界说使命履行的线程池;

@Configuration
public class ScheduleConfig implements SchedulingConfigurer {
    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.setScheduler(Executors.newScheduledThreadPool(5));
    }
}

2.2 调度办法

经过Scheduled注解来标记办法,根据定时器的规矩设定,来一致办理办法的履行时刻;

@Component
public class ScheduleJob {
    private static final Logger log = LoggerFactory.getLogger(ScheduleJob.class);
    private static final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss") ;
    /**
     * 上一次开始履行时刻点之后10秒再履行
     */
    @Scheduled(fixedRate = 10000)
    private void timerJob1(){
        log.info("timer-job-1:{}",format.format(new Date()));
    }
    /**
     * 上一次履行结束时刻点之后10秒再履行
     */
    @Scheduled(fixedDelay = 10000)
    private void timerJob2(){
        log.info("timer-job-2:{}",format.format(new Date()));
    }
    /**
     * Cron表达式:每30秒履行一次
     */
    @Scheduled(cron = "0/30 * * * * ?")
    private void timerJob3(){
        log.info("timer-job-3:{}",format.format(new Date()));
    }
}

五、邮件发送

1、邮件装备

选用QQ邮箱来模拟邮件的发送方,需求先开启smtp邮件传输协议,在QQ邮箱的设置/账户途径下,并且获取相应的授权码,在项目的装备中运用;

SpringBoot3进阶用法

spring:
  application:
    name: boot-senior
  # 邮件装备
  mail:
    host: smtp.qq.com
    port: 465
    protocol: smtps
    username: 邮箱账号
    password: 邮箱授权码
    properties:
      mail.smtp.ssl.enable: true

2、办法封装

界说一个简单的邮件发送办法,并且能够增加附件,是常用的功用之一;别的也能够经过Html静态页渲染,再转换为邮件内容的方式;

@Service
public class SendMailService {
    @Value("${spring.mail.username}")
    private String userName ;
    @Resource
    private JavaMailSender sender;
    /**
     * 带附件的邮件发送办法
     * @param toUsers 接收人
     * @param subject 主题
     * @param content 内容
     * @param attachPath 附件地址
     * @return java.lang.String
     * @since 2023-07-10 17:03
     */
    public String sendMail (String[] toUsers,String subject,
                            String content,String attachPath) throws Exception {
        // MIME邮件类
        MimeMessage mimeMessage = sender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
        // 邮件发送方From和接收方To
        helper.setFrom(userName);
        helper.setTo(toUsers);
        // 邮件主题和内容
        helper.setSubject(subject);
        helper.setText(content);
        // 邮件中的附件
        File attachFile = ResourceUtils.getFile(attachPath);
        helper.addAttachment(attachFile.getName(), attachFile);
        // 履行邮件发送指令
        sender.send(mimeMessage);
        return "send...mail...sus" ;
    }
}

测验结果

SpringBoot3进阶用法

六、运用监控

1、监控装备

springbootactuator组件中,能够经过供给的Rest接口,来获取运用的监控信息;

# 运用监控装备
management:
  endpoints:
    web:
      exposure:
        # 打开一切的监控点
        include: "*"
      base-path: /monitor
  endpoint:
    health:
      enabled: true
      show-details: always
    beans:
      enabled: true
    shutdown:
      enabled: true

2、相关接口

2.1 Get类型接口:主机:端口/monitor/health,检查运用的健康信息,三个中心指标:status状况,diskSpace磁盘空间,ping检查;

{
    /* 状况值 */
	"status": "UP",
	"components": {
	    /* 磁盘空间 */
		"diskSpace": {
			"status": "UP",
			"details": {
				"total": 250685575168,
				"free": 112149811200,
				"threshold": 10485760,
				"path": "Path/butte-spring-parent/.",
				"exists": true
			}
		},
		/* Ping检查 */
		"ping": {
			"status": "UP"
		}
	}
}

2.2 Get类型接口:主机:端口/monitor/beans,检查bean列表;

{
	"contexts": {
		"boot-senior": {
			"beans": {
				"asyncPool": {
					"scope": "singleton",
					"type": "org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor",
					"resource": "class path resource [com/boot/senior/schedule/PoolConfig.class]"
				},
				"asyncService": {
					"scope": "singleton",
					"type": "com.boot.senior.schedule.AsyncService$$SpringCGLIB$$0"
				}
			}
		}
	}
}

2.3 Post类型接口:主机:端口/monitor/shutdown,关闭运用程序;

{
    "message": "Shutting down, bye..."
}

七、参考源码

文档库房:
https://gitee.com/cicadasmile/butte-java-note
源码库房:
https://gitee.com/cicadasmile/butte-spring-parent