JStorm Source Code Analysis: The Loop Task AsyncLoopThread
Overview
AsyncLoopThread is a custom loop task execution utility in JStorm. The implementation is not complex, and on its own may not warrant a dedicated article. However, it is so widely used in JStorm — for features such as supervisor/nimbus heartbeat, fetching new topologies, updating worker status, and many more — that it’s worth introducing here, as it will also help with reading other parts of the codebase.
Usage
AsyncLoopThread can actually be extracted and used in your own projects. It’s quite convenient to use. You can refer to https://github.com/lcy362/Scenes/blob/4d8ec4ff166060cf5d491c33a96f5c86e8389333/src/main/java/com/mallow/jstormcode/AsyncLoop.java
You only need to first define a RunnableCallback class. RunnableCallback is a thread class wrapped by JStorm that enhances the Runnable interface with callback support and the ability to proactively shut down.
1 | public class TestThread extends RunnableCallback { |
Here we only implement two methods: run and getResult. The run method is the thread execution method, same as a normal thread. getResult returns the task execution interval time, in seconds.
After that, you just need to pass this TestThread as a parameter to AsyncLoopThread, and it will implement the loop execution of TestThread’s run method.
1 | public static void main(String args[]) { |
Design
Next, let’s look at the specific implementation of AsyncLoopThread.
The core code is the init method:
1 | private void init(RunnableCallback afn, boolean daemon, RunnableCallback kill_fn, int priority, boolean start) { |
The main thing here is the creation of a new AsyncLoopRunnable class, where the more core code resides. Additionally, note the kill_fn parameter, which is used in AsyncLoopRunnable to kill the task. The thread is also configured with several settings: priority, exception handling, etc. Furthermore, all threads here are set as daemon threads, which is why in the example above, we need to explicitly make the main thread sleep to observe the running effect.
Next, let’s look at AsyncLoopRunnable:
1 | public void run() { |
This code is also quite simple — it loops through the execution of RunnableCallback’s run method. During execution, there are several scenarios that cause the task to be interrupted or the process to be killed directly: shutdown, needQuit(), and exceptions. Among these, the needQuit() method controls the task execution speed based on the getResult return value mentioned earlier.
Original article: https://lichuanyang.top/posts/31761
