org.shoulder.batch.progress.DefaultBatchProgressCache Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of shoulder-batch Show documentation
Show all versions of shoulder-batch Show documentation
Shoulder 扩展-批处理模块,提供批量数据导入、导出、异步校验、导入历史记录管理等能力。
The newest version!
package org.shoulder.batch.progress;
import org.shoulder.core.concurrent.Threads;
import org.springframework.cache.Cache;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
* 任务进度存储:单机存储
*
* @author lym
*/
public class DefaultBatchProgressCache implements BatchProgressCache {
/**
* 允许进度以多种方式保存在其他地方,如 redis
*/
private final Cache progressCache;
/**
* 缓存名
*/
public static final String CACHE_NAME = "shoulder-batch-progressCache_DEFAULT";
public DefaultBatchProgressCache(Cache progressCache) {
this.progressCache = progressCache;
}
@SuppressWarnings("unchecked")
public T getNativeCache() {
return (T) progressCache.getNativeCache();
}
/**
* 触发异步刷进度
* 备注:场景适用于单个机器写,多个机器读
*
* @param task 需要被刷进度的 task
*/
@Override
public void triggerFlushProgress(ProgressAble task) {
BatchProgressRecord batchProgressRecord = task.getBatchProgress();
progressCache.put(batchProgressRecord.getId(), batchProgressRecord);
Threads.execute(genFlushProgressTask(task));
}
@Override
public void evict(String id) {
progressCache.evict(id);
}
@Override
public void clear() {
progressCache.clear();
}
/**
* 刷缓存线程不安全,需要加锁,最少是 batchId 级别,这里默认实现直接用 sync 了
*
* @param batchProgress 进度
*/
@Override
public synchronized void flushProgress(ProgressAble batchProgress) {
BatchProgressRecord record = batchProgress.getBatchProgress();
progressCache.put(record.getId(), record);
}
@Override
public Iterable getAllTaskProgressId() {
Object nativeCache = getNativeCache();
if (nativeCache instanceof Map) {
Map