001: /**
002: * Library name : Primrose - A Java Database Connection Pool.
003: * Published by Ben Keeping, http://primrose.org.uk .
004: * Copyright (C) 2004 Ben Keeping, primrose.org.uk
005: * Email: Use "Contact Us Form" on website
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */package uk.org.primrose.test;
021:
022: import uk.org.primrose.pool.core.PoolLoader;
023: import uk.org.primrose.vendor.standalone.*;
024: import javax.sql.*;
025: import java.sql.*;
026: import javax.naming.*;
027: import java.util.List;
028:
029: /**
030: *
031: * This class demonstrates how you may load primrose in a standalone
032: * Java application.
033: *
034: * It first loads a web console, to provide an interface for viewing pool activity
035: * and stopping/restarting pools on the fly.
036: *
037: * Then it loads some pools from a defined pool coniguration file.
038: *
039: * Note that in your application, you do not have to load the web console -
040: * it is entirely optional.
041: *
042: */
043: public class TestPrimroseStandalone {
044: public static void main(String args[]) throws Exception, Throwable {
045: if (args.length != 6) {
046: System.out
047: .println("java uk.org.primrose.test.TestPrimroseStandalone <config file> <webconsole_username> <webconsole_password> <webconsole_port> <log file name> <log level>");
048: System.out
049: .println("EG:\njava uk.org.primrose.test.TestPrimroseStandalone /usr/primrose/conf/primrose.conf admin admin 8090 /var/log/primrose_console.log verbose,info,warn,error,crisis");
050:
051: System.exit(0);
052: }
053:
054: // Start the web console
055: PrimroseWebConsoleLoader.load(args[1], args[2], Integer
056: .parseInt(args[3]), args[4], args[5]);
057:
058: // Load the pools
059: List<String> loadedPoolNames = PrimroseLoader.load(args[0],
060: true);
061:
062: // Get a pool name from the first loaded pool
063: // to run our test with
064: String poolName = loadedPoolNames.get(0);
065:
066: new TestPrimroseStandalone().runTest(poolName);
067:
068: //Thread.sleep(10000);
069:
070: //System.exit(0);
071: }
072:
073: public void runTest(String poolName) throws Throwable {
074: int i = 0;
075: while (i++ < 20) {
076: new RunThread(poolName).run();
077: }
078: /*
079: int connectionsToGet = 30;
080: int cycles = 10;
081:
082: int [] sleeps = new int[5];
083: sleeps[0] = 100;
084: sleeps[1] = 200;
085: sleeps[2] = 50;
086: sleeps[3] = 10000;
087: sleeps[4] = 10;
088: for (int k = 0; k < cycles; k++) {
089: for (int i = 0; i < sleeps.length; i++) {
090: int j = 0;
091: while (j++ < connectionsToGet) {
092: new RunThread(poolName).start();
093: }
094: Thread.sleep(sleeps[i]);
095: System.out.println("Sleep : " +sleeps[i]);
096: }
097: }
098: */
099: }
100:
101: /**
102: *
103: * Perform a test on the newly loaded pool
104: *
105: */
106: class RunThread extends Thread {
107: String poolName = null;
108:
109: public RunThread(String poolName) {
110: this .poolName = poolName;
111: }
112:
113: public void run() {
114: try {
115: //System.out.println("start ...");
116: Context ctx = new InitialContext();
117: DataSource ds = (DataSource) ctx
118: .lookup("java:comp/env/" + poolName);
119: long now = System.currentTimeMillis();
120: Connection c = ds.getConnection();
121: System.out.println("got connection " + c + " in "
122: + (System.currentTimeMillis() - now)
123: + "ms from pool " + poolName);
124: Statement s = c.createStatement();
125: ResultSet rs = s.executeQuery("select 1 from test");
126: //Thread.sleep(100);
127: while (rs.next()) {
128: rs.getString(1);
129: }
130: rs.close();
131: s.close();
132: //c.close();;
133: } catch (Exception e) {
134: e.printStackTrace();
135: }
136: }
137:
138: }
139: }
|