Search in sources :

Example 1 with BulkheadEvent

use of io.github.resilience4j.bulkhead.event.BulkheadEvent in project resilience4j by resilience4j.

the class BulkheadChain method execute.

@Override
public void execute(Chain chain) throws Exception {
    String prefix = chain.getRegistry().get(Resilience4jConfig.class).getEndpoints().getBulkheads().getPath();
    chain.prefix(prefix, chain1 -> {
        chain1.get("events", ctx -> Promise.<BulkheadEventsEndpointResponse>async(d -> {
            BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getAllEventConsumer().flatMap(CircularEventConsumer::getBufferedEvents).sorted(Comparator.comparing(BulkheadEvent::getCreationTime)).map(BulkheadEventDTO::createEventDTO).toJavaList());
            d.success(response);
        }).then(r -> ctx.render(Jackson.json(r))));
        chain1.get("stream/events", ctx -> {
            Seq<Flowable<BulkheadEvent>> eventStreams = bulkheadRegistry.getAllBulkheads().map(bulkhead -> RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()));
            Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(Flowable.merge(eventStreams), e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name", ctx -> {
            String bulkheadName = ctx.getPathTokens().get("name");
            Promise.<BulkheadEventsEndpointResponse>async(d -> {
                BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(bulkheadName).getBufferedEvents().map(BulkheadEventDTO::createEventDTO).toJavaList());
                d.success(response);
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name", ctx -> {
            String bulkheadName = ctx.getPathTokens().get("name");
            Bulkhead bulkhead = bulkheadRegistry.getAllBulkheads().find(b -> b.getName().equals(bulkheadName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("bulkhead with name %s not found", bulkheadName)));
            Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()), e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
        chain1.get("events/:name/:type", ctx -> {
            String bulkheadName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            Promise.<BulkheadEventsEndpointResponse>async(d -> {
                BulkheadEventsEndpointResponse response = new BulkheadEventsEndpointResponse(eventConsumerRegistry.getEventConsumer(bulkheadName).getBufferedEvents().filter(event -> event.getEventType() == BulkheadEvent.Type.valueOf(eventType.toUpperCase())).map(BulkheadEventDTO::createEventDTO).toJavaList());
                d.success(response);
            }).then(r -> ctx.render(Jackson.json(r)));
        });
        chain1.get("stream/events/:name/:type", ctx -> {
            String bulkheadName = ctx.getPathTokens().get("name");
            String eventType = ctx.getPathTokens().get("type");
            Bulkhead bulkhead = bulkheadRegistry.getAllBulkheads().find(b -> b.getName().equals(bulkheadName)).getOrElseThrow(() -> new IllegalArgumentException(String.format("bulkhead with name %s not found", bulkheadName)));
            Flowable<BulkheadEvent> eventStream = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()).filter(event -> event.getEventType() == BulkheadEvent.Type.valueOf(eventType.toUpperCase()));
            Function<BulkheadEvent, String> data = b -> Jackson.getObjectWriter(chain1.getRegistry()).writeValueAsString(BulkheadEventDTO.createEventDTO(b));
            ServerSentEvents events = ServerSentEvents.serverSentEvents(eventStream, e -> e.id(BulkheadEvent::getBulkheadName).event(c -> c.getEventType().name()).data(data));
            ctx.render(events);
        });
    });
}
Also used : Function(ratpack.func.Function) RxJava2Adapter(io.github.resilience4j.adapter.RxJava2Adapter) BulkheadRegistry(io.github.resilience4j.bulkhead.BulkheadRegistry) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) Promise(ratpack.exec.Promise) BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) Jackson(ratpack.jackson.Jackson) ServerSentEvents(ratpack.sse.ServerSentEvents) Chain(ratpack.handling.Chain) Inject(javax.inject.Inject) Flowable(io.reactivex.Flowable) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) Action(ratpack.func.Action) Seq(io.vavr.collection.Seq) Comparator(java.util.Comparator) EventConsumerRegistry(io.github.resilience4j.consumer.EventConsumerRegistry) Resilience4jConfig(io.github.resilience4j.ratpack.Resilience4jConfig) BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) Bulkhead(io.github.resilience4j.bulkhead.Bulkhead) CircularEventConsumer(io.github.resilience4j.consumer.CircularEventConsumer) ServerSentEvents(ratpack.sse.ServerSentEvents) Flowable(io.reactivex.Flowable)

Example 2 with BulkheadEvent

use of io.github.resilience4j.bulkhead.event.BulkheadEvent in project resilience4j by resilience4j.

the class SemaphoreBulkheadTest method setUp.

@Before
public void setUp() {
    BulkheadConfig config = BulkheadConfig.custom().maxConcurrentCalls(2).maxWaitTime(0).build();
    bulkhead = Bulkhead.of("test", config);
    testSubscriber = RxJava2Adapter.toFlowable(bulkhead.getEventPublisher()).map(BulkheadEvent::getEventType).test();
}
Also used : BulkheadEvent(io.github.resilience4j.bulkhead.event.BulkheadEvent) BulkheadConfig(io.github.resilience4j.bulkhead.BulkheadConfig) Before(org.junit.Before)

Aggregations

BulkheadEvent (io.github.resilience4j.bulkhead.event.BulkheadEvent)2 RxJava2Adapter (io.github.resilience4j.adapter.RxJava2Adapter)1 Bulkhead (io.github.resilience4j.bulkhead.Bulkhead)1 BulkheadConfig (io.github.resilience4j.bulkhead.BulkheadConfig)1 BulkheadRegistry (io.github.resilience4j.bulkhead.BulkheadRegistry)1 CircularEventConsumer (io.github.resilience4j.consumer.CircularEventConsumer)1 EventConsumerRegistry (io.github.resilience4j.consumer.EventConsumerRegistry)1 Resilience4jConfig (io.github.resilience4j.ratpack.Resilience4jConfig)1 Flowable (io.reactivex.Flowable)1 Seq (io.vavr.collection.Seq)1 Comparator (java.util.Comparator)1 Inject (javax.inject.Inject)1 Before (org.junit.Before)1 Promise (ratpack.exec.Promise)1 Action (ratpack.func.Action)1 Function (ratpack.func.Function)1 Chain (ratpack.handling.Chain)1 Jackson (ratpack.jackson.Jackson)1 ServerSentEvents (ratpack.sse.ServerSentEvents)1