Enabling an application to wait for a messaging engine to start

If an application depends on a messaging engine being available, then the messaging engine must be started before the application can be run.

If you want application server to start an application automatically, you should develop your applications to test that any required messaging engine has been started and, if needed, wait for the messaging engine. If this is technique used in a startup bean, then the startup bean method should perform the work (to test and wait) in a separate thread, using the standard WorkManager methods, so that the application server startup is not delayed.

For an example of code to test and wait for a messaging engine, see the following code extract:
import java.util.Iterator;
import javax.management.ObjectName;                                     
import com.ibm.websphere.management.AdminService;                       
import com.ibm.websphere.management.AdminServiceFactory;      

    String messagingEngineName = "messagingEngineName"; // Messaging engine to check if started? for example "node01.server1-bus1"                                      
    boolean meStarted = false;                                              
                                                                        
    AdminService adminService = AdminServiceFactory.getAdminService();                                  
    while (!meStarted) {                                                    
      String filterString = "WebSphere®:type=SIBMessagingEngine,name=" + messagingEngineName + ",*";                                             
      boolean foundBean = false;                                          
      ObjectName objectName = null;                                       
      try {                                                               
        ObjectName objectNameFilter = new ObjectName(filterString);     
        Iterator iter = adminService.queryNames(objectNameFilter,null).iterator();                                                       
        while (iter.hasNext()) {                                        
            objectName = (ObjectName) iter.next();                      
            foundBean = true;                                           
            break;                                                      
        }                                                               
      } catch (Exception e) {                                             
        e.printStackTrace();                                            
      }                                                                   
      if (foundBean) {                                                    
        // We have found messaging engine MBean which means it is initialized, now check if it is actually in Started state?              
        meStarted = ((Boolean) adminService.invoke(objectName, "isStarted", null, null)).booleanValue();                               
      }                                                                   
                                                                        
      if (!meStarted) {                                                   
        // messaging engine is not started yet so sleep (wait) for a bit...     
        Thread.sleep(5000);                                             
      }                                                                   
    }



Related concepts
Learning about messaging engines
Concept topic    

Terms of Use | Feedback

Last updated: Aug 29, 2010 8:25:23 PM CDT
http://www14.software.ibm.com/webapp/wsbroker/redirect?version=vela&product=was-nd-zos&topic=cjb0003_
File name: cjb0003_.html