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

📄 fwservicemonitor.java

📁 一个实用工具类
💻 JAVA
字号:
/* * Copyright (C) butor.com. All rights reserved. * * This software is published under the terms of the GNU Library General * Public License (GNU LGPL), a copy of which has been included with this * distribution in the LICENSE.txt file.  */package org.butor.fwService.starter;import java.util.Enumeration;import java.util.Vector;import org.butor.fwService.IFwServiceMonitor;import org.butor.helper.UniqueIDGenerator;import org.butor.log.Log;/** * This class represent a thread whose sole purpose is to call * the isAlive() methods of the ServiceClass classes of the * system.  Normally, the isAlive() method is used by the * ServiceClass instances to check their own sanity. * * @author  Denis Benoit, aiman sawan */public class FwServiceMonitor implements Runnable {	protected Vector f_listeners;	protected Thread f_thread = null;	protected int f_isAlivePeriod;	protected boolean f_shutdown = false;	protected static FwServiceMonitor s_serviceMonitor = null;	private static final int DEFAULT_IS_ALIVE_PERIOD = 5 * 1000; // 5 seonds	/**	 * FwServiceMonitor constructor.	 */	public FwServiceMonitor(int isAlivePeriod) {		super();		f_listeners = new Vector();		f_isAlivePeriod = isAlivePeriod;		if (isAlivePeriod < DEFAULT_IS_ALIVE_PERIOD) {			// Sorry! Not too fast.			f_isAlivePeriod = DEFAULT_IS_ALIVE_PERIOD;		}		f_thread = new Thread(this, "ServiceMonitor_" +UniqueIDGenerator.getNewId());		if (f_thread == null) {			Log.logStr(this, Log.LOG_TYPE_ERROR,				"Constructor", "Could not create thread!");			return;		}		s_serviceMonitor = this;				// Gateways won't shut it down...		f_thread.setDaemon(true);				f_thread.start();	}	/**	 * This method does an infinite loop:	 * - Call the isAlive() method of every ServiceClass of the system;	 * - Sleep an interval of mintNbMilliSecSleep millisecond	 */	public void run() {		Log.logStr(this, Log.LOG_TYPE_INFO, "run", "isAlive check start");		while (!f_shutdown) {			try {				isAlive();				if (f_shutdown) {					break;				}				Thread.sleep(f_isAlivePeriod);							} catch (InterruptedException e) {				// Just continue			}		}	}	/**	 * This method calls the isAlive() method of every ServiceClass	 * class of the system.	 */	private void isAlive() {		Enumeration en = f_listeners.elements();		Object oneElement = null;		Log.logStr(			Log.LOG_LEVEL_LOW,			this,			Log.LOG_TYPE_INFO,			"isAlive",			"Checking [" +f_listeners.size() + " service(s)] ...");				synchronized (en) {			while (en.hasMoreElements()) {				try {					oneElement = en.nextElement();					((IFwServiceMonitor) oneElement).handleIsAlive();									} catch (ClassCastException e) {					Log.logStr(this, Log.LOG_TYPE_WARN, "isAlive",						e.getMessage() + ":" + oneElement.toString());				}			}		}	}	/**	 * shutdown this thread.	 */	public void shutdown() {		f_shutdown = true;		synchronized (f_listeners) {			Enumeration services = f_listeners.elements();			Object oneElement = null;						while (services.hasMoreElements()) {				try {					oneElement = services.nextElement();					((IFwServiceMonitor) oneElement).shutdown();									} catch (ClassCastException e) {					Log.logStr(this, Log.LOG_TYPE_WARN, "shutdown()",						e.getMessage() + ":" + oneElement.toString());				}			}		}				f_thread.interrupt();		s_serviceMonitor = null;		Log.logStr(this, Log.LOG_TYPE_INFO, "shutdown()", "Stopped.");	}	/**	 * Register org.butor.web.listener to the keep it alive.	 * The keep alive call back will be done fast (one each	 * 5 seconds or so).	 * The org.butor.web.listener must do its processing once each multiple calls.	 * Example: Each 15 calls of handleIsAlive() the org.butor.web.listener do its	 * processing and reset its counter.	 */	public static void register(IFwServiceMonitor listener) {		Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_INFO, "register()", 			"Registering org.butor.web.listener [" +listener +"]");					if (s_serviceMonitor != null) {			s_serviceMonitor.addListener(listener);		} else {			Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_WARN, "register()", 				"Not yet initialized.");		}	}	/**	 * Unregister a org.butor.web.listener from the keep alive list.	 */	public static void unregister(IFwServiceMonitor listener) {		Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_INFO, "unregister()", 			"Unregistering org.butor.web.listener [" +listener +"]");					if (s_serviceMonitor != null) {			s_serviceMonitor.removeListener(listener);		} else {			Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_WARN, "unregister()", 				"Not yet initialized.");		}	}	/**	 * Unregister a org.butor.web.listener from the keep alive list.	 */	public static boolean registered(IFwServiceMonitor listener) {		Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_INFO, "registered()", 			"registered? listenr [" +listener +"]");					if (s_serviceMonitor != null) {			return s_serviceMonitor.exists(listener);		} else {			Log.logStr(FwServiceMonitor.class, Log.LOG_TYPE_WARN, "registered()", 				"Not yet initialized.");		}		return false;	}	/**	 * Add org.butor.web.listener to the keep alive list.	 */	protected void addListener(IFwServiceMonitor listener) {		if (listener == null) {			Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "removeListener()", 				"Got null org.butor.web.listener!");			return;		}		synchronized(f_listeners) {			if (f_listeners.contains(listener)) {				Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "addListener()", 					"Already added org.butor.web.listener [" +listener +"]");			} else {				f_listeners.add(listener);				Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "addListener()", 					"added org.butor.web.listener [" +listener +"]");			}		}	}	/**	 * Add org.butor.web.listener to the keep alive list.	 */	protected boolean exists(IFwServiceMonitor listener) {		if (listener == null) {			Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "exists()", 				"Got null org.butor.web.listener!");			return false;		}		synchronized(f_listeners) {			return (f_listeners.contains(listener));		}	}	/**	 * Remove org.butor.web.listener from the keep alive list.	 */	protected void removeListener(IFwServiceMonitor listener) {		if (listener == null) {			Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "removeListener()", 				"Got null org.butor.web.listener!");			return;		}				synchronized(f_listeners) {			if (f_listeners.contains(listener)) {				f_listeners.remove(listener);				Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_INFO, "removeListener()", 					"removed org.butor.web.listener [" +listener +"]");			} else {				Log.logStr(Log.LOG_LEVEL_LOW, this, Log.LOG_TYPE_WARN, "removeListener()", 					"Not registered org.butor.web.listener [" +listener +"]");			}		}	}}

⌨️ 快捷键说明

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