Skip to content

Commit

Permalink
SimpleFuture增加异常捕获
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Oct 17, 2024
1 parent 767f9ec commit 6a16b47
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.leaderli.litool.core.event;

import java.util.function.BiConsumer;

/**
* 分发事件给监听器,并提供监听器注册和注销的方法。
* <p>
Expand All @@ -19,7 +21,6 @@ public class LiEventBus implements LiEventBusBehavior {
* @param <E> 事件类型
* @param <S> 消息类型
* @param listener 要注册的监听器
*
*/
public <E extends LiEventObject<S>, S> void registerListener(ILiEventListener<E, S> listener) {
liEventMap.put(listener.componentType(), listener);
Expand Down Expand Up @@ -48,29 +49,39 @@ public <E extends LiEventObject<S>, S> void unRegisterListener(ILiEventListener<
* @see ILiEventListener#after(LiEventBusBehavior)
*/
public <E extends LiEventObject<S>, S> void push(E event) {
if (event == null) {
return;
}
Class<E> eventType = (Class<E>) event.getClass();
liEventMap.compute(eventType, listener -> {

S source = event.getSource();
if (source == null) {
listener.onNull();
return;
}
if (listener.before(source)) {
push(event, new BiConsumer<SourceProvider<S>, ILiEventListener<E, S>>() {

try {
@Override
public void accept(SourceProvider<S> sourceProvider, ILiEventListener<E, S> listener) {

listener.listen(source);
S source = sourceProvider.source;
if (source == null) {
listener.onNull();
return;
}
try {
if (listener.before(source)) {
listener.listen(source);
listener.after(sourceProvider.liEventBusBehavior);
}
} catch (Throwable throwable) {
listener.onError(throwable);
return;
}
listener.after(this);
}
});

}

public <E extends LiEventObject<S>, S> void push(E event, BiConsumer<SourceProvider<S>, ILiEventListener<E, S>> biConsumer) {

if (event == null) {
return;
}
Class<E> eventType = (Class<E>) event.getClass();
SourceProvider<S> sourceProvider = new SourceProvider<>(event.getSource(), this);
liEventMap.compute(eventType, lc -> {
biConsumer.accept(sourceProvider, lc);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.leaderli.litool.core.event;

public interface LiEventListenerConsumer<E extends LiEventObject<S>, S> {
void accpt(S source, LiEventBusBehavior liEventBusBehavior, ILiEventListener<E, S> listener);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package io.leaderli.litool.core.event;


public class SourceProvider<S> {
public final S source;
public final LiEventBusBehavior liEventBusBehavior;


public SourceProvider(S source, LiEventBusBehavior liEventBusBehavior) {
this.source = source;
this.liEventBusBehavior = liEventBusBehavior;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.leaderli.litool.core.meta.LiBox;
import io.leaderli.litool.core.meta.LiTuple;
import io.leaderli.litool.core.util.ThreadUtil;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

Expand Down Expand Up @@ -81,8 +82,18 @@ void test2() {
@Test
void test3() {
LiEventBus liEventBus = new LiEventBus();
// liEventBus.push();

ThreadListener listener = new ThreadListener();
liEventBus.registerListener(listener);
liEventBus.push(new TestStringLiEventObject("123"));
Assertions.assertSame(Thread.currentThread(), listener.thread);
Assertions.assertEquals("123", listener.source);
ExecutorService executorService = Executors.newFixedThreadPool(1);
liEventBus.push(new TestStringLiEventObject("456"), (sourceProvider, l) -> {
executorService.submit(() -> l.listen(sourceProvider.source));
});
ThreadUtil.sleep(100);
Assertions.assertNotSame(Thread.currentThread(), listener.thread);
Assertions.assertEquals("456", listener.source);
}

@Test
Expand Down Expand Up @@ -174,6 +185,17 @@ public Class<TestStringLiEventObject> componentType() {
}
}

static class ThreadListener implements ILiEventListener<TestStringLiEventObject, String> {

Thread thread;
String source;

@Override
public void listen(String source) {
this.thread = Thread.currentThread();
this.source = source;
}
}

static class TempListener implements ILiEventListener<TestStringLiEventObject, String> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,11 @@ void testGetSimpleName() {
Assertions.assertEquals("true", StringUtils.getSimpleName(true));
Assertions.assertEquals("1", StringUtils.getSimpleName(1));
Assertions.assertEquals("1", StringUtils.getSimpleName("1"));
Assertions.assertEquals("[1, 2]", StringUtils.getSimpleName(new int[]{1, 2}));
int[] ints = {1, 2};
//noinspection ImplicitArrayToString
if ((ints + "").length() > 10) {
Assertions.assertEquals("[1, 2]", StringUtils.getSimpleName(ints));
}
Assertions.assertEquals("int", StringUtils.getSimpleName(int.class));
Assertions.assertEquals("Integer", StringUtils.getSimpleName(Integer.class));
Object obj = new Object() {
Expand Down

0 comments on commit 6a16b47

Please sign in to comment.