Source Code Cross Referenced for ResourceLimitingPoolDemo.java in  » Database-ORM » ODAL » com » completex » objective » components » pool » demo » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Database ORM » ODAL » com.completex.objective.components.pool.demo 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Objective Database Abstraction Layer (ODAL)
003:         *  Copyright (c) 2004, The ODAL Development Group
004:         *  All rights reserved.
005:         *  For definition of the ODAL Development Group please refer to LICENCE.txt file
006:         *
007:         *  Distributable under LGPL license.
008:         *  See terms of license at gnu.org.
009:         */package com.completex.objective.components.pool.demo;
010:
011:        import com.completex.objective.components.log.adapter.StdErrorLogAdapter;
012:        import com.completex.objective.components.pool.impl.ResourceLimitingPool;
013:
014:        /**
015:         * @author Gennady Krizhevsky
016:         */
017:        public class ResourceLimitingPoolDemo {
018:
019:            public static void main(String args[]) throws InterruptedException {
020:                ResourceLimitingPoolDemo demo = new ResourceLimitingPoolDemo();
021:                demo.demoShrink();
022:                demo.demoWait();
023:                demo.demoWait02();
024:            }
025:
026:            public void demoShrink() throws InterruptedException {
027:                System.out.println("Enter demoShrink");
028:                try {
029:                    StdErrorLogAdapter logAdapter = StdErrorLogAdapter
030:                            .newLogInstance();
031:                    logAdapter.setTraceEnabled(true);
032:                    ResourceLimitingPool pool = new ResourceLimitingPool(
033:                            new ResourceFactoryImpl(), 10, 10000, logAdapter);
034:                    pool.setShrinkSize(2);
035:                    pool.setLog(logAdapter);
036:                    pool.start();
037:                    int secs = 15;
038:                    int n = 9;
039:                    Object[] resources = new Object[n];
040:                    for (int i = 0; i < n; i++) {
041:                        resources[i] = pool.acquireResource();
042:                    }
043:                    for (int i = 0; i < n; i++) {
044:                        pool.releaseResource(resources[i]);
045:                    }
046:                    System.out.println("Acquired " + n
047:                            + " resources: initial pool size [" + pool.size()
048:                            + "]");
049:                    System.out.println("Wait " + secs + " secs");
050:                    Thread.sleep(secs * 1000);
051:
052:                    System.out.println("Final pool size [" + pool.size() + "]");
053:                    pool.shutdown();
054:                } finally {
055:                    System.out.println("Exit demoShrink");
056:                }
057:
058:            }
059:
060:            public void demoWait() throws InterruptedException {
061:                System.out.println("Enter demoWait");
062:                try {
063:                    StdErrorLogAdapter logAdapter = StdErrorLogAdapter
064:                            .newLogInstance();
065:                    logAdapter.setTraceEnabled(false);
066:                    ResourceLimitingPool pool = new ResourceLimitingPool(
067:                            new ResourceFactoryImpl(), 10, 10000, logAdapter);
068:                    pool.setShrinkSize(2);
069:                    pool.setLog(logAdapter);
070:                    pool.start();
071:
072:                    ConsumerA consumerA = new ConsumerA(pool, 10000);
073:                    ConsumerB consumerB = new ConsumerB(pool, 2000);
074:                    consumerA.start();
075:                    consumerB.start();
076:                    consumerA.join();
077:                    consumerB.join();
078:                    System.out.println("Final pool size [" + pool.size() + "]");
079:
080:                    pool.shutdown();
081:                } finally {
082:                    System.out.println("Exit demoWait");
083:                }
084:
085:            }
086:
087:            public void demoWait02() throws InterruptedException {
088:                System.out
089:                        .println("Enter demoWait02: demonstrates use of timeout:"
090:                                + " with short timeout & pool size =  1 only the 1st consumer will be "
091:                                + "successful, all others will fail");
092:                try {
093:                    StdErrorLogAdapter logAdapter = StdErrorLogAdapter
094:                            .newLogInstance();
095:                    logAdapter.setTraceEnabled(false);
096:                    ResourceLimitingPool pool = new ResourceLimitingPool(
097:                            new ResourceFactoryImpl(), 1, 10000, 1, logAdapter);
098:                    pool.setShrinkSize(2);
099:                    pool.setLog(logAdapter);
100:                    pool.start();
101:
102:                    ConsumerA consumerA = new ConsumerA(pool, 10000);
103:                    ConsumerB consumerB = new ConsumerB(pool, 2000);
104:                    consumerA.start();
105:                    consumerB.start();
106:                    consumerA.join();
107:                    consumerB.join();
108:                    System.out.println("Final pool size [" + pool.size() + "]");
109:
110:                    pool.shutdown();
111:                } finally {
112:                    System.out.println("Exit demoWait02");
113:                }
114:
115:            }
116:
117:            class ConsumerA extends Thread {
118:                ResourceLimitingPool pool;
119:                long timeToWait;
120:
121:                public ConsumerA(ResourceLimitingPool pool, long timeToWait) {
122:                    this .pool = pool;
123:                    this .timeToWait = timeToWait;
124:                }
125:
126:                public void run() {
127:                    int n = 9;
128:                    Object[] resources = new Object[n];
129:                    for (int i = 0; i < n; i++) {
130:                        resources[i] = pool.acquireResource();
131:                        System.out.println("ConsumerA acquired " + i
132:                                + " resources");
133:                    }
134:                    System.out
135:                            .println("ConsumerA acquired " + n + " resources");
136:                    try {
137:                        Thread.sleep(timeToWait);
138:                    } catch (InterruptedException e) {
139:                        // Do nothing
140:                    }
141:                    for (int i = 0; i < n; i++) {
142:                        pool.releaseResource(resources[i]);
143:                        System.out
144:                                .println("ConsumerA released resource; pool size "
145:                                        + pool.size());
146:                    }
147:                }
148:            }
149:
150:            class ConsumerB extends Thread {
151:                ResourceLimitingPool pool;
152:                long timeToWait;
153:
154:                public ConsumerB(ResourceLimitingPool pool, long timeToWait) {
155:                    this .pool = pool;
156:                    this .timeToWait = timeToWait;
157:                }
158:
159:                public void run() {
160:                    try {
161:                        Thread.sleep(timeToWait);
162:                    } catch (InterruptedException e) {
163:                        // Do nothing
164:                    }
165:                    int n = 9;
166:                    Object[] resources = new Object[n];
167:                    for (int i = 0; i < n; i++) {
168:                        resources[i] = pool.acquireResource();
169:                        System.out.println("ConsumerB acquired resource");
170:                    }
171:                }
172:            }
173:
174:            class ResourceFactoryImpl implements 
175:                    com.completex.objective.components.pool.ResourceFactory {
176:                public Object createResource() {
177:                    return new Object();
178:                }
179:
180:                public void destroyResource(Object resource) {
181:                }
182:            }
183:
184:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.