Transporters - JBoss serialization
Transporter sample - JBoss serialization The transporter serialization example (found in org.jboss.remoting.samples.transporter.serialization package) is very similar to the previous basic example, except in this one, the domain objects being sent over the wire will NOT be Serializable. This is accomplished via the use of JBoss Serialization. This can be useful when don't know which domain objects you may be using in remote calls or if adding ability for remote calls on legacy code. To start, there are a few more domain objects: Order, OrderProcessor, and OrderProcessorImpl. These will use some of the domain objects from the previous example as well, such as Customer. public class Order { private int orderId = -1; private boolean isProcessed = false; private Customer customer = null; private List items = null; public int getOrderId() { return orderId; } public void setOrderId(int orderId) { this.orderId = orderId; } public boolean isProcessed() { return isProcessed; } public void setProcessed(boolean processed) { isProcessed = processed; } public Customer getCustomer() { return customer; } public void setCustomer(Customer customer) { this.customer = customer; } public List getItems() { return items; } public void setItems(List items) { this.items = items; } public String toString() { StringBuffer buffer = new StringBuffer(); buffer.append("\nOrder:\n"); buffer.append("\nIs processed: " + isProcessed); buffer.append("\nOrder id: " + orderId); buffer.append(customer.toString()); buffer.append("\nItems ordered:"); Iterator itr = items.iterator(); while(itr.hasNext()) { buffer.append("\n" + itr.next().toString()); } return buffer.toString(); } } public class OrderProcessorImpl implements OrderProcessor { private CustomerProcessor customerProcessor = null; public OrderProcessorImpl() { customerProcessor = new CustomerProcessorImpl(); } public Order processOrder(Order order) { System.out.println("Incoming order to process from customer.\n" + order.getCustomer()); // has this customer been processed? if(order.getCustomer().getCustomerId() < 0) { order.setCustomer(customerProcessor.processCustomer(order.getCustomer())); } List items = order.getItems(); System.out.println("Items ordered:"); Iterator itr = items.iterator(); while(itr.hasNext()) { System.out.println(itr.next()); } order.setOrderId(new Random().nextInt(1000)); order.setProcessed(true); System.out.println("Order processed. Order id now: " + order.getOrderId()); return order; } } public interface OrderProcessor { public Order processOrder(Order order); } The OrderProcessorImpl will take orders, via the processOrder() method, check that the customer for the order has been processed, and if not have the customer processor process the new customer. Then will place the order, which means will just set the order id and processed attribute to true. The most important point to this example is that the Order class does NOT implement java.io.Serializable. Now onto the Server class. This is just like the previous Server class in the basic example with one main difference: the locatorURI value. public class Server { private String locatorURI = "socket://localhost:5400/?serializationtype=jboss"; private TransporterServer server = null; public void start() throws Exception { server = TransporterServer.createTransporterServer(locatorURI, new OrderProcessorImpl()); } 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 addition of serializationtype=jboss tells the remoting framework to use JBoss Serialization in place of the standard java serialization. On the client side, there is the Client class, just as in the previous basic example. public class Client { private String locatorURI = "socket://localhost:5400/?serializationtype=jboss"; public void makeClientCall() throws Exception { Order order = createOrder(); OrderProcessor orderProcessor = (OrderProcessor) TransporterClient.createTransporterClient(locatorURI, OrderProcessor.class); System.out.println("Order to be processed: " + order); Order changedOrder = orderProcessor.processOrder(order); System.out.println("Order now processed " + changedOrder); TransporterClient.destroyTransporterClient(orderProcessor); } private Order createOrder() { Order order = new Order(); Customer customer = createCustomer(); order.setCustomer(customer); List items = new ArrayList(); items.add("Xbox 360"); items.add("Wireless controller"); items.add("Ghost Recon 3"); order.setItems(items); return order; } 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(); } } } Again, the biggest difference to note is that have added serializationtype=jboss to the locator uri. Note: Running this example requires JDK 1.5. To run this example, can run the Server and then the Client. Or can go to the examples directory and run the ant target 'ant run-transporter-serialization-server' and then in another window run the ant target 'ant run-transporter-serialization-client'. For example: ant run-transporter-serialization-server and then: ant run-transporter-serialization-client When the server and client are run the output for the Client class is: Order to be processed: Order: Is processed: false Order id: -1 Customer: customer id: -1 first name: Bob last name: Smith street: 101 Oak Street city: Atlanata state: GA zip: 30249 Items ordered: Xbox 360 Wireless controller Ghost Recon 3 Order now processed Order: Is processed: true Order id: 221 Customer: customer id: 861 first name: Bob last name: Smith street: 101 Oak Street city: Atlanata state: GA zip: 30249 Items ordered: Xbox 360 Wireless controller Ghost Recon 3 The client output shows the printout of the newly created order before calling the OrderProcessor and then the processed order afterwards. Noticed that the processed order has its customer's id set, its order id set and the processed attribute is set to true. And the output from the Server is: Incoming order to process from customer. Customer: customer id: -1 first name: Bob last name: Smith street: 101 Oak Street city: Atlanata state: GA zip: 30249 processed customer with new id of 861 Items ordered: Xbox 360 Wireless controller Ghost Recon 3 Order processed. Order id now: 221 The server output shows the printout of the customer before being processed and then the order while being processed.