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