Search in sources :

Example 1 with Headline

use of io.micronaut.http.client.docs.streaming.Headline in project micronaut-core by micronaut-projects.

the class HeadlineController method streamHeadlines.

// tag::streaming[]
// <1>
@Get(value = "/headlines", produces = MediaType.TEXT_EVENT_STREAM)
Flux<Event<Headline>> streamHeadlines() {
    return Flux.<Event<Headline>>create((emitter) -> {
        // <2>
        Headline headline = new Headline();
        headline.setText("Latest Headline at " + ZonedDateTime.now());
        emitter.next(Event.of(headline));
        emitter.complete();
    }, FluxSink.OverflowStrategy.BUFFER).repeat(// <3>
    100).delayElements(// <4>
    Duration.of(1, ChronoUnit.SECONDS));
}
Also used : Headline(io.micronaut.http.client.docs.streaming.Headline) Event(io.micronaut.http.sse.Event) Get(io.micronaut.http.annotation.Get)

Example 2 with Headline

use of io.micronaut.http.client.docs.streaming.Headline in project micronaut-core by micronaut-projects.

the class HeadlineControllerTest method testClientAnnotationStreaming.

// tag::streamingClient[]
@Test
public void testClientAnnotationStreaming() throws Exception {
    try (EmbeddedServer embeddedServer = ApplicationContext.run(EmbeddedServer.class)) {
        HeadlineClient headlineClient = embeddedServer.getApplicationContext().getBean(HeadlineClient.class);
        Event<Headline> headline = headlineClient.streamHeadlines().blockFirst();
        Assertions.assertNotNull(headline);
        Assertions.assertTrue(headline.getData().getText().startsWith("Latest Headline"));
    }
}
Also used : Headline(io.micronaut.http.client.docs.streaming.Headline) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) Test(org.junit.jupiter.api.Test)

Aggregations

Headline (io.micronaut.http.client.docs.streaming.Headline)2 Get (io.micronaut.http.annotation.Get)1 Event (io.micronaut.http.sse.Event)1 EmbeddedServer (io.micronaut.runtime.server.EmbeddedServer)1 Test (org.junit.jupiter.api.Test)1