This topic has not yet been written. The content below is from the topic description.
Transporters sample - proxy The proxy transporter example (found in org.jboss.remoting.samples.transporter.proxy package) shows how can have a TransporterClient sent over the network and called upon. In this example, will have a target pojo, CustomerProcessorImpl which itself creates a TransporterClient to another target pojo, Customer, and return it as response to a method invocation. To start, will look at the initial target pojo, CustomerProcessorImpl. public class CustomerProcessorImpl implements CustomerProcessor { private String locatorURI = "socket://localhost:5401"; /** * Takes the customer passed, and if not null and customer id * is less than 0, will create a new random id and set it. * The customer object returned will be the modified customer * object passed. * * @param customer * @return */ public ICustomer processCustomer(Customer customer) { if (customer != null && customer.getCustomerId() < 0) { customer.setCustomerId(new Random().nextInt(1000)); } ICustomer customerProxy = null; try { TransporterServer server = TransporterServer.createTransporterServer(locatorURI, customer, ICustomer.class.getName()); customerProxy = (ICustomer) TransporterClient.createTransporterClient(locatorURI, ICustomer.class); } catch (Exception e) { e.printStackTrace(); } System.out.println("processed customer with new id of " + customerProxy.getCustomerId()); return customerProxy; } } Notice that the processCustomer() method will take a Customer object and set customer id on it. Then it will create a TransporterServer for that customer instance and also create a TransporterClient for the same instance and return that TransporterClient proxy as the return to the processCustomer() method. Next will look at the Customer class. It is a basic data object in that is really just stores the customer data. public class Customer implements Serializable, ICustomer { private String firstName = null; private String lastName = null; private Address addr = null; private int customerId = -1; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public Address getAddr() { return addr; } public void setAddr(Address addr) { this.addr = addr; } public int getCustomerId() { return customerId; } public void setCustomerId(int customerId) { this.customerId = customerId; } public String toString() { System.out.println("Customer.toString() being called."); StringBuffer buffer = new StringBuffer(); buffer.append("\nCustomer:\n"); buffer.append("customer id: " + customerId + "\n"); buffer.append("first name: " + firstName + "\n"); buffer.append("last name: " + lastName + "\n"); buffer.append("street: " + addr.getStreet() + "\n"); buffer.append("city: " + addr.getCity() + "\n"); buffer.append("state: " + addr.getState() + "\n"); buffer.append("zip: " + addr.getZip() + "\n"); return buffer.toString(); } } Notice the toString() method and how it prints out to the standard out when being called. This will be important when the sample is run later. Now if look at the Server class, will see is a standard setup like have seen in previous samples. public class Server { private String locatorURI = "socket://localhost:5400"; private TransporterServer server = null; public void start() throws Exception { server = TransporterServer.createTransporterServer(locatorURI, new CustomerProcessorImpl(), CustomerProcessor.class.getName()); } public void stop() { if (server != null) { server.stop(); } } public static void main(String[] args) { Server server = new Server(); try { server.start(); Thread.currentThread().sleep(60000); } catch (Exception e) { e.printStackTrace(); } finally { server.stop(); } } } It is creating a TransporterServer for the CustomerProcessImpl upon being started and will wait 60 seconds for invocations. Next is the Client class. public class Client { private String locatorURI = "socket://localhost:5400"; public void makeClientCall() throws Exception { Customer customer = createCustomer(); CustomerProcessor customerProcessor = (CustomerProcessor) TransporterClient.createTransporterClient(locatorURI, CustomerProcessor.class); System.out.println("Customer to be processed: " + customer); ICustomer processedCustomer = customerProcessor.processCustomer(customer); // processedCustomer returned is actually a proxy to the Customer instnace // that lives on the server. So when print it out below, will actually // be calling back to the server to get the string (vi toString() call). // Notice the output of 'Customer.toString() being called.' on the server side. System.out.println("Customer is now: " + processedCustomer); TransporterClient.destroyTransporterClient(customerProcessor); } private Customer createCustomer() { Customer cust = new Customer(); cust.setFirstName("Bob"); cust.setLastName("Smith"); Address addr = new Address(); addr.setStreet("101 Oak Street"); addr.setCity("Atlanta"); addr.setState("GA"); addr.setZip(30249); cust.setAddr(addr); return cust; } public static void main(String[] args) { Client client = new Client(); try { client.makeClientCall(); } catch (Exception e) { e.printStackTrace(); } } } The client class looks similar to the other example seen in that it creates a TransporterClient for the CustomerProcessor and calls on it to process the customer. Will then call on the ICustomer instance returned from the processCustomer() method call and call toString() on it (in the system out call). To run this example, run the Server class and then the Client class. This can be done via ant targets 'run-transporter-proxy-server' and then 'run-transporter-proxy-client'. For example: ant run-transporter-proxy-server ant then: ant run-transporter-proxy-client The output for the client should look similar to: Customer.toString() being called. Customer to be processed: Customer: customer id: -1 first name: Bob last name: Smith street: 101 Oak Street city: Atlanta state: GA zip: 30249 Customer is now: Customer: customer id: 418 first name: Bob last name: Smith street: 101 Oak Street city: Atlanta state: GA zip: 30249 The first line is the print out from calling the Customer's toString() method that was created to be passed to the CustomerProcessor's processCustomer() method. Then the contents of the Customer object before being processed. Then have the print out of the customer after has been processed. Notice that when the ICustomer object instance is printed out the second time, do not see the 'Customer.toString() being called'. This is because that code is no longer being executed in the client vm, but instead is a remote call to the customer instance living on the server (remember, the processCustomer() method returned a TransporterClient proxy to the customer living on the server side). Now, if look at output from the server will look similar to: processed customer with new id of 418 Customer.toString() being called. Notice that the 'Customer.toString() being called.' printed out at the end. This is the result of the client's call to print out the contents of the customer object returned from the processCustomer() method, which actually lives within the server vm. This example has shown how can pass around TransporterClient proxies to target pojos. However, when doing this, is important to understand where the code is actually being executed as there are consequences to being remote verse local, which need to be understood.