1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
package org.mule.example.loanbroker; |
8 | |
|
9 | |
import org.mule.api.MuleException; |
10 | |
import org.mule.api.MuleMessage; |
11 | |
import org.mule.api.config.ConfigurationBuilder; |
12 | |
import org.mule.config.spring.SpringXmlConfigurationBuilder; |
13 | |
import org.mule.example.loanbroker.messages.Customer; |
14 | |
import org.mule.example.loanbroker.messages.CustomerQuoteRequest; |
15 | |
import org.mule.module.client.MuleClient; |
16 | |
import org.mule.module.client.RemoteDispatcher; |
17 | |
|
18 | |
import java.io.IOException; |
19 | |
import java.util.ArrayList; |
20 | |
import java.util.Iterator; |
21 | |
import java.util.List; |
22 | |
import java.util.Map; |
23 | |
|
24 | |
|
25 | |
|
26 | |
|
27 | |
public abstract class AbstractLoanBrokerApp |
28 | |
{ |
29 | 0 | private List<Customer> customers = new ArrayList<Customer>(); |
30 | 0 | private RemoteDispatcher remoteClient = null; |
31 | |
private String config; |
32 | |
|
33 | |
public AbstractLoanBrokerApp() throws Exception |
34 | 0 | { |
35 | 0 | this.config = null; |
36 | 0 | init(); |
37 | 0 | } |
38 | |
|
39 | |
public AbstractLoanBrokerApp(String config) throws Exception |
40 | 0 | { |
41 | 0 | this.config = config; |
42 | 0 | init(); |
43 | 0 | } |
44 | |
|
45 | |
protected void init() throws Exception |
46 | |
{ |
47 | 0 | MuleClient muleClient = new MuleClient(true); |
48 | 0 | remoteClient = muleClient.getRemoteDispatcher("tcp://localhost:5555"); |
49 | 0 | customers.add(new Customer("Jenson Button", 123)); |
50 | 0 | customers.add(new Customer("Michael Schumacker", 456)); |
51 | 0 | customers.add(new Customer("Juan Pablo Montoya", 789)); |
52 | 0 | customers.add(new Customer("David Colthard", 101)); |
53 | 0 | customers.add(new Customer("Rubens Barrichello", 112)); |
54 | 0 | customers.add(new Customer("Mark Webber", 131)); |
55 | 0 | customers.add(new Customer("Takuma Sato", 415)); |
56 | 0 | customers.add(new Customer("Kimi Raikkonen", 161)); |
57 | 0 | customers.add(new Customer("Ralf Schumacher", 718)); |
58 | 0 | customers.add(new Customer("Jarno Trulli", 192)); |
59 | 0 | } |
60 | |
|
61 | |
protected ConfigurationBuilder getConfigBuilder() throws MuleException |
62 | |
{ |
63 | 0 | return new SpringXmlConfigurationBuilder(config); |
64 | |
} |
65 | |
|
66 | |
protected void dispose() throws Exception |
67 | |
{ |
68 | 0 | remoteClient.dispose(); |
69 | 0 | } |
70 | |
|
71 | |
protected void run(boolean synchronous) throws Exception |
72 | |
{ |
73 | 0 | int response = 0; |
74 | 0 | while (response != 'q') |
75 | |
{ |
76 | 0 | System.out.println("\n" + LocaleMessage.menu()); |
77 | |
|
78 | 0 | response = readCharacter(); |
79 | |
|
80 | 0 | switch (response) |
81 | |
{ |
82 | |
case '1' : |
83 | |
{ |
84 | 0 | CustomerQuoteRequest request = getRequestFromUser(); |
85 | 0 | request(request, synchronous); |
86 | 0 | break; |
87 | |
} |
88 | |
|
89 | |
case '2' : |
90 | |
{ |
91 | 0 | sendRandomRequests(100, synchronous); |
92 | 0 | break; |
93 | |
} |
94 | |
|
95 | |
case '3' : |
96 | |
{ |
97 | 0 | System.out.println(LocaleMessage.menuOptionNumberOfRequests()); |
98 | 0 | int number = readInt(); |
99 | 0 | if (number < 1) |
100 | |
{ |
101 | 0 | System.out.println(LocaleMessage.menuErrorNumberOfRequests()); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 0 | sendRandomRequests(number, synchronous); |
106 | |
} |
107 | 0 | break; |
108 | |
} |
109 | |
|
110 | |
case 'q' : |
111 | |
{ |
112 | 0 | System.out.println(LocaleMessage.exiting()); |
113 | 0 | dispose(); |
114 | 0 | System.exit(0); |
115 | |
} |
116 | |
|
117 | |
default : |
118 | |
{ |
119 | 0 | System.out.println(LocaleMessage.menuError()); |
120 | |
} |
121 | |
} |
122 | |
} |
123 | 0 | } |
124 | |
|
125 | |
public CustomerQuoteRequest createRequest() |
126 | |
{ |
127 | 0 | int index = new Double(Math.random() * 10).intValue(); |
128 | 0 | Customer c = customers.get(index); |
129 | |
|
130 | 0 | return new CustomerQuoteRequest(c, getRandomAmount(), getRandomDuration()); |
131 | |
} |
132 | |
|
133 | |
protected static CustomerQuoteRequest getRequestFromUser() throws IOException |
134 | |
{ |
135 | 0 | byte[] buf = new byte[128]; |
136 | 0 | System.out.print(LocaleMessage.enterName()); |
137 | 0 | System.in.read(buf); |
138 | 0 | String name = new String(buf).trim(); |
139 | 0 | System.out.print(LocaleMessage.enterLoanAmount()); |
140 | 0 | buf = new byte[16]; |
141 | 0 | System.in.read(buf); |
142 | 0 | String amount = new String(buf).trim(); |
143 | 0 | System.out.print(LocaleMessage.enterLoanDuration()); |
144 | 0 | buf = new byte[16]; |
145 | 0 | System.in.read(buf); |
146 | 0 | String duration = new String(buf).trim(); |
147 | |
|
148 | 0 | int d = 0; |
149 | |
try |
150 | |
{ |
151 | 0 | d = Integer.parseInt(duration); |
152 | |
} |
153 | 0 | catch (NumberFormatException e) |
154 | |
{ |
155 | 0 | System.out.println(LocaleMessage.loanDurationError(duration)); |
156 | 0 | d = getRandomDuration(); |
157 | 0 | } |
158 | |
|
159 | 0 | double a = 0; |
160 | |
try |
161 | |
{ |
162 | 0 | a = Double.valueOf(amount).doubleValue(); |
163 | |
} |
164 | 0 | catch (NumberFormatException e) |
165 | |
{ |
166 | 0 | System.out.println(LocaleMessage.loanAmountError(amount)); |
167 | 0 | a = getRandomAmount(); |
168 | 0 | } |
169 | |
|
170 | 0 | Customer c = new Customer(name, getRandomSsn()); |
171 | 0 | CustomerQuoteRequest request = new CustomerQuoteRequest(c, a, d); |
172 | 0 | return request; |
173 | |
} |
174 | |
|
175 | |
public void request(CustomerQuoteRequest request, boolean sync) throws Exception |
176 | |
{ |
177 | 0 | if (!sync) |
178 | |
{ |
179 | 0 | remoteClient.dispatchRemote("CustomerRequests", request, null); |
180 | 0 | System.out.println(LocaleMessage.sentAsync()); |
181 | |
|
182 | 0 | Thread.sleep(3000); |
183 | |
|
184 | |
} |
185 | |
else |
186 | |
{ |
187 | 0 | MuleMessage result = remoteClient.sendRemote("CustomerRequests", request, null); |
188 | 0 | if (result == null) |
189 | |
{ |
190 | 0 | System.out.println(LocaleMessage.requestError()); |
191 | |
} |
192 | |
else |
193 | |
{ |
194 | 0 | System.out.println(LocaleMessage.requestResponse(result.getPayload())); |
195 | |
} |
196 | |
} |
197 | 0 | } |
198 | |
|
199 | |
public void requestDispatch(int number, String endpoint) throws Exception |
200 | |
{ |
201 | 0 | for (int i = 0; i < number; i++) |
202 | |
{ |
203 | 0 | remoteClient.dispatchRemote(endpoint, createRequest(), null); |
204 | |
} |
205 | 0 | } |
206 | |
|
207 | |
public List<Object> requestSend(int number, String endpoint) throws Exception |
208 | |
{ |
209 | 0 | return requestSend(number, endpoint, null); |
210 | |
} |
211 | |
|
212 | |
public List<Object> requestSend(int number, String endpoint, Map properties) throws Exception |
213 | |
{ |
214 | 0 | List<Object> results = new ArrayList<Object>(number); |
215 | 0 | for (int i = 0; i < number; i++) |
216 | |
{ |
217 | 0 | MuleMessage result = remoteClient.sendRemote(endpoint, createRequest(), properties); |
218 | |
|
219 | 0 | if (result != null) |
220 | |
{ |
221 | 0 | results.add(result.getPayload()); |
222 | |
} |
223 | |
} |
224 | 0 | return results; |
225 | |
} |
226 | |
|
227 | |
protected void sendRandomRequests(int number, boolean synchronous) throws Exception |
228 | |
{ |
229 | 0 | if (synchronous) |
230 | |
{ |
231 | 0 | List<Object> list = requestSend(number, "CustomerRequests"); |
232 | 0 | int i = 1; |
233 | 0 | System.out.println("sendRandomRequests"); |
234 | 0 | for (Iterator<Object> iterator = list.iterator(); iterator.hasNext(); i++) |
235 | |
{ |
236 | 0 | System.out.println("sendRandomRequests results :" + |
237 | |
LocaleMessage.request(i, iterator.next().toString())); |
238 | |
} |
239 | 0 | } |
240 | |
else |
241 | |
{ |
242 | 0 | this.requestDispatch(number, "CustomerRequests"); |
243 | 0 | System.out.println(LocaleMessage.sentAsync()); |
244 | |
} |
245 | 0 | } |
246 | |
|
247 | |
protected static int readCharacter() throws IOException |
248 | |
{ |
249 | 0 | byte[] buf = new byte[16]; |
250 | 0 | System.in.read(buf); |
251 | 0 | return buf[0]; |
252 | |
} |
253 | |
|
254 | |
protected static String readString() throws IOException |
255 | |
{ |
256 | 0 | byte[] buf = new byte[80]; |
257 | 0 | System.in.read(buf); |
258 | 0 | return new String(buf).trim(); |
259 | |
} |
260 | |
|
261 | |
protected static int readInt() throws IOException |
262 | |
{ |
263 | |
try |
264 | |
{ |
265 | 0 | return Integer.parseInt(readString()); |
266 | |
} |
267 | 0 | catch (NumberFormatException nfex) |
268 | |
{ |
269 | 0 | return 0; |
270 | |
} |
271 | |
} |
272 | |
|
273 | |
protected static double getRandomAmount() |
274 | |
{ |
275 | 0 | return Math.round(Math.random() * 18000); |
276 | |
} |
277 | |
|
278 | |
protected static int getRandomDuration() |
279 | |
{ |
280 | 0 | return new Double(Math.random() * 60).intValue(); |
281 | |
} |
282 | |
|
283 | |
protected static int getRandomSsn() |
284 | |
{ |
285 | 0 | return new Double(Math.random() * 6000).intValue(); |
286 | |
} |
287 | |
} |