Skip to content

Commit

Permalink
下载事件优化
Browse files Browse the repository at this point in the history
  • Loading branch information
Linyuzai committed Dec 25, 2023
1 parent 3e0208e commit 6380d54
Show file tree
Hide file tree
Showing 46 changed files with 162 additions and 360 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadContextEvent;
import lombok.Getter;

/**
* {@link Compression} 事件。
*/
@Getter
public class AbstractCompressionEvent extends DownloadContextEvent {

private final Compression compression;

public AbstractCompressionEvent(DownloadContext context, Compression compression) {
super(context);
this.compression = compression;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public Compression compress(Source source, DownloadWriter writer, DownloadContex
File cache = new File(dir, cacheName);
//缓存是否存在
if (cache.exists()) {
publisher.publish(new SourceCompressedCacheUsedEvent(context, source, cache.getAbsolutePath()));
publisher.publish(new SourceCompressedUsingCacheEvent(context, source, cache.getAbsolutePath()));
} else {
publisher.publish(new SourceFileCompressionEvent(context, source, cache));
//写入缓存文件
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadContextEvent;
import lombok.Getter;

/**
* {@link Compression} 的缓存删除时会发布该事件。
*/
@Getter
public class CompressionCacheDeletedEvent extends DownloadContextEvent {

/**
* 删除缓存的 {@link Compression}
*/
private final Compression compression;
public class CompressionCacheDeletedEvent extends AbstractCompressionEvent {

public CompressionCacheDeletedEvent(DownloadContext context, Compression compression) {
super(context);
setMessage("Compression cache deleted");
this.compression = compression;
super(context, compression);
}
}
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
package com.github.linyuzai.download.core.compress;

import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadContextEvent;
import lombok.Getter;

/**
* {@link Compression} 的资源释放时会发布该事件。
*/
@Getter
public class CompressionReleasedEvent extends DownloadContextEvent {

/**
* 被释放资源的 {@link Compression}
*/
private final Compression compression;
public class CompressionReleasedEvent extends AbstractCompressionEvent {

public CompressionReleasedEvent(DownloadContext context, Compression compression) {
super(context);
setMessage("Compression resource released");
this.compression = compression;
super(context, compression);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@
* {@link Source} 压缩使用缓存时会发布该事件。
*/
@Getter
public class SourceCompressedCacheUsedEvent extends AbstractCompressSourceEvent {
public class SourceCompressedUsingCacheEvent extends AbstractCompressSourceEvent {

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

public SourceCompressedCacheUsedEvent(DownloadContext context, Source source, String cache) {
public SourceCompressedUsingCacheEvent(DownloadContext context, Source source, String cache) {
super(context, source, "Compress source using cache " + cache);
this.cache = cache;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,6 @@ public abstract class AbstractDownloadEvent implements DownloadEvent {
* 事件信息
*/
private String message;

private long timestamp = System.currentTimeMillis();
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,5 @@ public class DownloadStartedEvent extends DownloadContextEvent {

public DownloadStartedEvent(DownloadContext context) {
super(context);
setMessage("Context initialized");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class CreateSourceHandler implements DownloadHandler, DownloadLifecycleLi
* 通过 {@link SourceFactoryAdapter} 获得适配的 {@link SourceFactory},
* 通过 {@link SourceFactory} 创建对应的 {@link Source},
* 将 {@link Source} 设置到 {@link DownloadContext} 中,
* 发布 {@link AfterSourceCreatedEvent} 事件。
* 发布 {@link SourceCreatedEvent} 事件。
*
* @param context {@link DownloadContext}
*/
Expand All @@ -39,7 +39,7 @@ public Object handle(DownloadContext context, DownloadHandlerChain chain) {
Source source = factory.create(original, context);
context.set(Source.class, source);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
publisher.publish(new AfterSourceCreatedEvent(context, source));
publisher.publish(new SourceCreatedEvent(context, source));
return chain.next(context);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import com.github.linyuzai.download.core.event.DownloadEventPublisher;
import com.github.linyuzai.download.core.handler.DownloadHandler;
import com.github.linyuzai.download.core.handler.DownloadHandlerChain;
import com.github.linyuzai.download.core.load.AfterSourceLoadedEvent;
import com.github.linyuzai.download.core.load.SourceLoadedEvent;
import com.github.linyuzai.download.core.load.SourceLoader;
import com.github.linyuzai.download.core.source.Source;
import lombok.Getter;
Expand All @@ -25,7 +25,7 @@ public class LoadSourceHandler implements DownloadHandler {
/**
* 加载 {@link Source}。
* 使用 {@link SourceLoader} 加载所有的 {@link Source},
* 发布 {@link AfterSourceLoadedEvent} 事件,
* 发布 {@link SourceLoadedEvent} 事件,
* 设置新的 {@link Source} 到 {@link DownloadContext} 中。
*
* @param context {@link DownloadContext}
Expand All @@ -35,7 +35,7 @@ public Object handle(DownloadContext context, DownloadHandlerChain chain) {
Source source = context.get(Source.class);
DownloadEventPublisher publisher = DownloadEventPublisher.get(context);
sourceLoader.load(source, context);
publisher.publish(new AfterSourceLoadedEvent(context, source));
publisher.publish(new SourceLoadedEvent(context, source));
return chain.next(context);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public boolean support(DownloadContext context) {
* 设置 {@link DownloadResponse} 的响应头,
* 将 {@link Compression} 写入到 {@link DownloadResponse} 中
* 并发布 {@link ResponseWritingProgressEvent} 事件,
* 最后发布 {@link AfterResponseWrittenEvent} 事件。
* 最后发布 {@link ResponseWrittenEvent} 事件。
*
* @param context {@link DownloadContext}
*/
Expand Down Expand Up @@ -85,7 +85,7 @@ public void accept(OutputStream os) {
}
os.flush();
}
}, () -> chain.next(context), () -> publisher.publish(new AfterResponseWrittenEvent(context)));
}, () -> chain.next(context), () -> publisher.publish(new ResponseWrittenEvent(context)));
} else {
return chain.next(context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public void load(DownloadContext context) {
File cache = new File(dir, nameToUse);
//缓存存在
if (cache.exists()) {
publisher.publish(new SourceLoadedCacheUsedEvent(context, this, cache.getAbsolutePath()));
publisher.publish(new SourceLoadedUsingCacheEvent(context, this, cache.getAbsolutePath()));
} else {
//写到缓存文件
FileOutputStream fos = new FileOutputStream(cache);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,23 @@
import java.util.Collection;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executor;
import java.util.function.Function;

public class CompletableFutureSourceLoader extends ConcurrentSourceLoader {

@SneakyThrows
@Override
public void concurrentLoad(Collection<Source> sources, DownloadContext context) {
Executor executor = DownloadExecutor.getExecutor(context);
CompletableFuture<?>[] futures;
Function<Source, CompletableFuture<?>> function;
if (executor == null) {
futures = sources.stream()
.map(it -> CompletableFuture.runAsync(() -> it.load(context)))
.toArray(CompletableFuture[]::new);
function = it -> CompletableFuture.runAsync(() -> it.load(context));
} else {
futures = sources.stream()
.map(it -> CompletableFuture.runAsync(() -> it.load(context), executor))
.toArray(CompletableFuture[]::new);
function = it -> CompletableFuture.runAsync(() -> it.load(context), executor);
}
CompletableFuture<?>[] futures = sources.stream()
.map(function)
.toArray(CompletableFuture[]::new);
CompletableFuture.allOf(futures).get();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@
/**
* 支持并发的 {@link SourceLoader}。
*/
@Getter
@NoArgsConstructor
@AllArgsConstructor
public abstract class ConcurrentSourceLoader implements SourceLoader {

/**
* 单个文件的时候串行加载
*/
@Getter
@Setter
private boolean serialOnSingle = true;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
package com.github.linyuzai.download.core.load;

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 Source} 会发布该事件。
*/
public class SourceAlreadyLoadedEvent extends AbstractLoadSourceEvent {
public class SourceAlreadyLoadedEvent extends AbstractSourceEvent {

public SourceAlreadyLoadedEvent(DownloadContext context, Source source) {
super(context, source, "Skip load " + source.getDescription() + " because it has already been loaded");
super(context, source);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,19 @@
import lombok.NonNull;

/**
* {@link Source} 加载相关的事件的父类
* {@link Source} 加载完成后会发布该事件
*/
@Getter
public abstract class AbstractLoadSourceEvent extends DownloadContextEvent {
public class SourceLoadedEvent extends DownloadContextEvent {

/**
* 被加载的 {@link Source}
*/
@NonNull
private final Source source;

public AbstractLoadSourceEvent(DownloadContext context, @NonNull Source source, String message) {
public SourceLoadedEvent(DownloadContext context, @NonNull Source source) {
super(context);
this.source = source;
setMessage(message);
}
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package com.github.linyuzai.download.core.load;

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 SourceLoadedCacheUsedEvent extends AbstractLoadSourceEvent {
public class SourceLoadedUsingCacheEvent extends AbstractSourceEvent {

private final String cache;

public SourceLoadedCacheUsedEvent(DownloadContext context, Source source, String cache) {
super(context, source, "Load " + source.getDescription() + " using cache " + cache);
public SourceLoadedUsingCacheEvent(DownloadContext context, Source source, String cache) {
super(context, source);
this.cache = cache;
}
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,15 @@
package com.github.linyuzai.download.core.logger;

import com.github.linyuzai.download.core.compress.AbstractCompressSourceEvent;
import com.github.linyuzai.download.core.compress.CompressionCacheDeletedEvent;
import com.github.linyuzai.download.core.compress.CompressionReleasedEvent;
import com.github.linyuzai.download.core.compress.*;
import com.github.linyuzai.download.core.context.DownloadContext;
import com.github.linyuzai.download.core.event.DownloadCompletedEvent;
import com.github.linyuzai.download.core.event.DownloadContextEvent;
import com.github.linyuzai.download.core.event.DownloadStartedEvent;
import com.github.linyuzai.download.core.load.AbstractLoadSourceEvent;
import com.github.linyuzai.download.core.source.AbstractCreateSourceEvent;
import com.github.linyuzai.download.core.source.Source;
import com.github.linyuzai.download.core.source.SourceCacheDeletedEvent;
import com.github.linyuzai.download.core.source.SourceReleasedEvent;
import com.github.linyuzai.download.core.web.AbstractWriteResponseEvent;
import com.github.linyuzai.download.core.load.SourceAlreadyLoadedEvent;
import com.github.linyuzai.download.core.load.SourceLoadedEvent;
import com.github.linyuzai.download.core.load.SourceLoadedUsingCacheEvent;
import com.github.linyuzai.download.core.source.*;
import com.github.linyuzai.download.core.web.ResponseWrittenEvent;

/**
* 标准流程日志。
Expand All @@ -31,17 +29,46 @@ public void logOnEvent(Object event) {
if (event instanceof DownloadContextEvent) {
DownloadContextEvent dce = (DownloadContextEvent) event;
DownloadContext context = dce.getContext();
if (event instanceof DownloadStartedEvent ||
event instanceof AbstractCreateSourceEvent ||
event instanceof AbstractLoadSourceEvent ||
event instanceof AbstractCompressSourceEvent ||
event instanceof AbstractWriteResponseEvent ||
event instanceof SourceReleasedEvent ||
event instanceof SourceCacheDeletedEvent ||
event instanceof CompressionReleasedEvent ||
event instanceof CompressionCacheDeletedEvent) {
log(context, dce.getMessage());
if (event instanceof DownloadStartedEvent) {
log(context, "Context initialized");
} else if (event instanceof DownloadCompletedEvent) {
log(context, "Context destroyed");
} else if (event instanceof SourceCreatedEvent) {
log(context, "Source created: " + getSource(event));
} else if (event instanceof SourceLoadedEvent) {
log(context, "Source loaded: " + getSource(event));
} else if (event instanceof SourceAlreadyLoadedEvent) {
log(context, "Skip load " + getSource(event) + " for already loaded");
} else if (event instanceof SourceLoadedUsingCacheEvent) {
String cache = ((SourceLoadedUsingCacheEvent) event).getCache();
log(context, "Load " + getSource(event) + " using cache " + cache);
} else if (event instanceof AbstractCompressSourceEvent) {
//TODO 压缩事件
} else if (event instanceof ResponseWrittenEvent) {
log(context, "Response written");
} else if (event instanceof SourceCacheDeletedEvent) {
log(context, "Source " + getSource(event) + " cache deleted");
} else if (event instanceof SourceReleasedEvent) {
log(context, "Source " + getSource(event) + " resource released");
} else if (event instanceof CompressionCacheDeletedEvent) {
log(context, "Compression " + getCompression(event) + " cache deleted");
} else if (event instanceof CompressionReleasedEvent) {
log(context, "Compression " + getCompression(event) + " resource released");
}
}
}

protected String getSource(Object event) {
if (event instanceof AbstractSourceEvent) {
return ((AbstractSourceEvent) event).getSource().getDescription();
}
return "[Unknown]";
}

protected String getCompression(Object event) {
if (event instanceof AbstractCompressionEvent) {
return ((AbstractCompressionEvent) event).getCompression().getDescription();
}
return "[Unknown]";
}
}
Loading

0 comments on commit 6380d54

Please sign in to comment.