Class SpringTransactionSynchronizationListener
- java.lang.Object
-
- org.zkoss.zkplus.spring.SpringTransactionSynchronizationListener
-
- All Implemented Interfaces:
EventThreadCleanup
,EventThreadInit
,EventThreadResume
public class SpringTransactionSynchronizationListener extends java.lang.Object implements EventThreadInit, EventThreadCleanup, EventThreadResume
Listener to make sure each ZK thread got the same ThreadLocal value of the spring's org.springframework.transaction.support.TransactionSynchronizationManager; especially those thread bound resources.
This listener is used with Spring Framework (version 1.2.8+) "thread" bounded resources.
<listener> <description>Spring TransactionSynchronizationManager handler</description> <listener-class>org.zkoss.zkplus.spring.SpringTransactionSynchronizationListener</listener-class> </listener>
Applicable to Spring Framework version 2.x or later
- Author:
- henrichen
-
-
Constructor Summary
Constructors Constructor Description SpringTransactionSynchronizationListener()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
abortResume(Component comp, Event evt)
Called when the suspended event thread is aborted.void
afterResume(Component comp, Event evt)
Called after the suspended event thread is resumed.void
beforeResume(Component comp, Event evt)
Called just before the suspended event thread is resumed.void
cleanup(Component comp, Event evt, java.util.List errs)
Cleans up the event processing thread.void
complete(Component comp, Event evt)
Called in the serlvet thread to clean up.boolean
init(Component comp, Event evt)
Initialize the event processing thread before processing the event.void
prepare(Component comp, Event evt)
Prepares the initialization at the servlet thread.
-
-
-
Method Detail
-
prepare
public void prepare(Component comp, Event evt)
Description copied from interface:EventThreadInit
Prepares the initialization at the servlet thread.It is invoked in the servlet thread (and before
EventThreadInit.init(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
). Thus, you can NOT manipulate the desktop in this method.If this method throws an exception, it will abort the execution and shows an error message to the end user. Note:
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
won't be called if an exception is thrown in this method, since it executes in the main thread.In addition to throwing an exception, you can prevent an event from processing by returning false in
EventThreadInit.init(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
. The event is ignored 'silently' then.- Specified by:
prepare
in interfaceEventThreadInit
-
init
public boolean init(Component comp, Event evt)
Description copied from interface:EventThreadInit
Initialize the event processing thread before processing the event.The simplest form is
void init(Component c, Event e) {return true;}
Unlike
EventThreadInit.prepare(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
, it is invoked in the event processing thread (and afterEventThreadInit.prepare(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
). Thus, you can manipulate the desktop in this method such as creating a component.If you want to prevent an event from processing, you can return false in this method. For example, you might create a highlighted window and return false to prevent the user from accessing, if the system is too busy.
If the use of the event thread is disabled (
Configuration.isEventThreadEnabled()
returns false), this method is also invoked in the Servlet thread.If this method throws an exception, it will abort the execution and shows an error message to the end user (unless it is cleaned up by
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
).- Specified by:
init
in interfaceEventThreadInit
- Returns:
- if it is initialized successfully. If false is returned, the event is ignored, i.e., no event handler/listener will be invoked.
-
cleanup
public void cleanup(Component comp, Event evt, java.util.List errs)
Description copied from interface:EventThreadCleanup
Cleans up the event processing thread. It is called, after a event processing thread has processed an event.If this method threw an exception and errs is empty, the exception will be propagated back to the servlet thread and then reported to the user.
Note:
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
is called first in the event processing thread, and thenEventThreadCleanup.complete(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
is called in the servlet thread. Note:EventThreadCleanup.complete(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
of anEventThreadCleanup
instance is called only ifEventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
called against the same instance didn't throw any exception.If the use of the event thread is disabled (
Configuration.isEventThreadEnabled()
returns false), this method is also invoked in the Servlet thread.- Specified by:
cleanup
in interfaceEventThreadCleanup
errs
- a list of exceptions (java.lang.Throwable) if any exception occurred before this method is called, or null if no exception at all. Note: you can manipulate the list directly to add or clean up exceptions. For example, if exceptions are fixed correctly, you can call errs.clear() such that no error message will be displayed at the client.
-
complete
public void complete(Component comp, Event evt)
Description copied from interface:EventThreadCleanup
Called in the serlvet thread to clean up. It is called afterEventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
is called.Note:
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
is called first in the event processing thread, and thenEventThreadCleanup.complete(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
is called in the servlet thread.- Specified by:
complete
in interfaceEventThreadCleanup
-
beforeResume
public void beforeResume(Component comp, Event evt)
Description copied from interface:EventThreadResume
Called just before the suspended event thread is resumed. UnlikeEventThreadResume.afterResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
, it executes in the main thread that resumes the suspended event processing thread.If this method throws an exception, it will abort the execution and shows an error message to the end user. Note:
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
won't be called if an exception is thrown in this method, since it executes in the main thread.- Specified by:
beforeResume
in interfaceEventThreadResume
-
afterResume
public void afterResume(Component comp, Event evt)
Description copied from interface:EventThreadResume
Called after the suspended event thread is resumed. UnlikeEventThreadResume.beforeResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
, it executes in the event processing thread that is resumed.If this method throws an exception, it will abort the execution and shows an error message to the end user (unless it is cleaned up by
EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
).- Specified by:
afterResume
in interfaceEventThreadResume
-
abortResume
public void abortResume(Component comp, Event evt)
Description copied from interface:EventThreadResume
Called when the suspended event thread is aborted. It is called in the main thread (i.e., the servlet thread).If a suspended event thread is resumed normally,
EventThreadResume.beforeResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
andEventThreadResume.afterResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
are called. On the other hand, if it is aborted (usually caused by destroying the desktop that owns this thread),EventThreadResume.abortResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
is called instead.Note: if the suspended thread is aborted, none of
EventThreadResume.beforeResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
,EventThreadResume.afterResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
,EventThreadCleanup.cleanup(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event, java.util.List<java.lang.Throwable>)
, andEventThreadCleanup.complete(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
will be called. Thus, you have to do necessary cleanups in this method.- Specified by:
abortResume
in interfaceEventThreadResume
-
-