1 10 11 package org.mule.samples.loanbroker.esb; 12 13 import java.io.IOException ; 14 15 import org.activemq.broker.BrokerContainer; 16 import org.activemq.broker.impl.BrokerContainerImpl; 17 import org.mule.MuleManager; 18 import org.mule.config.builders.MuleXmlConfigurationBuilder; 19 import org.mule.extras.client.MuleClient; 20 import org.mule.samples.loanbroker.esb.message.Customer; 21 import org.mule.samples.loanbroker.esb.message.CustomerQuoteRequest; 22 import org.mule.umo.UMOMessage; 23 24 27 28 public class Main 29 { 30 private MuleClient client = null; 31 private BrokerContainer msgBroker = null; 32 33 public Main(String config) throws Exception 34 { 35 msgBroker = new BrokerContainerImpl("ActiveMQ"); 37 msgBroker.addConnector("tcp://localhost:61616"); 38 msgBroker.start(); 39 40 MuleXmlConfigurationBuilder builder = new MuleXmlConfigurationBuilder(); 41 builder.configure(config, null); 42 client = new MuleClient(); 43 } 44 45 public void close() throws Exception 46 { 47 MuleManager.getInstance().dispose(); 48 if (msgBroker != null) 49 { 50 msgBroker.stop(); 51 } 52 } 53 54 private static double getRandomAmount() 55 { 56 return Math.round(Math.random() * 18000); 57 } 58 59 private static int getRandomDuration() 60 { 61 return new Double (Math.random() * 60).intValue(); 62 } 63 64 public UMOMessage request(CustomerQuoteRequest request) throws Exception 65 { 66 return client.send("vm://loan.broker.requests", request, null); 67 } 68 69 public static void main(String [] args) 70 { 71 Main loanConsumer = null; 72 int response = 0; 73 try 74 { 75 System.out.println("******************" 76 + "\nWelcome to the Mule Loan Broker ESB example. This example demonstrates using JMS, Web Services,\n Http/Rest and EJBs using an ESB architecture." 77 + "\nFor more information see http://mule.mulesource.org/LoanBroker." 78 + "\n\nThe example demonstrates integrating EJB applications in 2 ways -" 79 + "\n 1. Calling out to a remote EJB using a Mule Endpoint." 80 + "\n 2. Managing an EJB as a Mule component." 81 + "\n\nBoth have the same behavior but the second method allows a remote EJB to be used as if it were\n a local Mule component, thereby enabling tighter integration." 82 + "\n\nPlease select [1], [2] or [q]uit" + "\n******************"); 83 84 response = getSelection(); 85 if (response == '1') 86 { 87 System.out.println("Loading 'Ejb via an Endpoint' version"); 88 loanConsumer = new Main("loan-broker-esb-mule-config.xml"); 89 } 90 else if (response == 'q') 91 { 92 System.out.println("Bye"); 93 System.exit(0); 94 } 95 else 96 { 97 System.out.println("Loading 'Managed Ejb Component' version"); 98 loanConsumer = new Main("loan-broker-esb-mule-config-with-ejb-container.xml"); 99 } 100 101 while (response != 'q') 102 { 103 System.out.println("\n[1] make a loan request"); 104 System.out.println("[q] quit"); 105 System.out.println("\nPlease make your selection: "); 106 107 response = getSelection(); 108 if (response == '1') 109 { 110 CustomerQuoteRequest request = getRequestFromUser(); 111 UMOMessage result = loanConsumer.request(request); 112 if (result == null) 113 { 114 System.out.println("A result was not received, an error must have occurred. Check the logs."); 115 } 116 else 117 { 118 System.out.println("Loan Consumer received a Quote: " + result.getPayload()); 119 } 120 } 121 else if (response == 'q') 122 { 123 System.out.println("Exiting now"); 124 loanConsumer.close(); 125 System.exit(0); 126 } 127 else 128 { 129 System.out.println("That response is not recognised, try again:"); 130 } 131 } 132 133 } 134 catch (Exception e) 135 { 136 System.err.println(e.getMessage()); 137 e.printStackTrace(System.err); 138 System.exit(1); 139 } 140 } 141 142 private static int getSelection() throws IOException 143 { 144 byte[] buf = new byte[16]; 145 System.in.read(buf); 146 return buf[0]; 147 } 148 149 private static CustomerQuoteRequest getRequestFromUser() throws IOException 150 { 151 byte[] buf = new byte[128]; 152 System.out.println("Enter your name:"); 153 System.in.read(buf); 154 String name = new String (buf).trim(); 155 System.out.println("Enter loan Amount:"); 156 buf = new byte[16]; 157 System.in.read(buf); 158 String amount = new String (buf).trim(); 159 System.out.println("Enter loan Duration in months:"); 160 buf = new byte[16]; 161 System.in.read(buf); 162 String duration = new String (buf).trim(); 163 164 int d = 0; 165 try 166 { 167 d = Integer.parseInt(duration); 168 } 169 catch (NumberFormatException e) 170 { 171 System.out.println("Failed to parse duration: " + duration + ". Using random default"); 172 d = getRandomDuration(); 173 } 174 175 double a = 0; 176 try 177 { 178 a = Double.valueOf(amount).doubleValue(); 179 } 180 catch (NumberFormatException e) 181 { 182 System.out.println("Failed to parse amount: " + amount + ". Using random default"); 183 a = getRandomAmount(); 184 } 185 186 Customer c = new Customer(name, getRandomSsn()); 187 CustomerQuoteRequest request = new CustomerQuoteRequest(c, a, d); 188 return request; 189 } 190 191 private static int getRandomSsn() 192 { 193 return new Double (Math.random() * 6000).intValue(); 194 } 195 } 196 | Popular Tags |