Created
October 9, 2019 14:59
-
-
Save martinandersson/4d31ab3a0a78191e6c25af709f378754 to your computer and use it in GitHub Desktop.
Micronaut Endpoint/Controller that reveals the calling thread.
This file contains hidden or 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
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