PowerJob的GridFsManager工作流程源码解读

 更新时间:2024年01月23日 09:10:47   作者:codecraft  
这篇文章主要为大家介绍了PowerJob的GridFsManager工作流程源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下PowerJob的GridFsManager

GridFsManager

tech/powerjob/server/persistence/mongodb/GridFsManager.java

@Slf4j
@Service
public class GridFsManager implements InitializingBean {
    private final Environment environment;
    private final MongoDatabase db;
    private boolean available;
    private final Map<String, GridFSBucket> bucketCache = Maps.newConcurrentMap();
    public static final String LOG_BUCKET = "log";
    public static final String CONTAINER_BUCKET = "container";
    public GridFsManager(Environment environment, @Autowired(required = false) MongoTemplate mongoTemplate) {
        this.environment = environment;
        if (mongoTemplate != null) {
            this.db = mongoTemplate.getDb();
        } else {
            this.db = null;
        }
    }
    /**
     * 是否可用
     * @return true:可用;false:不可用
     */
    public boolean available() {
        return available;
    }
    //......
    private GridFSBucket getBucket(String bucketName) {
        return bucketCache.computeIfAbsent(bucketName, ignore -> GridFSBuckets.create(db, bucketName));
    }
    @Override
    public void afterPropertiesSet() throws Exception {
        String enable = environment.getProperty(PowerJobServerConfigKey.MONGODB_ENABLE, Boolean.FALSE.toString());
        available = Boolean.TRUE.toString().equals(enable) && db != null;
        log.info("[GridFsManager] available: {}, db: {}", available, db);
    }
}
GridFsManager实现了InitializingBean接口,其afterPropertiesSet从environment读取oms.mongodb.enable配置,默认为false;其构造器注入mongoTemplate,若为null则available为false;其getBucket方法则根据bucketName缓存到bucketCache,若不存在则通过GridFSBuckets.create创建

store

/**
     * 存储文件到 GridFS
     * @param localFile 本地文件
     * @param bucketName 桶名称
     * @param fileName GirdFS中的文件名称
     * @throws IOException 异常
     */
    public void store(File localFile, String bucketName, String fileName) throws IOException {
        if (available()) {
            GridFSBucket bucket = getBucket(bucketName);
            try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream(localFile))) {
                bucket.uploadFromStream(fileName, bis);
            }
        }
    }
store方法先获取bucket,再读取localFile,通过bucket.uploadFromStream上传

download

/**
     * 从 GridFS 下载文件
     * @param targetFile 下载的目标文件(本地文件)
     * @param bucketName 桶名称
     * @param fileName GirdFS中的文件名称
     * @throws IOException 异常
     */
    public void download(File targetFile, String bucketName, String fileName) throws IOException {
        if (available()) {
            GridFSBucket bucket = getBucket(bucketName);
            try (GridFSDownloadStream gis = bucket.openDownloadStream(fileName);
                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(targetFile))
            ) {
                byte[] buffer = new byte[1024];
                int bytes = 0;
                while ((bytes = gis.read(buffer)) != -1) {
                    bos.write(buffer, 0, bytes);
                }
                bos.flush();
            }
        }
    }
download方法则先获取bucket,再通过bucket.openDownloadStream获取GridFSDownloadStream,最后写到targetFile

deleteBefore

/**
     * 删除几天前的文件
     * @param bucketName 桶名称
     * @param day 日期偏移量,单位 天
     */
    public void deleteBefore(String bucketName, int day) {
        Stopwatch sw = Stopwatch.createStarted();
        Date date = DateUtils.addDays(new Date(), -day);
        GridFSBucket bucket = getBucket(bucketName);
        Bson filter = Filters.lt("uploadDate", date);
        // 循环删除性能很差?我猜你肯定没看过官方实现[狗头]:org.springframework.data.mongodb.gridfs.GridFsTemplate.delete
        bucket.find(filter).forEach((Consumer<GridFSFile>) gridFSFile -> {
            ObjectId objectId = gridFSFile.getObjectId();
            try {
                bucket.delete(objectId);
                log.info("[GridFsManager] deleted {}#{}", bucketName, objectId);
            }catch (Exception e) {
                log.error("[GridFsManager] deleted {}#{} failed.", bucketName, objectId, e);
            }
        });
        log.info("[GridFsManager] clean bucket({}) successfully, delete all files before {}, using {}.", bucketName, date, sw.stop());
    }
deleteBefore主要通过bucket.find(Filters.lt("uploadDate", date))找到GridFSFile,再挨个执行delete

exists

public boolean exists(String bucketName, String fileName) {
        GridFSBucket bucket = getBucket(bucketName);
        GridFSFindIterable files = bucket.find(Filters.eq("filename", fileName));
        try {
            GridFSFile first = files.first();
            return first != null;
        }catch (Exception ignore) {
        }
        return false;
    }
