-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
wangqi
committed
Mar 20, 2024
1 parent
601e03e
commit df43daf
Showing
4 changed files
with
197 additions
and
1 deletion.
There are no files selected for viewing
55 changes: 55 additions & 0 deletions
55
milky-common/src/main/java/cn/sliew/milky/common/concurrent/CallableWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
milky-common/src/main/java/cn/sliew/milky/common/concurrent/RunnableWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
milky-common/src/main/java/cn/sliew/milky/common/filter/DelegatingActionListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |