Skip to content

Commit

Permalink
下载事件优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Dec 26, 2023
1 parent 2316cf4 commit d8b8bb3
Show file tree
Hide file tree
Showing 17 changed files with 113 additions and 164 deletions.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;
import com.github.linyuzai.download.core.utils.DownloadUtils;
import lombok.Getter;

/**
* {@link Source} 压缩完成后会发布该事件。
*/
@Getter
public class SourceCompressedEvent extends AbstractSourceEvent {

/**
* 压缩后的对象
*/
private final Compression compression;

public SourceCompressedEvent(DownloadContext context, Source source, Compression compression) {
super(context, source);
this.compression = compression;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;
import lombok.Getter;

/**
* {@link Source} 压缩使用缓存时会发布该事件。
*/
@Getter
public class SourceCompressedUsingCacheEvent extends AbstractCompressSourceEvent {
public class SourceCompressedUsingCacheEvent extends AbstractSourceEvent {

/**
* 缓存
*/
private final String cache;

public SourceCompressedUsingCacheEvent(DownloadContext context, Source source, String cache) {
super(context, source, "Compress source using cache " + cache);
super(context, source);
this.cache = cache;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;
import lombok.Getter;

/**
* 当确定 {@link Source} 压缩格式时会发布该事件。
*/
@Getter
public class SourceCompressionFormatEvent extends AbstractCompressSourceEvent {
public class SourceCompressionFormatEvent extends AbstractSourceEvent {

/**
* 压缩格式
*/
private final String format;

public SourceCompressionFormatEvent(DownloadContext context, Source source, String format) {
super(context, source, "Compress source using " + format);
super(context, source);
this.format = format;
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;
import lombok.Getter;

Expand All @@ -10,15 +11,15 @@
* 当使用 {@link FileCompression} 文件压缩时会发布该事件。
*/
@Getter
public class SourceFileCompressionEvent extends AbstractCompressSourceEvent {
public class SourceFileCompressionEvent extends AbstractSourceEvent {

/**
* 压缩文件
*/
private final File file;

public SourceFileCompressionEvent(DownloadContext context, Source source, File file) {
super(context, source, "Compress source with file " + file.getAbsolutePath());
super(context, source);
this.file = file;
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;

/**
* 当使用 {@link MemoryCompression} 内存压缩时会发布该事件。
*/
public class SourceMemoryCompressionEvent extends AbstractCompressSourceEvent {
public class SourceMemoryCompressionEvent extends AbstractSourceEvent {

public SourceMemoryCompressionEvent(DownloadContext context, Source source) {
super(context, source, "Compress source in memory");
super(context, source);
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.source.AbstractSourceEvent;
import com.github.linyuzai.download.core.source.Source;

/**
* 当使用 {@link NoCompression} 不压缩时会发布该事件。
*/
public class SourceNoCompressionEvent extends AbstractCompressSourceEvent {
public class SourceNoCompressionEvent extends AbstractSourceEvent {

public SourceNoCompressionEvent(DownloadContext context, Source source) {
super(context, source, "Compression skipped");
super(context, source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,5 @@ public class DownloadErrorEvent extends DownloadContextEvent {
public DownloadErrorEvent(DownloadContext context, Throwable error) {
super(context);
this.error = error;
setMessage("Error");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public class DownloadSuccessEvent extends DownloadContextEvent {

public DownloadSuccessEvent(DownloadContext context) {
super(context);
setMessage("Success");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class CompressSourceHandler implements DownloadHandler, DownloadLifecycle
* 可通过 {@link Download#forceCompress()} 强制压缩。
* 根据指定或默认的压缩格式,通过 {@link SourceCompressorAdapter} 获得 {@link SourceCompressor},
* 通过 {@link SourceCompressor} 执行压缩。
* 发布 {@link AfterSourceCompressedEvent} 事件,
* 发布 {@link SourceCompressedEvent} 事件,
* 将压缩后的 {@link Compression} 设置到 {@link DownloadContext} 中。
*
* @param context {@link DownloadContext}
Expand All @@ -60,7 +60,7 @@ public Object handle(DownloadContext context, DownloadHandlerChain chain) {
DownloadWriter writer = writerAdapter.getWriter(source, context);
compression = compressor.compress(source, writer, context);
}
publisher.publish(new AfterSourceCompressedEvent(context, source, compression));
publisher.publish(new SourceCompressedEvent(context, source, compression));
context.set(Compression.class, compression);
return chain.next(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadEventListener;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;
import com.github.linyuzai.download.core.options.DownloadOptions;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.lang.reflect.Method;
import java.util.UUID;

/**
* 下载日志抽象类。
Expand All @@ -19,19 +17,11 @@
@NoArgsConstructor
public abstract class LoggingDownloadEventListener implements DownloadEventListener {

public static final String LOG_ID = "_download@log_id";

private boolean enabled;

@Override
public void onEvent(Object event) {
if (enabled) {
if (event instanceof DownloadStartedEvent) {
DownloadContext context = ((DownloadStartedEvent) event).getContext();
if (!hasLogId(context)) {
setLogId(context, generateLogId());
}
}
logOnEvent(event);
}
}
Expand All @@ -58,20 +48,4 @@ protected String getTag(Method method) {
return method.getDeclaringClass().getSimpleName() + "#" + method.getName() + " ";
}
}

protected String generateLogId() {
return UUID.randomUUID().toString();
}

protected boolean hasLogId(DownloadContext context) {
return context.contains(LOG_ID);
}

protected void setLogId(DownloadContext context, String logId) {
context.set(LOG_ID, logId);
}

protected String getLogId(DownloadContext context) {
return context.get(LOG_ID);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadCompletedEvent;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;
import com.github.linyuzai.download.core.load.SourceLoadingProgressEvent;
import com.github.linyuzai.download.core.web.ResponseWritingProgressEvent;
import com.github.linyuzai.download.core.write.ProgressEvent;
Expand All @@ -23,11 +23,6 @@
@AllArgsConstructor
public class ProgressCalculationLogger extends LoggingDownloadEventListener {

/**
* 进度缓存
*/
private final Map<String, Map<Object, ProgressInterval>> progressIntervalMap = new ConcurrentHashMap<>();

/**
* 打印间隔
*/
Expand All @@ -48,24 +43,23 @@ public void onProgress(ProgressEvent event) {
}

/**
* 监听到 {@link DownloadContext} 销毁事件时,移除缓存;
* 监听到 {@link ProgressEvent} 事件时,打印更新进度。
*
* @param event 事件
*/
@Override
public void logOnEvent(Object event) {
if (event instanceof DownloadCompletedEvent) {
DownloadContext context = ((DownloadCompletedEvent) event).getContext();
String id = getLogId(context);
progressIntervalMap.remove(id);
if (event instanceof DownloadStartedEvent) {
DownloadContext context = ((DownloadStartedEvent) event).getContext();
context.set(ProgressInterval.class, new ConcurrentHashMap<>());
} else if (event instanceof ProgressEvent) {
ProgressEvent pde = (ProgressEvent) event;
DownloadContext context = ((ProgressEvent) event).getContext();
String id = getLogId(context);
progressIntervalMap.computeIfAbsent(id, k ->
new ConcurrentHashMap<>()).computeIfAbsent(getId(pde), o ->
new ProgressInterval(this::onProgress)).publish(pde);
Map<Object, ProgressInterval> map = context.get(ProgressInterval.class);
if (map != null) {
map.computeIfAbsent(getId(pde), o ->
new ProgressInterval(this::onProgress)).publish(pde);
}
}
}

Expand Down
Loading

0 comments on commit d8b8bb3

Please sign in to comment.