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
27
28
29
30 public abstract class AbstractLoanBrokerApp
31 {
32 private List<Customer> customers = new ArrayList<Customer>();
33 private RemoteDispatcher remoteClient = null;
34 private String config;
35
36 public AbstractLoanBrokerApp() throws Exception
37 {
38 this.config = null;
39 init();
40 }
41
42 public AbstractLoanBrokerApp(String config) throws Exception
43 {
44 this.config = config;
45 init();
46 }
47
48 protected void init() throws Exception
49 {
50 MuleClient muleClient = new MuleClient(true);
51 remoteClient = muleClient.getRemoteDispatcher("tcp://localhost:5555");
52 customers.add(new Customer("Jenson Button", 123));
53 customers.add(new Customer("Michael Schumacker", 456));
54 customers.add(new Customer("Juan Pablo Montoya", 789));
55 customers.add(new Customer("David Colthard", 101));
56 customers.add(new Customer("Rubens Barrichello", 112));
57 customers.add(new Customer("Mark Webber", 131));
58 customers.add(new Customer("Takuma Sato", 415));
59 customers.add(new Customer("Kimi Raikkonen", 161));
60 customers.add(new Customer("Ralf Schumacher", 718));
61 customers.add(new Customer("Jarno Trulli", 192));
62 }
63
64 protected ConfigurationBuilder getConfigBuilder() throws MuleException
65 {
66 return new SpringXmlConfigurationBuilder(config);
67 }
68
69 protected void dispose() throws Exception
70 {
71 remoteClient.dispose();
72 }
73
74 protected void run(boolean synchronous) throws Exception
75 {
76 int response = 0;
77 while (response != 'q')
78 {
79 System.out.println("\n" + LocaleMessage.menu());
80
81 response = readCharacter();
82
83 switch (response)
84 {
85 case '1' :
86 {
87 CustomerQuoteRequest request = getRequestFromUser();
88 request(request, synchronous);
89 break;
90 }
91
92 case '2' :
93 {
94 sendRandomRequests(100, synchronous);
95 break;
96 }
97
98 case '3' :
99 {
100 System.out.println(LocaleMessage.menuOptionNumberOfRequests());
101 int number = readInt();
102 if (number < 1)
103 {
104 System.out.println(LocaleMessage.menuErrorNumberOfRequests());
105 }
106 else
107 {
108 sendRandomRequests(number, synchronous);
109 }
110 break;
111 }
112
113 case 'q' :
114 {
115 System.out.println(LocaleMessage.exiting());
116 dispose();
117 System.exit(0);
118 break;
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 List<Object> results = new ArrayList<Object>(number);
214 for (int i = 0; i < number; i++)
215 {
216 MuleMessage result = remoteClient.sendRemote(endpoint, createRequest(), null);
217
218 if (result != null)
219 {
220 results.add(result.getPayload());
221 }
222 }
223 return results;
224 }
225
226 protected void sendRandomRequests(int number, boolean synchronous) throws Exception
227 {
228 if (synchronous)
229 {
230 List<Object> list = requestSend(number, "CustomerRequests");
231 int i = 1;
232 System.out.println("sendRandomRequests");
233 for (Iterator<Object> iterator = list.iterator(); iterator.hasNext(); i++)
234 {
235 System.out.println("sendRandomRequests results :" +
236 LocaleMessage.request(i, iterator.next().toString()));
237 }
238 }
239 else
240 {
241 this.requestDispatch(number, "CustomerRequests");
242 System.out.println(LocaleMessage.sentAsync());
243 }
244 }
245
246 protected static int readCharacter() throws IOException
247 {
248 byte[] buf = new byte[16];
249 System.in.read(buf);
250 return buf[0];
251 }
252
253 protected static String readString() throws IOException
254 {
255 byte[] buf = new byte[80];
256 System.in.read(buf);
257 return new String(buf).trim();
258 }
259
260 protected static int readInt() throws IOException
261 {
262 try
263 {
264 return Integer.parseInt(readString());
265 }
266 catch (NumberFormatException nfex)
267 {
268 return 0;
269 }
270 }
271
272 protected static double getRandomAmount()
273 {
274 return Math.round(Math.random() * 18000);
275 }
276
277 protected static int getRandomDuration()
278 {
279 return new Double(Math.random() * 60).intValue();
280 }
281
282 protected static int getRandomSsn()
283 {
284 return new Double(Math.random() * 6000).intValue();
285 }
286 }