Skip to content

Annotations support

Egor Ushakov edited this page Aug 14, 2018 · 6 revisions

Async Annotations

In IDEA 2018.1 you can use code annotations to setup instrumenting agent for debugger Async Stack Traces. Add dependency on the updated jetbrains annotations (Maven Central) and use @Async.Schedule and @Async.Execute in your code.

When the position annotated with @Async.Schedule is reached, it saves the current stack in a storage map. When the position annotated with @Async.Execute is reached, it tries to find the related stack in the storage map.

You can annotate:

  • method - then this value is used as a key
  • parameter - then the parameter value is used as a key

Source code of the Async annotations is in this repository.

Debug the example:

import org.jetbrains.annotations.Async;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class AsyncSchedulerExample {
    private static final BlockingQueue<Integer> queue = new LinkedBlockingQueue<>();

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                while (true) {
                    process(queue.take());
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }).start();
        schedule(1);
        schedule(2);
        schedule(3);
    }

    private static void schedule(@Async.Schedule Integer i) throws InterruptedException {
        System.out.println("Scheduling " + i);
        queue.put(i);
    }

    private static void process(@Async.Execute Integer i) {
        // Put a breakpoint here
        System.out.println("Processing " + i);
    }
}
Clone this wiki locally