@Override public EventInput readFrom(Class<EventInput> chunkedInputClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inputStream) throws IOException, WebApplicationException { InputStream closeableInputStream = ReaderInterceptorExecutor.closeableInputStream(inputStream); return new EventInput( closeableInputStream, annotations, mediaType, headers, messageBodyWorkers.get(), propertiesDelegateProvider.get()); } }
public static void consumeEventStream(String url, Consumer consumer) throws Exception { Client client = ClientBuilder.newBuilder().register(new SseFeature()).build(); WebTarget target = client.target(url); EventInput e = null; while (true) { Thread.sleep(1000); if (e==null || e.isClosed()) { // (re)connect e = target.request().get(EventInput.class); e.setChunkType("text/event-stream"); } final InboundEvent inboundEvent = e.read(); if (inboundEvent == null) { break; } else { String data = inboundEvent.getData(); // do something here - notify observers, parse json etc } } System.out.println("connection closed"); }
public void run() { Client client = ClientBuilder.newBuilder().register(SseFeature.class).build(); WebTarget target = client.target("https://stream.example.com/"); EventInput eventInput = target.request().get(EventInput.class); while (!eventInput.isClosed()) { final InboundEvent inboundEvent = eventInput.read(); if (inboundEvent == null) { // connection has been closed break; } onEvent(inboundEvent); } } protected void onEvent(InboundEvent inboundEvent) { .... }
Client client = ClientBuilder.newBuilder() .register(SseFeature.class).build(); WebTarget target = client.target("http://localhost:9998/events"); EventInput eventInput = target.request().get(EventInput.class); while (!eventInput.isClosed()) { final InboundEvent inboundEvent = eventInput.read(); if (inboundEvent == null) { // connection has been closed break; } System.out.println(inboundEvent.getName() + "; " + inboundEvent.readData(String.class)); }
@Override public EventInput readFrom(Class<EventInput> chunkedInputClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inputStream) throws IOException, WebApplicationException { InputStream closeableInputStream = ReaderInterceptorExecutor.closeableInputStream(inputStream); return new EventInput( closeableInputStream, annotations, mediaType, headers, messageBodyWorkers.get(), propertiesDelegateProvider.get()); } }
@Override public EventInput readFrom(Class<EventInput> chunkedInputClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inputStream) throws IOException, WebApplicationException { InputStream closeableInputStream = ReaderInterceptorExecutor.closeableInputStream(inputStream); return new EventInput( closeableInputStream, annotations, mediaType, headers, messageBodyWorkers.get(), propertiesDelegateProvider.get()); } }
@Override public EventInput readFrom(Class<EventInput> chunkedInputClass, Type type, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> headers, InputStream inputStream) throws IOException, WebApplicationException { InputStream closeableInputStream = ReaderInterceptorExecutor.closeableInputStream(inputStream); return new EventInput( closeableInputStream, annotations, mediaType, headers, messageBodyWorkers.get(), propertiesDelegateProvider.get()); } }