KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > logicalcobwebs > dbscript > ScriptRunner


1 /*
2  * This software is released under a licence similar to the Apache Software Licence.
3  * See org.logicalcobwebs.proxool.package.html for details.
4  * The latest version is available at http://proxool.sourceforge.net
5  */

6 package org.logicalcobwebs.dbscript;
7
8 import org.apache.commons.logging.Log;
9 import org.apache.commons.logging.LogFactory;
10 import org.logicalcobwebs.proxool.ProxoolException;
11
12 import java.sql.SQLException JavaDoc;
13
14 /**
15  * Run a {@link Script script}.
16  *
17  * @version $Revision: 1.12 $, $Date: 2006/01/18 14:40:05 $
18  * @author Bill Horsman (bill@logicalcobwebs.co.uk)
19  * @author $Author: billhorsman $ (current maintainer)
20  * @since Proxool 0.5
21  */

22 public class ScriptRunner {
23
24     private static final Log LOG = LogFactory.getLog(ScriptRunner.class);
25
26     /**
27      * Run the script.
28      *
29      * @param script to run
30      * @param adapter so we know where to connections from
31      * @throws SQLException if anything goes wrong
32      */

33     protected static final void runScript(Script script, ConnectionAdapterIF adapter, CommandFilterIF commandFilter) throws SQLException JavaDoc, ProxoolException {
34         adapter.setup(script.getDriver(), script.getUrl(), script.getInfo());
35
36         try {
37             Thread.sleep(5000);
38         } catch (InterruptedException JavaDoc e) {
39             e.printStackTrace(); //To change body of catch statement use Options | File Templates.
40
}
41
42         Command[] commands = script.getCommands();
43         for (int i = 0; i < commands.length; i++) {
44             Command command = commands[i];
45             long start = System.currentTimeMillis();
46
47             // Execute the SQL
48
Commander[] commanders = new Commander[command.getLoad()];
49             for (int load = 0; load < command.getLoad(); load++) {
50                 commanders[load] = new Commander(adapter, command, commandFilter);
51                 Thread JavaDoc t = new Thread JavaDoc(commanders[load]);
52                 t.setName(script.getName() + "." + command.getName() + "." + load);
53                 t.start();
54             }
55
56             while (true) {
57                 try {
58                     Thread.sleep(1000);
59                 } catch (InterruptedException JavaDoc e) {
60                     LOG.error("Awoken from sleep", e);
61                 }
62
63                 int remaining = command.getLoad();
64                 for (int load = 0; load < command.getLoad(); load++) {
65                     if (commanders[load].isFinished()) {
66                         remaining--;
67                     }
68                 }
69
70                 if (remaining > 0) {
71                     // LOG.debug("Waiting for " + remaining + " threads to complete.");
72
} else {
73                     break;
74                 }
75             }
76
77             long elapsed = System.currentTimeMillis() - start;
78             int count = command.getLoad() * command.getLoops();
79             double lap = (double) elapsed / (double) count;
80             if (count > 1) {
81                 LOG.info(adapter.getName() + ":" + command.getName() + " ran " + count + " commands in " + elapsed + " milliseconds (avg." + lap + ")");
82             } else {
83                 LOG.debug(adapter.getName() + ":" + command.getName() + " ran in " + elapsed + " milliseconds");
84             }
85
86         }
87     }
88
89 }
90
91 /*
92  Revision history:
93  $Log: ScriptRunner.java,v $
94  Revision 1.12 2006/01/18 14:40:05 billhorsman
95  Unbundled Jakarta's Commons Logging.
96
97  Revision 1.11 2003/03/03 11:12:03 billhorsman
98  fixed licence
99
100  Revision 1.10 2003/02/19 15:14:21 billhorsman
101  fixed copyright (copy and paste error,
102  not copyright change)
103
104  Revision 1.9 2003/02/06 17:41:02 billhorsman
105  now uses imported logging
106
107  Revision 1.8 2003/01/17 00:38:12 billhorsman
108  wide ranging changes to clarify use of alias and url -
109  this has led to some signature changes (new exceptions
110  thrown) on the ProxoolFacade API.
111
112  Revision 1.7 2002/11/09 16:00:34 billhorsman
113  fix doc
114
115  Revision 1.6 2002/11/09 14:45:07 billhorsman
116  now threaded and better exception handling
117
118  Revision 1.5 2002/11/06 21:07:14 billhorsman
119  Support for CommandFilterIF
120
121  Revision 1.4 2002/11/02 14:22:16 billhorsman
122  Documentation
123
124  Revision 1.3 2002/11/02 13:57:34 billhorsman
125  checkstyle
126
127  Revision 1.2 2002/11/02 12:45:54 billhorsman
128  improved debug
129
130  Revision 1.1 2002/11/02 11:29:53 billhorsman
131  new script runner for testing
132
133 */

134
Popular Tags