|
Struts example source code file (UtilTimerStack.java)
The Struts UtilTimerStack.java source code/* * Copyright (c) 2002-2003, Atlassian Software Systems Pty Ltd All rights reserved. * * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * * * Redistributions of source code must retain the above copyright notice, * this list of conditions and the following disclaimer. * * Redistributions in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation and/or * other materials provided with the distribution. * * Neither the name of Atlassian Software Systems Pty Ltd nor the names of * its contributors may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ package com.opensymphony.xwork2.util.profiling; import com.opensymphony.xwork2.util.logging.Logger; import com.opensymphony.xwork2.util.logging.LoggerFactory; /** * A timer stack. * * <p /> * * <!-- START SNIPPET: profilingAspect_struts2 --> * * Struts2 profiling aspects involves the following :- * <ul> * <li>ActionContextCleanUp * <li>FreemarkerPageFilter * <li>DispatcherFilter * <ul> * <li>Dispatcher * <ul> * <li>creation of DefaultActionProxy * <ul> * <li>creation of DefaultActionInvocation * <ul> * <li>creation of Action * </ul> * </ul> * <li>execution of DefaultActionProxy * <ul> * <li>invocation of DefaultActionInvocation * <ul> * <li>invocation of Interceptors * <li>invocation of Action * <li>invocation of PreResultListener * <li>invocation of Result * </ul> * </ul> * </ul> * </ul> * </ul> * * <!-- END SNIPPET: profilingAspect_struts2 --> * * * <!-- START SNIPPET: profilingAspect_xwork --> * * XWork2 profiling aspects involves the following :- * <ul> * <ul> * <li>creation of DefaultActionProxy * <ul> * <li>creation of DefaultActionInvocation * <ul> * <li>creation of Action * </ul> * </ul> * <li>execution of DefaultActionProxy * <ul> * <li>invocation of DefaultActionInvocation * <ul> * <li>invocation of Interceptors * <li>invocation of Action * <li>invocation of PreResultListener * <li>invocation of Result * </ul> * </ul> * </ul> * </ul> * * <!-- END SNIPPET: profilingAspect_xwork --> * * * <!-- START SNIPPET: activationDescription --> * * Activating / Deactivating of the profiling feature could be done through:- * * <!-- END SNIPPET: activationDescription --> * * <p/> * * System properties:- <p/> * <pre> * <!-- START SNIPPET: activationThroughSystemProperty --> * * -Dxwork.profile.activate=true * * <!-- END SNIPPET: activationThroughSystemProperty --> * </pre> * * <!-- START SNIPPET: activationThroughSystemPropertyDescription --> * * This could be done in the container startup script eg. CATALINA_OPTS in catalina.sh * (tomcat) or using "java -Dxwork.profile.activate=true -jar start.jar" (jetty) * * <!-- END SNIPPET: activationThroughSystemPropertyDescription --> * * <p/> * Code :- <p/> * <pre> * <!-- START SNIPPET: activationThroughCode --> * * UtilTimerStack.setActivate(true); * * <!-- END SNIPPET: activationThroughCode --> * </pre> * * * * <!-- START SNIPPET: activationThroughCodeDescription --> * * This could be done in a static block, in a Spring bean with lazy-init="false", * in a Servlet with init-on-startup as some numeric value, in a Filter or * Listener's init method etc. * * <!-- END SNIPPET: activationThroughCodeDescription --> * * <p/> * Parameter:- * * <pre> * <!-- START SNIPPET: activationThroughParameter --> * * <action ... > * ... * <interceptor-ref name="profiling"> * <param name="profilingKey">profiling</param> * </interceptor-ref> * ... * </action> * * or * * <action .... > * ... * <interceptor-ref name="profiling" /> * ... * </action> * * through url * * http://host:port/context/namespace/someAction.action?profiling=true * * through code * * ActionContext.getContext().getParameters().put("profiling", "true); * * <!-- END SNIPPET: activationThroughParameter --> * </pre> * * * <!-- START SNIPPET: activationThroughParameterDescription --> * * To use profiling activation through parameter, one will need to pass in through * the 'profiling' parameter (which is the default) and could be changed through * the param tag in the interceptor-ref. * * <!-- END SNIPPET: activationThroughParameterDescription --> * * <p/> * Warning:<p/> * <!-- START SNIPPET: activationThroughParameterWarning --> * * Profiling activation through a parameter requires the following: * * <ul> * <li>Profiling interceptor in interceptor stack * <li>dev mode on (struts.devMode=true in struts.properties) * </ul> * * <!-- END SNIPPET: activationThroughParameterWarning --> * * <p/> * * <!-- START SNIPPET: filteringDescription --> * * One could filter out the profile logging by having a System property as follows. With this * 'xwork.profile.mintime' property, one could only log profile information when its execution time * exceed those specified in 'xwork.profile.mintime' system property. If no such property is specified, * it will be assumed to be 0, hence all profile information will be logged. * * <!-- END SNIPPET: filteringDescription --> * * <pre> * <!-- START SNIPPET: filteringCode --> * * -Dxwork.profile.mintime=10000 * * <!-- END SNIPPET: filteringCode --> * </pre> * * <!-- START SNIPPET: methodDescription --> * * One could extend the profiling feature provided by Struts2 in their web application as well. * * <!-- END SNIPPET: methodDescription --> * * <pre> * <!-- START SNIPPET: method1 --> * * String logMessage = "Log message"; * UtilTimerStack.push(logMessage); * try { * // do some code * } * finally { * UtilTimerStack.pop(logMessage); // this needs to be the same text as above * } * * <!-- END SNIPPET: method1 --> * </pre> * * or * * <pre> * <!-- START SNIPPET: method2 --> * * String result = UtilTimerStack.profile("purchaseItem: ", * new UtilTimerStack.ProfilingBlock<String>() { * public String doProfiling() { * // do some code * return "Ok"; * } * }); * * <!-- END SNIPPET: method2 --> * </pre> * * * <!-- START SNIPPET: profileLogFile --> * * Profiled result is logged using commons-logging under the logger named * 'com.opensymphony.xwork2.util.profiling.UtilTimerStack'. Depending on the underlying logging implementation * say if it is Log4j, one could direct the log to appear in a different file, being emailed to someone or have * it stored in the db. * * <!-- END SNIPPET: profileLogFile --> * * @version $Date: 2009-12-27 19:00:13 +0100 (Sun, 27 Dec 2009) $ $Id: UtilTimerStack.java 894087 2009-12-27 18:00:13Z martinc $ */ public class UtilTimerStack { // A reference to the current ProfilingTimerBean protected static ThreadLocal<ProfilingTimerBean> current = new ThreadLocal Other Struts examples (source code examples)Here is a short list of links related to this Struts UtilTimerStack.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.