Skip to content

Instantly share code, notes, and snippets.

@martinandersson
Created October 9, 2019 14:59
Show Gist options
  • Save martinandersson/4d31ab3a0a78191e6c25af709f378754 to your computer and use it in GitHub Desktop.
Save martinandersson/4d31ab3a0a78191e6c25af709f378754 to your computer and use it in GitHub Desktop.
Micronaut Endpoint/Controller that reveals the calling thread.
package com.hello.controllerthread;
import io.micronaut.http.annotation.Controller;
import io.micronaut.http.annotation.Get;
import io.reactivex.Single;
import java.util.Map;
@Controller("/thread")
public class ThreadEndpoint
{
static final class ThreadInfo {
public final String id, name, group;
ThreadInfo() {
Thread t = Thread.currentThread();
id = String.valueOf(t.getId());
name = t.getName();
group = t.getThreadGroup().getName();
}
}
@Get("/reactive")
Single<Map<String, ThreadInfo>> reactiveReturn() {
var executor = entryOfThread("executor");
return Single.fromCallable(() ->
Map.ofEntries(executor, entryOfThread("subscriber")));
}
@Get("/nonreactive")
Map.Entry<String, ThreadInfo> nonReactive() {
return entryOfThread("executor");
}
private static Map.Entry<String, ThreadInfo> entryOfThread(String key) {
return Map.entry(key, new ThreadInfo());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment