Package org.glassfish.jersey.media.sse

Examples of org.glassfish.jersey.media.sse.EventOutput


    public static class SseResource {

        @GET
        @Produces(SseFeature.SERVER_SENT_EVENTS)
        public EventOutput getIt() throws IOException {
            final EventOutput result = new EventOutput();
            result.write(new OutboundEvent.Builder().name("event1").data(String.class, "ping").build());
            result.write(new OutboundEvent.Builder().name("event2").data(String.class, "pong").build());
            result.close();
            return result;
        }
View Full Code Here


     */
    @GET
    @Produces(SseFeature.SERVER_SENT_EVENTS)
    public EventOutput getMessageStream() {
        LOGGER.info("--> SSE connection received.");
        final EventOutput eventOutput = new EventOutput();
        broadcaster.add(eventOutput);
        return eventOutput;
    }
View Full Code Here

    @GET
    @Path("events")
    @Produces(SseFeature.SERVER_SENT_EVENTS)
    public EventOutput itemEvents(
            @HeaderParam(SseFeature.LAST_EVENT_ID_HEADER) @DefaultValue("-1") int lastEventId) {
        final EventOutput eventOutput = new EventOutput();

        if (lastEventId >= 0) {
            LOGGER.info("Received last event id :" + lastEventId);

            // decide the reconnect handling strategy based on current reconnect delay value.
View Full Code Here

    public static class SseTestResource {

        @GET
        @Path("single")
        public EventOutput getSingleEvent() {
            final EventOutput output = new EventOutput();
            try {
                return output;
            } finally {
                new Thread() {
                    public void run() {
                        try {
                            output.write(new OutboundEvent.Builder().data(String.class, "single").build());
                            output.close();
                        } catch (Exception e) {
                            e.printStackTrace();
                            fail();
                        }
                    }
View Full Code Here

        }

        @GET
        @Path("closed-single")
        public EventOutput getClosedSingleEvent() throws IOException {
            final EventOutput output = new EventOutput();
            output.write(new OutboundEvent.Builder().data(String.class, "closed").build());
            output.close();
            return output;
        }
View Full Code Here

        }

        @GET
        @Path("closed-empty")
        public EventOutput getClosedEmpty() throws IOException {
            final EventOutput output = new EventOutput();
            output.close();
            return output;
        }
View Full Code Here

        @GET
        @Path("charset")
        @Produces("text/event-stream;charset=utf-8")
        public EventOutput getSseWithCharset() throws IOException {
            final EventOutput output = new EventOutput();
            output.write(new OutboundEvent.Builder().data(String.class, "charset").build());
            output.close();
            return output;
        }
View Full Code Here

    public static class SseResource {

        @GET
        @Produces(SseFeature.SERVER_SENT_EVENTS)
        public EventOutput getServerSentEvents() {
            final EventOutput eventOutput = new EventOutput();
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        int i = 0;
                        while (latch.getCount() > 0) {

                            // send message with name "message-to-client" -> should be read by the client
                            eventOutput.write(new OutboundEvent.Builder()
                                    .name("message-to-client")
                                    .mediaType(MediaType.TEXT_PLAIN_TYPE)
                                    .data(Integer.class, i)
                                    .build());

                            // send another event with name "foo" -> should be ignored by the client
                            eventOutput.write(new OutboundEvent.Builder()
                                    .name("foo")
                                    .mediaType(MediaType.TEXT_PLAIN_TYPE)
                                    .data(String.class, "bar")
                                    .build());

                            // send another un-mamed event -> should be ignored by the client
                            eventOutput.write(new OutboundEvent.Builder()
                                    .mediaType(MediaType.TEXT_PLAIN_TYPE)
                                    .data(String.class, "baz")
                                    .build());
                            latch.countDown();
                            i++;
                        }

                    } catch (IOException e) {
                        throw new RuntimeException("Error when writing the event.", e);
                    } finally {
                        try {
                            eventOutput.close();
                        } catch (IOException ioClose) {
                            throw new RuntimeException("Error when closing the event output.", ioClose);
                        }
                    }
                }
View Full Code Here

            throw new RuntimeException(ex);
        }
    }

    protected Response executeCommandAsSse(ParameterMap data) {
        EventOutput ec = ResourceUtil.runCommandWithSse(commandName, data, null, null);
        return Response.status(HttpURLConnection.HTTP_OK).entity(ec).build();
    }
View Full Code Here

    @GET
    @Path("events")
    @Produces(SseFeature.SERVER_SENT_EVENTS)
    public EventOutput itemEvents() {
        final EventOutput eventOutput = new EventOutput();
        BROADCASTER.add(eventOutput);
        return eventOutput;
    }
View Full Code Here

TOP

Related Classes of org.glassfish.jersey.media.sse.EventOutput

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.