Search in sources :

Example 1 with Headline

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

the class HeadlineController method streamHeadlines.

// tag::streaming[]
// <1>
@Get(value = "/headlines", processes = MediaType.TEXT_EVENT_STREAM)
Publisher<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.docs.streaming.Headline) Event(io.micronaut.http.sse.Event) Get(io.micronaut.http.annotation.Get)

Example 2 with Headline

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

the class HeadlineControllerSpec 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 = Mono.from(headlineClient.streamHeadlines()).block();
        assertNotNull(headline);
        assertTrue(headline.getData().getText().startsWith("Latest Headline"));
    }
}
Also used : Headline(io.micronaut.docs.streaming.Headline) EmbeddedServer(io.micronaut.runtime.server.EmbeddedServer) Test(org.junit.Test)

Aggregations

Headline (io.micronaut.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.Test)1