1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
package org.mule.samples.voipservice.client; |
12 | |
|
13 | |
import org.mule.MuleManager; |
14 | |
import org.mule.config.builders.MuleXmlConfigurationBuilder; |
15 | |
import org.mule.extras.client.MuleClient; |
16 | |
import org.mule.impl.MuleMessage; |
17 | |
import org.mule.samples.voipservice.LocaleMessage; |
18 | |
import org.mule.samples.voipservice.to.CreditCardTO; |
19 | |
import org.mule.samples.voipservice.to.CreditProfileTO; |
20 | |
import org.mule.samples.voipservice.to.CustomerTO; |
21 | |
import org.mule.samples.voipservice.to.ServiceParamTO; |
22 | |
import org.mule.umo.UMOException; |
23 | |
import org.mule.umo.UMOMessage; |
24 | |
import org.mule.util.StringMessageUtils; |
25 | |
|
26 | |
import java.io.IOException; |
27 | |
|
28 | |
import org.apache.commons.logging.Log; |
29 | |
import org.apache.commons.logging.LogFactory; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
public class VoipConsumer |
35 | |
{ |
36 | |
|
37 | 0 | protected static transient Log logger = LogFactory.getLog(VoipConsumer.class); |
38 | |
|
39 | 0 | private MuleClient muleClient = null; |
40 | |
|
41 | |
public VoipConsumer() throws UMOException |
42 | 0 | { |
43 | 0 | init(); |
44 | 0 | } |
45 | |
|
46 | |
public VoipConsumer(String config) throws UMOException |
47 | 0 | { |
48 | |
|
49 | 0 | MuleXmlConfigurationBuilder builder = new MuleXmlConfigurationBuilder(); |
50 | 0 | builder.configure(config); |
51 | 0 | init(); |
52 | 0 | } |
53 | |
|
54 | |
private void init() throws UMOException |
55 | |
{ |
56 | 0 | muleClient = new MuleClient(); |
57 | 0 | } |
58 | |
|
59 | |
public void close() |
60 | |
{ |
61 | 0 | MuleManager.getInstance().dispose(); |
62 | 0 | } |
63 | |
|
64 | |
public void requestSend(String endpoint) throws Exception |
65 | |
{ |
66 | |
UMOMessage result; |
67 | 0 | CustomerTO customerTO = CustomerTO.getRandomCustomer(); |
68 | 0 | CreditCardTO creditCardTO = CreditCardTO.getRandomCreditCard(); |
69 | 0 | result = muleClient.send(endpoint, new ServiceParamTO(customerTO, creditCardTO), null); |
70 | 0 | CreditProfileTO creditProfileTO = (CreditProfileTO)((MuleMessage)result).getPayload(); |
71 | 0 | boolean valid = creditProfileTO.isValid(); |
72 | 0 | logger.info("SyncVoipConsumer.requestSend. valid = " + valid); |
73 | 0 | } |
74 | |
|
75 | |
public static void main(String[] args) |
76 | |
{ |
77 | 0 | VoipConsumer voipConsumer = null; |
78 | 0 | int response = 0; |
79 | |
|
80 | |
try |
81 | |
{ |
82 | 0 | voipConsumer = new VoipConsumer("voip-broker-sync-config.xml"); |
83 | |
|
84 | 0 | String msg = LocaleMessage.getWelcomeMessage(); |
85 | |
|
86 | 0 | System.out.println(StringMessageUtils.getBoilerPlate(msg, '*', 70)); |
87 | |
|
88 | 0 | while (response != 'q') |
89 | |
{ |
90 | 0 | System.out.println("\n" + LocaleMessage.getMenuOption1()); |
91 | 0 | System.out.println(LocaleMessage.getMenuOptionQuit()); |
92 | 0 | System.out.println("\n" + LocaleMessage.getMenuPromptMessage()); |
93 | |
|
94 | 0 | response = getSelection(); |
95 | 0 | if (response == '1') |
96 | |
{ |
97 | 0 | logger.info("Sending Request..."); |
98 | 0 | voipConsumer.requestSend("vm://VoipBrokerRequests"); |
99 | 0 | logger.info("Request Completed."); |
100 | |
} |
101 | 0 | else if (response == 'q') |
102 | |
{ |
103 | 0 | System.out.println(LocaleMessage.getGoodbyeMessage()); |
104 | 0 | System.exit(0); |
105 | |
} |
106 | |
else |
107 | |
{ |
108 | 0 | System.out.println(LocaleMessage.getMenuErrorMessage()); |
109 | |
} |
110 | |
} |
111 | |
|
112 | |
} |
113 | 0 | catch (Exception e) |
114 | |
{ |
115 | 0 | System.err.println(e.getMessage()); |
116 | 0 | e.printStackTrace(System.err); |
117 | 0 | System.exit(1); |
118 | 0 | } |
119 | 0 | } |
120 | |
|
121 | |
private static int getSelection() throws IOException |
122 | |
{ |
123 | 0 | byte[] buf = new byte[16]; |
124 | 0 | System.in.read(buf); |
125 | 0 | return buf[0]; |
126 | |
} |
127 | |
|
128 | |
} |