org.glassfish.jersey.media.sse
Class EventSource

java.lang.Object
  extended by org.glassfish.jersey.media.sse.EventSource
All Implemented Interfaces:
EventListener

public class EventSource
extends java.lang.Object
implements EventListener

Client for reading and processing Server Sent Events.

When EventSource is created, it makes GET request to given URI and waits for incoming Events. Whenever any event is received, onEvent(Event) is called and listeners (if any) are notified (see addEventListener(String, EventListener) and addEventListener(String, EventListener).

Instances of this class are thread safe.

Author:
Pavel Bucek (pavel.bucek at oracle.com)

Constructor Summary
EventSource(Target target)
          Create new instance and start processing incoming Events in newly created ExecutorService (Executors.newCachedThreadPool().
EventSource(Target target, java.util.concurrent.ExecutorService executorService)
          Create new instance and start processing incoming Events in provided ExecutorService.
 
Method Summary
 void addEventListener(EventListener listener)
          Add EventListener.
 void addEventListener(java.lang.String eventName, EventListener listener)
          Add EventListener which will be called only when Event with certain name is received.
 void onEvent(Event event)
          Called when Event is received.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

EventSource

public EventSource(Target target)
Create new instance and start processing incoming Events in newly created ExecutorService (Executors.newCachedThreadPool().

Parameters:
target - JAX-RS Target instance which will be used to obtain Events.

EventSource

public EventSource(Target target,
                   java.util.concurrent.ExecutorService executorService)
Create new instance and start processing incoming Events in provided ExecutorService.

Parameters:
target - JAX-RS Target instance which will be used to obtain Events.
executorService - used for processing events.
Method Detail

addEventListener

public void addEventListener(EventListener listener)
Add EventListener.

Parameters:
listener - EventListener to add to current instance.

addEventListener

public void addEventListener(java.lang.String eventName,
                             EventListener listener)
Add EventListener which will be called only when Event with certain name is received.

Parameters:
eventName - Event name.
listener - EventListener to add to current instance.

onEvent

public void onEvent(Event event)
Called when Event is received. Empty implementations, users can override this method to handle incoming Events. Please note that this is the ONLY way how to be absolutely sure that you won't miss any incoming Event. Initial request is made right after EventSource is created and processing starts immediately. EventListeners registered after Event is received won't be notified.

Specified by:
onEvent in interface EventListener
Parameters:
event - received event.


Copyright © 2007-2012 Oracle Corporation. All Rights Reserved. Use is subject to license terms.