KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jmeter > control > TransactionController


1 // $Header: /home/cvs/jakarta-jmeter/src/components/org/apache/jmeter/control/TransactionController.java,v 1.4 2004/03/30 18:08:09 sebb Exp $
2
/*
3  * Copyright 2004 The Apache Software Foundation.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17 */

18
19 package org.apache.jmeter.control;
20
21 import java.io.Serializable JavaDoc;
22
23 import org.apache.jmeter.samplers.SampleEvent;
24 import org.apache.jmeter.samplers.SampleResult;
25 import org.apache.jmeter.samplers.Sampler;
26 import org.apache.jmeter.threads.JMeterContext;
27 import org.apache.jmeter.threads.JMeterThread;
28 import org.apache.jmeter.threads.JMeterVariables;
29 import org.apache.jmeter.threads.ListenerNotifier;
30 import org.apache.jmeter.threads.SamplePackage;
31 import org.apache.jorphan.logging.LoggingManager;
32 import org.apache.log.Logger;
33
34 /**
35  * Transaction Controller to measure transaction times
36  *
37  * @version $Revision: 1.4 $
38  */

39 public class TransactionController
40     extends GenericController
41     implements Controller, Serializable JavaDoc
42 {
43     protected static final Logger log = LoggingManager.getLoggerForClass();
44
45     transient private String JavaDoc threadName;
46     transient private ListenerNotifier lnf;
47     transient private JMeterContext threadContext;
48     transient private JMeterVariables threadVars;
49     transient private SampleResult res;
50     
51     /**
52      * Creates a Transaction Controller
53      */

54     public TransactionController()
55     {
56         threadName = Thread.currentThread().getName();
57         lnf = new ListenerNotifier();
58     }
59
60     private void log_debug(String JavaDoc s)
61     {
62         String JavaDoc n = this.getName();
63         log.debug(threadName + " " + n + " "+ s);
64     }
65     
66     private int calls;
67     /**
68      * @see org.apache.jmeter.control.Controller#next()
69      */

70     public Sampler next()
71     {
72         Sampler returnValue = null;
73         if (isFirst()) // must be the start of the subtree
74
{
75             log_debug("+++++++++++++++++++++++++++++");
76             calls = 0;
77             res = new SampleResult();
78             res.sampleStart();
79         }
80         
81         calls++;
82         
83         returnValue = super.next();
84
85         if (returnValue == null) // Must be the end of the controller
86
{
87             log_debug("-----------------------------"+calls);
88             if (res == null){
89                 log_debug("already called");
90             } else {
91                 res.sampleEnd();
92                 res.setSuccessful(true);
93                 res.setSampleLabel(getName());
94                 res.setResponseCode("200");
95                 res.setResponseMessage("Called: "+calls);
96                 res.setThreadName(threadName);
97             
98                 //TODO could these be done earlier (or just once?)
99
threadContext = getThreadContext();
100                 threadVars = threadContext.getVariables();
101                 
102                 SamplePackage pack = (SamplePackage)
103                               threadVars.getObject(JMeterThread.PACKAGE_OBJECT);
104                 if (pack == null)
105                 {
106                     log.warn("Could not fetch SamplePackage");
107                 }
108                 else
109                 {
110                     lnf.notifyListeners(new SampleEvent(res,getName()),pack.getSampleListeners());
111                 }
112                 res=null;
113             }
114         }
115
116         return returnValue;
117     }
118 }
119
Popular Tags