⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shutdownhook.java

📁 this is example use EJB with jboss.
💻 JAVA
字号:
/*
 * Copyright(C) 2008, NTT AT Co., Ltd.
 * Project: AWGView
 *
 * Notes:
 *  N/A
 *
 * Record of change:
 * Date         Version      Name       Content
 * 2008/01/20   1.0          TriNT      First create       
 */
package jp.co.ntt.awgview.server.thread;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;

import jp.co.ntt.awgview.server.AppMainServer;
import jp.co.ntt.awgview.server.common.LogWriter;
import jp.co.ntt.awgview.server.dao.DBFunctionsManager;
import jp.co.ntt.awgview.server.snmp.SnmpManager;

/**
 * Class name 	: ObjectFIFO <BR>
 * 
 * Package 		: jp.co.ntt_at.awgview.server.thread <BR>
 * 
 * Description	: Demonstrate adding a Shutdown Hook to the Runtime.
 * This class provides a way of trapping a shutdown of the virtual machine<BR>
 * 
 * @author 		: AI&T
 * @version 	: 1.0
 */
public class ShutdownHook extends Thread {
	/** SNMP thread that need to safety stop */	
	private Thread safeThreadSnmp = null;
	
	/** EJB thread that need to safety stop */
	private Thread safeThreadEJB = null;
	
	/** Keep reference SnmpManager object to wake it up when shutdown */	
	private SnmpManager snmpManager = null;	

	/**
	 * ShutdownHook <BR>
	 * 
	 */	
	public ShutdownHook() {
		super("Thread shutdown hook");		
	}

	/**
	 * set Snmp Thread <BR>	 * 
	 * @param   safeThreadSnmp thread
	 * 
	 */	
	public void setSnmpThread(Thread safeThreadSnmp) {		
		this.safeThreadSnmp = safeThreadSnmp;
	}
	
	/**
	 * set EJB Thread <BR>	
	 * @param   safeThreadEJB thread
	 * 
	 */	
	public void setEJBThread(Thread safeThreadEJB) {		
		this.safeThreadEJB = safeThreadEJB;
	}
	
	/**
	 * setSnmpManagerProcess Setting Process <BR>
	 * @param  snmpManager
	 */	
	public void setSnmpManagerProcess(SnmpManager snmpManager) {
		this.snmpManager = snmpManager;
	}

	/**
	 * Running thread process to execute stop server<BR>
	 * 
	 */	
	public synchronized void run() {	
		try {
			LogWriter.getSNMPLogger().info("Starting clean-up enviroment of AppMainServer Object");			
			//Close all connection
			DBFunctionsManager.closeAllConnection();
			
			if ( (this.snmpManager != null)) {				
				//Stop service				
				synchronized (this.snmpManager) {
					// Server will be stopped in snmpManager process					
					this.snmpManager.notify();
				}				
			} 
			
			AppMainServer.shutDown();
			
			try{
				if (safeThreadSnmp != null){					
					if (safeThreadSnmp.isAlive()){
						LogWriter.getSNMPLogger().info("[ShutdownHook] Closed thread " +  safeThreadEJB.getName());
						safeThreadSnmp.interrupt();					
					}				
				}
			}catch (Exception e){
			}
			
			try{	
				if (safeThreadEJB != null){
					if (safeThreadEJB.isAlive()){
						safeThreadEJB.interrupt();
						LogWriter.getSNMPLogger().info("[ShutdownHook] Closed thread " +  safeThreadEJB.getName());
					}
				}			
			}catch (Exception e){				
			}
						
			interruptAllThread();
			LogWriter.getSNMPLogger().info("Clean-up enviroment is success");
			LogWriter.getSNMPLogger().info("AWGView Server stopped");

		} catch (Exception e) {
			LogWriter.getSNMPLogger().error("Error when execute stop server " + e.toString());
			e.printStackTrace();
		}
	}
	
	/**
	 * Interrupt All Thread when AWG-Server shutdown
	 * @throws InterruptedException 
	 */
	private void interruptAllThread(){
		Thread[] threadList = getAllThreads();
		
		if (threadList == null){
			return;
		}
		
		int len = threadList.length;
		LogWriter.getSNMPLogger().info("Number Thread is running: " + threadList.length);
		for (int i=0; i < len; i++){
			try{
				LogWriter.getSNMPLogger().debug("Thread [" + i + "] name = " 
						+ threadList[i].getName() + " >> ID = " + threadList[i].getId() 
						+ " >> Prority = " + threadList[i].getPriority());
				if ((threadList[i] != null) && (threadList[i].isAlive())){
					// Interrupting thread.
					threadList[i].interrupt();				
					threadList[i].join();
					LogWriter.getSNMPLogger().warn("Thread [" + i + "] interrupted = " +  threadList[i].isInterrupted());
				}
			}catch (Exception e){				
			}
		}		
	}	
	
	/**
	 * Get a list of all threads. Since there is always at least one thread,
	 * this method never returns null or an empty array.
	 * 
	 * @return an array of threads
	 */
	public static Thread[] getAllThreads() {
		try{
			final ThreadGroup root = getRootThreadGroup();
			
			if (root == null){				
				return null;
			}
			
			final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
			if (thbean == null){				
				return null;
			}
			
			int nAlloc = thbean.getThreadCount();
			if (nAlloc == 0){				
				return null;
			}
			
			int len = 0;
			Thread[] threads = null;
			do {
				nAlloc *= 2;
				threads = new Thread[nAlloc];
				len = root.enumerate(threads, true);
			} while (len == nAlloc);
			return java.util.Arrays.copyOf(threads, len);
		}catch (Exception e){
			//e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Get the root thread group in the thread group tree.
	 * Since there is always a root thread group, this method never returns null.
	 * 
	 * @return the root thread group
	 */
	public static ThreadGroup getRootThreadGroup() {
		ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
		ThreadGroup parentThreadGroup = null;
		
		if (threadGroup == null){			
			return null;
		}
		
		while ((parentThreadGroup = threadGroup.getParent()) != null){
			threadGroup = parentThreadGroup;
		}
		return parentThreadGroup;
	}	
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -