Class VaadinExternalListener<S>

  • Type Parameters:
    S - The type of the event source
    Direct Known Subclasses:
    VaadinApplicationListener

    public abstract class VaadinExternalListener<S>
    extends Object
    Support superclass customized for use by listeners that are part of a Vaadin application when listening to non-Vaadin ("external") event sources.

    Listeners that are part of a Vaadin application should use this superclass if they are going to be registered with non-Vaadin event sources, and use the methods register() and unregister() to control listener registration. Subclasses implement register(S) and register(S) to perform the actual listener registration/unregister operations.

    Use of this class will ensure two things:

    • Events are delivered in the proper Vaadin application context; and
    • The listener is automatically unregistered from the external event source when the Vaadin application is closed, avoiding a memory leak

    Important: subclass listener methods must use handleEvent() when handling events. This will ensure proper locking to avoid race conditions.

    Note: when listening to event sources that are scoped to specific Vaadin application instances and already originate events within the proper Vaadin application context (i.e., event sources that are not external to the Vaadin application), then the use of this superclass is not necessary (however, it also doesn't hurt to use it anyway).

    See Also:
    VaadinUtil.invoke(com.vaadin.server.VaadinSession, java.lang.Runnable), VaadinApplicationScope, VaadinApplicationListener, SpringVaadinSessionListener
    • Method Detail

      • isAsynchronous

        public boolean isAsynchronous()
        Determine whether this instance is configured for asynchronous notification. Default false.
        Returns:
        true if this instance will notify asynchronously
      • setAsynchronous

        public void setAsynchronous​(boolean asynchronous)
        Set whether to notify asynchronously. If set, VaadinUtil.invokeLater() will be used for notifications, so that these occur on a different thread from the original notifying thread.
        Parameters:
        asynchronous - true to notify asynchronously, false for synchronous notifications
        See Also:
        handleEvent()
      • register

        public void register()
        Register as a listener on configured event source.

        This also registers a SessionDestroyListener on the configured Vaadin application, so that when the application closes we can unregister this instance from the event source to avoid a memory leak.

      • unregister

        public void unregister()
        Un-register as a listener on configured event source.

        This also unregisters the SessionDestroyListener registered by register().

      • getSession

        public final VaadinSession getSession()
        Get the VaadinSession (aka Vaadin application) with which this instance is associated.
        Returns:
        associated session
      • getEventSource

        public final S getEventSource()
        Get the event source with which this instance is (or was) registered as a listener.
        Returns:
        associated event source
      • register

        protected abstract void register​(S eventSource)
        Register as a listener on the given event source.

        Subclass must implement this to perform the actual listener registration.

        Parameters:
        eventSource - event source, never null; will be same as provided to the constructor
      • unregister

        protected abstract void unregister​(S eventSource)
        Register as a listener from the given event source.

        Subclass must implement this to perform the actual listener registration.

        Parameters:
        eventSource - event source, never null; will be same as provided to the constructor