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
Constructor and Description |
---|
SpringTransactionSynchronizationListener() |
Modifier and Type | Method and 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.
|
public SpringTransactionSynchronizationListener()
public void prepare(Component comp, Event evt)
EventThreadInit
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.
prepare
in interface EventThreadInit
public boolean init(Component comp, Event evt)
EventThreadInit
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 after EventThreadInit.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>)
).
init
in interface EventThreadInit
public void cleanup(Component comp, Event evt, java.util.List errs)
EventThreadCleanup
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 then EventThreadCleanup.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 an EventThreadCleanup
instance is called
only if EventThreadCleanup.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.
cleanup
in interface EventThreadCleanup
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.public void complete(Component comp, Event evt)
EventThreadCleanup
EventThreadCleanup.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 then EventThreadCleanup.complete(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
is called in the servlet thread.
complete
in interface EventThreadCleanup
public void beforeResume(Component comp, Event evt)
EventThreadResume
EventThreadResume.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.
beforeResume
in interface EventThreadResume
public void afterResume(Component comp, Event evt)
EventThreadResume
EventThreadResume.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>)
).
afterResume
in interface EventThreadResume
public void abortResume(Component comp, Event evt)
EventThreadResume
If a suspended event thread is resumed normally, EventThreadResume.beforeResume(org.zkoss.zk.ui.Component, org.zkoss.zk.ui.event.Event)
and EventThreadResume.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>)
, and
EventThreadCleanup.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.
abortResume
in interface EventThreadResume
Copyright © 2005-2023 Potix Corporation. All Rights Reserved.