Source Code Cross Referenced for AbstractAsynchronousLoanBrokerTestCase.java in  » ESB » mule » org » mule » example » loanbroker » tests » 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 » ESB » mule » org.mule.example.loanbroker.tests 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * $Id: AbstractAsynchronousLoanBrokerTestCase.java 11393 2008-03-17 15:16:00Z dirk.olmes $
003:         * --------------------------------------------------------------------------------------
004:         * Copyright (c) MuleSource, Inc.  All rights reserved.  http://www.mulesource.com
005:         *
006:         * The software in this package is published under the terms of the CPAL v1.0
007:         * license, a copy of which has been included with this distribution in the
008:         * LICENSE.txt file.
009:         */
010:
011:        package org.mule.example.loanbroker.tests;
012:
013:        import org.mule.api.MuleException;
014:        import org.mule.api.MuleMessage;
015:        import org.mule.example.loanbroker.messages.Customer;
016:        import org.mule.example.loanbroker.messages.CustomerQuoteRequest;
017:        import org.mule.example.loanbroker.messages.LoanQuote;
018:        import org.mule.module.client.MuleClient;
019:        import org.mule.transport.NullPayload;
020:        import org.mule.util.ExceptionHolder;
021:        import org.mule.util.StringMessageUtils;
022:
023:        import java.beans.ExceptionListener;
024:
025:        import edu.emory.mathcs.backport.java.util.concurrent.CountDownLatch;
026:
027:        import org.apache.commons.lang.time.StopWatch;
028:
029:        /**
030:         * Tests the Loan Broker application asynchronously.  Note that a simple thread delay is used to wait for the
031:         * incoming responses to arrive.  This may or may not be sufficient depending on external factors (processor
032:         * speed, logging detail, etc.).  To make the tests reliable, a more accurate mechanism should be employed
033:         * (notifications, thread-safe counter, etc.)
034:         */
035:        public abstract class AbstractAsynchronousLoanBrokerTestCase extends
036:                AbstractLoanBrokerTestCase {
037:            @Override
038:            protected int getNumberOfRequests() {
039:                return 100;
040:            }
041:
042:            /** Milliseconds to wait after sending each message in order for the thread to "catch up" with the test. */
043:            protected int getDelay() {
044:                return 10000;
045:            }
046:
047:            protected int getWarmUpMessages() {
048:                return 50;
049:            }
050:
051:            @Override
052:            public void testSingleLoanRequest() throws Exception {
053:                MuleClient client = new MuleClient();
054:                Customer c = new Customer("Ross Mason", 1234);
055:                CustomerQuoteRequest request = new CustomerQuoteRequest(c,
056:                        100000, 48);
057:                // Send asynchronous request
058:                client.dispatch("CustomerRequests", request, null);
059:
060:                // Wait for asynchronous response
061:                MuleMessage result = client.request("CustomerResponses",
062:                        getDelay());
063:                assertNotNull("Result is null", result);
064:                assertFalse("Result is null",
065:                        result.getPayload() instanceof  NullPayload);
066:                assertTrue("Result should be LoanQuote but is "
067:                        + result.getPayload().getClass().getName(), result
068:                        .getPayload() instanceof  LoanQuote);
069:                LoanQuote quote = (LoanQuote) result.getPayload();
070:                assertTrue(quote.getInterestRate() > 0);
071:            }
072:
073:            @Override
074:            public void testLotsOfLoanRequests() throws Exception {
075:                final MuleClient client = new MuleClient();
076:                Customer c = new Customer("Ross Mason", 1234);
077:                CustomerQuoteRequest[] requests = new CustomerQuoteRequest[3];
078:                requests[0] = new CustomerQuoteRequest(c, 100000, 48);
079:                requests[1] = new CustomerQuoteRequest(c, 1000, 12);
080:                requests[2] = new CustomerQuoteRequest(c, 10, 24);
081:
082:                final StopWatch stopWatch = new StopWatch();
083:
084:                final int numRequests = getNumberOfRequests()
085:                        + getWarmUpMessages();
086:                int i = 0;
087:
088:                int numberOfThreads = 1;
089:
090:                CountDownLatch latch = new CountDownLatch(numberOfThreads);
091:                ExceptionHolder exceptionHolder = new ExceptionHolder();
092:                try {
093:                    for (int x = 0; x < numberOfThreads; x++) {
094:                        Thread thread = new Thread(new ClientReceiver(latch,
095:                                numRequests / numberOfThreads, exceptionHolder));
096:                        thread.start();
097:                    }
098:
099:                    for (i = 0; i < numRequests; i++) {
100:                        if (i == getWarmUpMessages()) {
101:                            stopWatch.start();
102:                        }
103:                        client.dispatch("CustomerRequests", requests[i % 3],
104:                                null);
105:                    }
106:                } finally {
107:                    latch.await();
108:                    stopWatch.stop();
109:                    System.out.println("Total running time was: "
110:                            + stopWatch.getTime() + "ms");
111:                    System.out.println("Requests processed was: " + i);
112:                    int mps = (int) (numRequests / ((double) stopWatch
113:                            .getTime() / (double) 1000));
114:                    System.out.println("Msg/sec: " + mps + " (warm up msgs = "
115:                            + getWarmUpMessages() + ")");
116:                    if (exceptionHolder.isExceptionThrown()) {
117:                        exceptionHolder.print();
118:                        fail("Exceptions thrown during async processing");
119:                    }
120:                }
121:            }
122:
123:            private class ClientReceiver implements  Runnable {
124:
125:                private CountDownLatch latch;
126:                private int numberOfRequests;
127:                private ExceptionListener exListener;
128:
129:                public ClientReceiver(CountDownLatch latch,
130:                        int numberOfRequests, ExceptionListener exListener) {
131:                    this .latch = latch;
132:                    this .numberOfRequests = numberOfRequests;
133:                    this .exListener = exListener;
134:                }
135:
136:                public void run() {
137:                    int i = 0;
138:                    try {
139:                        MuleClient client = new MuleClient();
140:                        MuleMessage result = null;
141:                        for (i = 0; i < numberOfRequests; i++) {
142:                            try {
143:                                result = client.request("CustomerResponses",
144:                                        getDelay());
145:                            } catch (MuleException e) {
146:                                exListener.exceptionThrown(e);
147:                                break;
148:                            }
149:                            //System.out.println("Received: " + i);
150:                            assertNotNull("Result is null", result);
151:                            assertFalse("Result is null",
152:                                    result.getPayload() instanceof  NullPayload);
153:                            assertTrue("Result should be LoanQuote but is "
154:                                    + result.getPayload().getClass().getName(),
155:                                    result.getPayload() instanceof  LoanQuote);
156:                            LoanQuote quote = (LoanQuote) result.getPayload();
157:                            assertTrue(quote.getInterestRate() > 0);
158:                        }
159:                    } catch (Throwable e) {
160:                        //e.printStackTrace();
161:                        System.out.println(StringMessageUtils
162:                                .getBoilerPlate("Processed Messages=" + i));
163:                        if (e instanceof  Error) {
164:                            //throw (Error)e;
165:                            exListener.exceptionThrown(new Exception(e));
166:                        } else {
167:                            exListener.exceptionThrown((Exception) e);
168:                        }
169:                    } finally {
170:                        latch.countDown();
171:                    }
172:                }
173:            }
174:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.