All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.cheny.toolbox.entityCache.spring.EntityBufferAutoScanner Maven / Gradle / Ivy

There is a newer version: 2.3.6-jdk8
Show newest version
package cn.cheny.toolbox.entityCache.spring;

import cn.cheny.toolbox.entityCache.annotation.CacheEntity;
import cn.cheny.toolbox.entityCache.holder.EntityBufferHolder;
import cn.cheny.toolbox.scan.PathScanner;
import cn.cheny.toolbox.scan.filter.ScanFilter;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.InitializingBean;

import java.util.List;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

/**
 * 实体缓存扫描器
 *
 * @author cheney
 * @date 2020-09-02
 */
@Slf4j
public class EntityBufferAutoScanner implements InitializingBean, DisposableBean {

    /**
     * 自动刷新默认间隔时间:60分钟
     */
    private final static Long DEFAULT_PERIOD = 60L;

    /**
     * 实体缓存配置
     */
    private final EntityBufferProperties entityBufferProperties;

    /**
     * 实体缓存持有者
     */
    private final EntityBufferHolder entityBufferHolder;

    /**
     * 定时任务线程池
     */
    private ScheduledExecutorService scheduledExecutor;

    public EntityBufferAutoScanner(EntityBufferProperties entityBufferProperties, EntityBufferHolder entityBufferHolder) {
        this.entityBufferProperties = entityBufferProperties;
        this.entityBufferHolder = entityBufferHolder;
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        String[] entityPaths = entityBufferProperties.getEntityPaths();
        if (entityPaths == null || entityPaths.length == 0) {
            return;
        }
        ScanFilter scanFilter = new ScanFilter();
        scanFilter.addAnnotation(CacheEntity.class);
        for (String entityPath : entityPaths) {
            if (!StringUtils.isEmpty(entityPath)) {
                List> classes = new PathScanner(scanFilter).scanClass(entityPath);
                if (classes.size() > 0) {
                    scheduled(classes);
                }
            }
        }
    }

    /**
     * 执行任务,缓存实体
     *
     * @param classes 缓存实体class集合
     */
    private void scheduled(List> classes) {
        Runnable task = () -> {
            if (log.isDebugEnabled()){
                log.debug("Start executing refresh entity buffer");
            }
            try {
                classes.parallelStream().forEach(entityBufferHolder::refreshCache);
            } catch (Throwable e) {
                log.error("Executed refresh entity buffer error,cause:", e);
            }
        };
        if (entityBufferProperties.isAutoRefresh()) {
            ScheduledExecutorService scheduledExecutorService = Executors.newSingleThreadScheduledExecutor();
            this.scheduledExecutor = scheduledExecutorService;
            Long period = entityBufferProperties.getPeriod() == null ? DEFAULT_PERIOD : entityBufferProperties.getPeriod();
            scheduledExecutorService.scheduleAtFixedRate(task, 0, period, TimeUnit.MINUTES);
        } else {
            new Thread(task).start();
        }
    }

    @Override
    public void destroy() throws Exception {
        if (scheduledExecutor != null) {
            scheduledExecutor.shutdownNow();
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy