Initialization and Cleanup of Event Processing Thread

From Documentation
DocumentationZK Developer's GuideZK in DepthEvent listening and processingInitialization and Cleanup of Event Processing Thread
Initialization and Cleanup of Event Processing Thread


Stop.png This documentation is for an older version of ZK. For the latest one, please click here.


Initialization Before Processing Each Event

An event listener is executed in an event processing thread. Sometimes, you have to initialize the thread before processing any event.

A typical example is to initialize the thread for the authentication. Some J2EE or Web containers store authentication information in the thread local storage, such that they could re-authenticate automatically when needed.

To initialize the event processing threads, you have to register a class, that implements the org.zkoss.zk.ui.event.EventThreadInit interface, to the listener element in the WEB-INF/zk.xml file[1].

Once registered, an instance of the specified class is constructed in the main thread (aka., the servlet thread), before starting an event processing thread. Then, the init method of the instance is called at the context of the event processing thread before doing anything else.

Notice that the constructor and the init method are invoked at different thread such that developers could retrieve thread-dependent data from one thread and pass to anther.

Here is an example for the authentication mechanism of JBoss[2]. In this example, we retrieve the information stored in the servlet thread in the constructor. Then, we initialize the event processing thread when the init method is called.

 import java.security.Principal;
 import org.jboss.security.SecurityAssociation;
 import org.zkoss.zk.ui.Component;
 import org.zkoss.zk.ui.event.Event;
 import org.zkoss.zk.ui.event.EventThreadInit;

 public class JBossEventThreadInit implements EventThreadInit {
     private final Principal _principal;
     private final Object _credential;
     /** Retrieve info at the constructor, which runs at the servlet thread. */
     public JBossEventThreadInit() {
         _principal = SecurityAssociation.getPrincipal();
         _credential = SecurityAssociation.getCredential();
     }
     //-- EventThreadInit --//
     /** Initial the event processing thread at this method. */
     public void init(Component comp, Event evt) {
         SecurityAssociation.setPrincipal(_principal);
         SecurityAssociation.setCredential(_credential);
     }
 }

Then, in WEB-INF/zk.xml, you have to specify as follows.

<zk>
    <listener>
        <listener-class>JBossEventThreadInit</listener-class>
    </listener>
</zk>

Cleanup After Processed Each Event

Similarly, you might have to clean up an event processing thread after it has processed an event.

A typical example is to close the transaction, if it is not closed properly.

To cleanup the event processing threads, you have to register a listener class, that implements the org.zkoss.zk.ui.event.EventThreadCleanup interface, to the listener element in the WEB-INF/zk.xml file.

<zk>
    <listener>
        <listener-class>my.MyEventThreadCleanup</listener-class>
    <listener>
</zk>

Notes

  1. It is described more detailedly in Appendix B in the Developer's Reference.
  2. http://www.jboss.org



Last Update : 2022/01/19

Copyright © Potix Corporation. This article is licensed under GNU Free Documentation License.