exists方法则通过bucket.find(Filters.eq("filename", fileName))来进行查找

sync

tech/powerjob/server/core/instance/InstanceLogService.java

@Async(PJThreadPool.BACKGROUND_POOL)
    public void sync(Long instanceId) {
        Stopwatch sw = Stopwatch.createStarted();
        try {
            // 先持久化到本地文件
            File stableLogFile = genStableLogFile(instanceId);
            // 将文件推送到 MongoDB
            if (gridFsManager.available()) {
                try {
                    gridFsManager.store(stableLogFile, GridFsManager.LOG_BUCKET, genMongoFileName(instanceId));
                    log.info("[InstanceLog-{}] push local instanceLogs to mongoDB succeed, using: {}.", instanceId, sw.stop());
                }catch (Exception e) {
                    log.warn("[InstanceLog-{}] push local instanceLogs to mongoDB failed.", instanceId, e);
                }
            }
        }catch (Exception e) {
            log.warn("[InstanceLog-{}] sync local instanceLogs failed.", instanceId, e);
        }
        // 删除本地数据库数据
        try {
            instanceId2LastReportTime.remove(instanceId);
            CommonUtils.executeWithRetry0(() -> localInstanceLogRepository.deleteByInstanceId(instanceId));
            log.info("[InstanceLog-{}] delete local instanceLog successfully.", instanceId);
        }catch (Exception e) {
            log.warn("[InstanceLog-{}] delete local instanceLog failed.", instanceId, e);
        }
    }
InstanceLogService的sync方法先持久化到本地文件,再将文件推送到 MongoDB

小结

GridFsManager实现了InitializingBean接口,其afterPropertiesSet从environment读取oms.mongodb.enable配置,默认为false;其构造器注入mongoTemplate,若为null则available为false;其store和download方法都先判断是否available,为false则空操作。

以上就是PowerJob的GridFsManager工作流程源码解读的详细内容,更多关于PowerJob GridFsManager的资料请关注脚本之家其它相关文章!

相关文章

  • Java中使用instanceof判断对象类型的示例

    Java中使用instanceof判断对象类型的示例

    在List<Object>中遍历Object时,先判断类型,再定向转换,本文给大家介绍Java中使用instanceof判断对象类型,感兴趣的朋友跟随小编一起看看吧
    2023-08-08
  • Spring注解中@Autowired和@Bean的区别详解

    Spring注解中@Autowired和@Bean的区别详解

    这篇文章主要详细介绍了Spring注解中@Autowired和@Bean二者有什么区别,文中通过两个注解的使用场景介绍了二者的区别,感兴趣的同学可以参考阅读
    2023-06-06
  • java设计模式之组合模式(Composite)

    java设计模式之组合模式(Composite)

    这篇文章主要为大家详细介绍了java设计模式之组合模式Composite,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-01-01
  • 利用Java中Calendar计算两个日期之间的天数和周数

    利用Java中Calendar计算两个日期之间的天数和周数

    Java 语言的Calendar(日历),Date(日期),和DateFormat(日期格式)组成了Java标准的一个基本但是非常重要的部分。日期是商业逻辑计算一个关键的部分。下面这篇文章就给大家介绍了如何利用Java中Calendar计算两个日期之间的天数和周数,下面来一起看看吧。
    2016-12-12
  • JavaMail与Spring整合过程解析

    JavaMail与Spring整合过程解析

    这篇文章主要介绍了JavaMail与Spring整合过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2019-11-11
  • 详解Java深拷贝,浅拷贝和Cloneable接口

    详解Java深拷贝,浅拷贝和Cloneable接口

    这篇文章主要为大家详细介绍了Java中Cloneable接口以及深拷贝与浅拷贝的相关知识,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下,希望能够给你带来帮助
    2022-08-08
  • SpringBoot生成License的实现示例

    SpringBoot生成License的实现示例

    License指的是版权许可证,那么对于SpringBoot项目,如何增加License呢?本文就来介绍一下,感兴趣的可以了解一下
    2021-06-06
  • LocalDateTime日期时间格式中间多了一个T的问题及解决

    LocalDateTime日期时间格式中间多了一个T的问题及解决

    这篇文章主要介绍了LocalDateTime日期时间格式中间多了一个T的问题及解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-03-03
  • Spring的Model 和 Map的原理源码解析

    Spring的Model 和 Map的原理源码解析

    这篇文章主要介绍了Spring的Model 和 Map的原理解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-06-06
  • Spring Boot Admin 监控指标接入Grafana可视化的实例详解

    Spring Boot Admin 监控指标接入Grafana可视化的实例详解

    Spring Boot Admin2 自带有部分监控图表,如图,有线程、内存Heap和内存Non Heap,这篇文章主要介绍了Spring Boot Admin 监控指标接入Grafana可视化,需要的朋友可以参考下
    2022-11-11

最新评论