|
|
@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
package com.example.api.task;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import com.example.api.model.entity.Admin; // 导入Admin实体类,代表管理员信息
|
|
|
|
|
|
|
|
import com.example.api.repository.AdminRepository; // 导入AdminRepository接口,用于访问管理员数据
|
|
|
|
|
|
|
|
import org.slf4j.Logger; // 导入Logger接口,用于日志记录
|
|
|
|
|
|
|
|
import org.slf4j.LoggerFactory; // 导入LoggerFactory类,用于创建Logger实例
|
|
|
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; // 导入Autowired注解,用于自动注入Bean
|
|
|
|
|
|
|
|
import org.springframework.boot.ApplicationArguments; // 导入ApplicationArguments类,用于获取启动参数
|
|
|
|
|
|
|
|
import org.springframework.boot.ApplicationRunner; // 导入ApplicationRunner接口,用于在Spring Boot启动时执行代码
|
|
|
|
|
|
|
|
import org.springframework.core.annotation.Order; // 导入Order注解,用于指定组件的执行顺序
|
|
|
|
|
|
|
|
import org.springframework.stereotype.Component; // 导入Component注解,标识组件
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.text.SimpleDateFormat; // 导入SimpleDateFormat类,用于日期格式化
|
|
|
|
|
|
|
|
import java.util.Date; // 导入Date类,用于处理日期
|
|
|
|
|
|
|
|
import java.util.concurrent.Executors; // 导入Executors工具类,用于创建线程池
|
|
|
|
|
|
|
|
import java.util.concurrent.ScheduledExecutorService; // 导入ScheduledExecutorService接口,用于延迟或定时执行任务
|
|
|
|
|
|
|
|
import java.util.concurrent.TimeUnit; // 导入TimeUnit枚举,用于时间单位转换
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 项目启动时执行的任务。
|
|
|
|
|
|
|
|
* 该类实现了ApplicationRunner接口,在Spring Boot启动时会自动执行其中的run方法。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
@Component
|
|
|
|
|
|
|
|
@Order(1)
|
|
|
|
|
|
|
|
public class ConsumeMqTask implements ApplicationRunner {
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
|
|
|
private AdminRepository adminRepository; // 自动注入AdminRepository
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
|
|
* 日志对象。
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConsumeMqTask.class);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
|
|
|
public void run(ApplicationArguments args) throws Exception {
|
|
|
|
|
|
|
|
// 记录启动时的日志信息
|
|
|
|
|
|
|
|
LOGGER.info("start to run ConsumeMqTask.");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 以下是示例代码,用于在启动时创建一个超级管理员账号
|
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
Admin admin = new Admin();
|
|
|
|
|
|
|
|
admin.setRoles("ROLE_SUPER_ADMIN");
|
|
|
|
|
|
|
|
admin.setEmail("admin@qq.com");
|
|
|
|
|
|
|
|
admin.setPassword("123456");
|
|
|
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
|
|
String time = sdf.format(date);
|
|
|
|
|
|
|
|
admin.setCreateAt(time);
|
|
|
|
|
|
|
|
adminRepository.save(admin);
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 记录结束时的日志信息
|
|
|
|
|
|
|
|
LOGGER.info("end to run ConsumeMqTask.");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|