Skip to content

Commit

Permalink
feature: update action listener
Browse files Browse the repository at this point in the history
  • Loading branch information
wangqi committed Mar 20, 2024
1 parent 601e03e commit df43daf
Show file tree
Hide file tree
Showing 4 changed files with 197 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package cn.sliew.milky.common.concurrent;

import java.util.concurrent.Callable;

public interface CallableWrapper<T> extends Callable<T> {

@Override
default T call() throws Exception {
T result = null;
try {
onBefore();
result = doCall();
onAfter(result);
} catch (Exception t) {
onFailure(t);
} finally {
onFinal();
}
return result;
}

/**
* This method has the same semantics as {@link Runnable#run()}
*
* @throws InterruptedException if the run method throws an InterruptedException
*/
T doCall() throws Exception;

/**
* This method is invoked for all exception thrown by {@link #doCall()}
*/
void onFailure(Exception e);

/**
* This method is called before all execution for init.
*/
default void onBefore() throws Exception {

}

/**
* This method is called after execution.
*/
default void onAfter(T result) throws Exception {

}

/**
* This method is called in a finally block after successful execution
* or on a rejection.
*/
default void onFinal() {
// nothing by default
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
package cn.sliew.milky.common.concurrent;

public interface RunnableWrapper extends Runnable {

@Override
default void run() {
try {
onBefore();
doRun();
onAfter();
} catch (Exception t) {
onFailure(t);
} finally {
onFinal();
}
}

/**
* This method has the same semantics as {@link Runnable#run()}
*
* @throws InterruptedException if the run method throws an InterruptedException
*/
void doRun() throws Exception;

/**
* This method is invoked for all exception thrown by {@link #doRun()}
*/
void onFailure(Exception e);

/**
* This method is called before all execution for init.
*/
default void onBefore() throws Exception {

}

/**
* This method is called after execution.
*/
default void onAfter() throws Exception {

}

/**
* This method is called in a finally block after successful execution
* or on a rejection.
*/
default void onFinal() {
// nothing by default
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
package cn.sliew.milky.common.filter;

import cn.sliew.milky.common.concurrent.CallableWrapper;
import cn.sliew.milky.common.concurrent.RunnableWrapper;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.BiConsumer;

/**
* A listener for action responses or failures.
*/
Expand All @@ -13,5 +20,68 @@ public interface ActionListener<Response> {
/**
* A failure caused by an exception at some phase of the task.
*/
void onFailure(Exception e);
void onFailure(Throwable throwable);

default CompletableFuture<Response> toFuture() {
CompletableFuture<Response> future = new CompletableFuture();
future.whenComplete((response, throwable) -> {
if (throwable != null) {
onFailure(throwable);
} else {
onResponse(response);
}
});
return future;
}

default BiConsumer<Response, Throwable> toBiConsumer() {
return new BiConsumer<Response, Throwable>() {
@Override
public void accept(Response response, Throwable throwable) {
if (throwable != null) {
onFailure(throwable);
} else {
onResponse(response);
}
}
};
}

static Runnable wrap(Runnable runnable, ActionListener listener) {
return new RunnableWrapper() {
@Override
public void doRun() throws Exception {
runnable.run();
}

@Override
public void onAfter() {
listener.onResponse(null);
}

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}
};
}

static <T> Callable<T> wrap(Callable<T> callable, ActionListener<T> listener) {
return new CallableWrapper<T>() {
@Override
public T doCall() throws Exception {
return callable.call();
}

@Override
public void onFailure(Exception e) {
listener.onFailure(e);
}

@Override
public void onAfter(T result) throws Exception {
listener.onResponse(result);
}
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package cn.sliew.milky.common.filter;

public class DelegatingActionListener<Response> implements ActionListener<Response> {

private final ActionListener<Response> delegate;

public DelegatingActionListener(ActionListener<Response> delegate) {
this.delegate = delegate;
}

@Override
public void onResponse(Response response) {
delegate.onResponse(response);
}

@Override
public void onFailure(Throwable throwable) {
delegate.onFailure(throwable);
}
}

0 comments on commit df43daf

Please sign in to comment.