Class VaadinExternalListener<S>
- Type Parameters:
S
- The type of the event source
Listeners that are part of a Vaadin application should use this superclass if they are going to be registered
with non-Vaadin event sources, where "non-Vaadin" means not operating in the context of a VaadinSession
and not holding the corresponding lock.
Use the methods register()
and unregister()
from within a VaadinSession
to control
listener registration.
Subclasses must then implement registerExternal(S)
and unregisterExternal(S)
to perform the actual external
registration/unregister operations, and then when notified by the external source, must use handleEvent()
to relay the notification back to the caller with the VaadinSession
safely locked.
Use of this class will prevent these bugs from happening:
- Notifications being delivered without the
VaadinSession
being locked; - Race conditions between unregistering a listener and receiving a listener notification; and
- Memory leaks that occur if the Vaadin application is closed while listeners are still registered
Note: when listening to event sources that are scoped to specific Vaadin application instances and that already originate events within the proper Vaadin application context (i.e., non-external event sources), then the use of this class is not necessary, but it also won't hurt to use it.
-
Constructor Summary
ModifierConstructorDescriptionprotected
VaadinExternalListener
(S eventSource) Convenience constructor.protected
VaadinExternalListener
(S eventSource, VaadinSession session) Primary constructor. -
Method Summary
Modifier and TypeMethodDescriptionfinal S
Get the event source with which this instance is (or was) registered as a listener.final VaadinSession
Get theVaadinSession
with which this instance is associated.protected void
handleEvent
(Runnable action) Execute the given listener action while theVaadinSession
with which this instance is associated is locked.protected void
Handle aRuntimeException
thrown by thehandleEvent
callback.void
register()
Register as a listener on configured event source.protected abstract void
registerExternal
(S eventSource) Register as a listener on the given external event source.void
Un-register as a listener on configured event source.protected abstract void
unregisterExternal
(S eventSource) Register as a listener from the given external event source.
-
Constructor Details
-
VaadinExternalListener
Convenience constructor. Equivalent to:VaadinExternalListener(eventSource, VaadinUtil.getCurrentSession())
- Parameters:
eventSource
- the event source on which this listener will register- Throws:
IllegalArgumentException
- ifeventSource
is nullIllegalStateException
- if there is noVaadinSession
associated with the current thread
-
VaadinExternalListener
Primary constructor.- Parameters:
eventSource
- the event source on which this listener will register whenregister()
is invokedsession
- the associated Vaadin application's session- Throws:
IllegalArgumentException
- if either parameter is null
-
-
Method Details
-
register
public void register()Register as a listener on configured event source.This method may be invoked from any context.
- Throws:
IllegalStateException
- if this instance is already registered
-
unregister
public void unregister()Un-register as a listener on configured event source.This method may be invoked from any context.
-
getSession
Get theVaadinSession
with which this instance is associated.- Returns:
- associated session
-
getEventSource
Get the event source with which this instance is (or was) registered as a listener.- Returns:
- associated event source
-
handleEvent
Execute the given listener action while theVaadinSession
with which this instance is associated is locked.Subclass listener methods should handle events by invoking this method to ensure proper locking to avoid race conditions.
This method delegates to
VaadinUtil.accessSession()
to actually handle the event.- Parameters:
action
- action to perform- Throws:
IllegalArgumentException
- ifaction
is null
-
handleException
Handle aRuntimeException
thrown by thehandleEvent
callback.The implementation in
VaadinExternalListener
logs an error and then re-throws the exception.- Parameters:
e
- exception thrown
-
registerExternal
Register as a listener on the given external 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
-
unregisterExternal
Register as a listener from the given external 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
-