This topic has not yet been written. The content below is from the topic description.
Transporter sample - basic The basic transporter example (found in org.jboss.remoting.samples.transporter.basic package) illustrates how to build a simple transporter for making remote invocations on plain old java objects. In this basic transporter example, will be using a few domain objects; Customer and Address, which are just data objects. public class Customer implements Serializable { 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() { 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(); } } public class Address implements Serializable { private String street = null; private String city = null; private String state = null; private int zip = -1; public String getStreet() { return street; } public void setStreet(String street) { this.street = street; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public String getState() { return state; } public void setState(String state) { this.state = state; } public int getZip() { return zip; } public void setZip(int zip) { this.zip = zip; } } Next comes the POJO that we want to expose a remote proxy for, which is CustomerProcessorImpl class. This implementation has one method to process a Customer object. It also implements the CustomerProcessor interface. public class CustomerProcessorImpl implements CustomerProcessor { /** * 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 Customer processCustomer(Customer customer) { if(customer != null && customer.getCustomerId() < 0) { customer.setCustomerId(new Random().nextInt(1000)); } System.out.println("processed customer with new id of " + customer.getCustomerId()); return customer; } } public interface CustomerProcessor { /** * Process a customer object. Implementors * should ensure that the customer object * passed as parameter should have its internal * state changed somehow and returned. * * @param customer * @return */ public Customer processCustomer(Customer customer); } So far, nothing special, just plain old java objects. Next need to create the server component that will listen for remote request to invoke on the target POJO. This is where the transporter comes in. public class Server { private String locatorURI = "socket://localhost:5400"; private TransporterServer server = null; public void start() throws Exception { server = TransporterServer.createTransporterServer(locatorURI, new CustomerProcessorImpl()); } 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(); } } } The Server class is a pretty simple one. It calls the TransporterServer factory method to create the server component for the CustomerProcessorImpl instance using the specified remoting locator information. The TransporterServer returned from the createTransporterServer() call will be a running instance of a remoting server using the socket transport that is bound to localhost and listening for remote requests on port 5400. The requests that come in will be forwarded to the remoting handler which will convert them into direct method calls on the target POJO, CustomerProcessorImpl in this case, using reflection. The TransporterServer has a start() and stop() method exposed to control when to start and stop the running of the remoting server. The start() method is called automatically within the createTransporterServer() method, so is ready to receive requests upon the return of this method. The stop() method, however, needs to be called explicitly when no longer wish to receive remote calls on the target POJO. Next up is the client side. This is represented by 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); Customer processedCustomer = customerProcessor.processCustomer(customer); 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("Atlanata"); 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 is also pretty simple. It creates a new Customer object instance, creates the remote proxy to the CustomerProcessor, and then calls on the CustomerProcessor to process its new Customer instance. To get the remote proxy for the CustomerProcessor, all that is required is to call the TransporterClient's method createTransporterClient() method and pass the locator uri and the type of the remote proxy (and explicitly cast the return to that type). This will create a dynamic proxy for the specified type, CustomerProcessor in this case, which is backed by a remoting client which in turn makes the calls to the remote POJO's remoting server. Once the call to createTransportClient() has returned, the remoting client has already made its connection to the remoting server and is ready to make calls (will throw an exception if it could not connect to the specified remoting server). When finished making calls on the remote POJO proxy, will need to explicitly destroy the client by calling destroyTransporterClient() and pass the remote proxy instance. This allows the remoting client to disconnect from the POJO's remoting server and clean up any network resources previously used. To run this example, can run the Server and then the Client. Or can go to the examples directory and run the ant target 'run-transporter-basic-server' and then in another window run the ant target 'run-transporter-basic-client'. For example: ant run-transporter-basic-server and then: ant run-transporter-basic-client The output from the Client console should be similar to: Customer to be processed: Customer: customer id: -1 first name: Bob last name: Smith street: 101 Oak Street city: Atlanata state: GA zip: 30249 Customer is now: Customer: customer id: 204 first name: Bob last name: Smith street: 101 Oak Street city: Atlanata state: GA zip: 30249 and the output from the Server class should be similar to: processed customer with new id of 204 The output shows that the Customer instance created on the client was sent to the server where it was processed (by setting the customer id to 204) and returned to the client (and printed out showing that the customer id was set to 204).