[jboss-cvs] JBossRemoting/docs/guide/en ...

Tom Elrod tom.elrod at jboss.com
Mon Jul 31 16:08:18 EDT 2006


  User: telrod  
  Date: 06/07/31 16:08:18

  Modified:    docs/guide/en  chap10.xml
  Log:
  JBREM-428 - added transporter multiple and proxy samples.
  
  Revision  Changes    Path
  1.4       +487 -3    JBossRemoting/docs/guide/en/chap10.xml
  
  (In the diff below, changes in quantity of whitespace are not shown.)
  
  Index: chap10.xml
  ===================================================================
  RCS file: /cvsroot/jboss/JBossRemoting/docs/guide/en/chap10.xml,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -b -r1.3 -r1.4
  --- chap10.xml	31 Jul 2006 19:17:14 -0000	1.3
  +++ chap10.xml	31 Jul 2006 20:08:18 -0000	1.4
  @@ -867,13 +867,13 @@
      }
   }</programlisting>
   
  -      <para> In the Client class, create a TransporterClient which can be cast
  +      <para>In the Client class, create a TransporterClient which can be cast
         to the desired type, which is DataProcessor in this case. In calling the
         createTransporterClient, need to specify the locator ulr (same as was
         used for the TransporterServer), and the interface type will be calling
         on for the target pojo. Once have the DateProcessor variable, will make
         the call to formatDate() and pass a newly created Date object. The
  -      return will be a formated String of the date passed. </para>
  +      return will be a formated String of the date passed.</para>
   
         <para>To run this example, can run the Server and then the Client. Or
         can go to the examples directory and run the ant target
  @@ -1953,6 +1953,490 @@
       </section>
   
       <section>
  +      <title>Transporters sample - multiple</title>
  +
  +      <para>The multiple transporter example (found in
  +      org.jboss.remoting.samples.transporter.multiple package) shows how can
  +      have a multiple target pojos exposed via the same TransporterServer. In
  +      this example, will be two pojos being exposed, CustomerProcessorImpl and
  +      AccountProcessorImpl. Since the domain objects for this example is
  +      similar to the others discussed in previous examples, will just focus on
  +      the server and client code. On the server side, need to create the
  +      TransporterServer so that will included both of the target pojos.</para>
  +
  +      <programlisting>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());
  +      server.addHandler(new AccountProcessorImpl(), AccountProcessor.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();
  +      }
  +   }
  +}</programlisting>
  +
  +      <para>The TransporterServer is created with the CustomerProcessorImpl as
  +      the inital target pojo. Now that have a live TransporterServer, can add
  +      other pojos as targets. This is done using the addHandler() method where
  +      the target pojo instance is passed and then the interface type to be
  +      exposed as.</para>
  +
  +      <para>Next have the Client that makes the call to both pojos.</para>
  +
  +      <programlisting>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);
  +
  +      AccountProcessor accountProcessor = (AccountProcessor) TransporterClient.createTransporterClient(locatorURI, AccountProcessor.class);
  +
  +      System.out.println("Asking for a new account to be created for customer.");
  +      Account account = accountProcessor.createAccount(processedCustomer);
  +      System.out.println("New account: " + account);
  +
  +      TransporterClient.destroyTransporterClient(customerProcessor);
  +      TransporterClient.destroyTransporterClient(accountProcessor);
  +
  +   }
  +
  +   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)
  +   {
  +      org.jboss.remoting.samples.transporter.multiple.client.Client client = new org.jboss.remoting.samples.transporter.multiple.client.Client();
  +      try
  +      {
  +         client.makeClientCall();
  +      }
  +      catch (Exception e)
  +      {
  +         e.printStackTrace();
  +      }
  +   }
  +
  +
  +}</programlisting>
  +
  +      <para>Notice that TransporterClients are created for each target pojo
  +      want to call upon, they just happen to share the same locator uri. These
  +      are independant instances so need to both be destroyed on their own when
  +      finished with them.</para>
  +
  +      <para>To run this example, run the Server class and then the Client
  +      class. This can be done via ant targets
  +      'run-transporter-multiple-server' and then
  +      'run-transporter-multiple-client'. For example:</para>
  +
  +      <programlisting>ant run-transporter-multiple-server</programlisting>
  +
  +      <para>and then:</para>
  +
  +      <programlisting>ant run-transporter-multiple-client</programlisting>
  +
  +      <para>The output for the server should look similar to:</para>
  +
  +      <programlisting>processed customer with new id of 980
  +Created new account with account number: 1 and for customer:
  +
  +Customer:
  +customer id: 980
  +first name: Bob
  +last name: Smith
  +street: 101 Oak Street
  +city: Atlanta
  +state: GA
  +zip: 30249</programlisting>
  +
  +      <para>and the output from the client should look similar to:</para>
  +
  +      <programlisting>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: 980
  +first name: Bob
  +last name: Smith
  +street: 101 Oak Street
  +city: Atlanta
  +state: GA
  +zip: 30249
  +
  +Asking for a new account to be created for customer.
  +New account: Account - account number: 1
  +Customer: 
  +Customer:
  +customer id: 980
  +first name: Bob
  +last name: Smith
  +street: 101 Oak Street
  +city: Atlanta
  +state: GA
  +zip: 30249
  +</programlisting>
  +    </section>
  +
  +    <section>
  +      <title>Transporters sample - proxy</title>
  +
  +      <para>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.</para>
  +
  +      <para>To start, will look at the initial target pojo,
  +      CustomerProcessorImpl.</para>
  +
  +      <programlisting>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 &amp;&amp; customer.getCustomerId() &lt; 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;
  +   }
  +
  +}</programlisting>
  +
  +      <para>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.</para>
  +
  +      <para>Next will look at the Customer class. It is a basic data object in
  +      that is really just stores the customer data.</para>
  +
  +      <programlisting>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();
  +   }
  +
  +
  +}
  +</programlisting>
  +
  +      <para>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.</para>
  +
  +      <para>Now if look at the Server class, will see is a standard setup like
  +      have seen in previous samples.</para>
  +
  +      <programlisting>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();
  +      }
  +   }
  +}</programlisting>
  +
  +      <para>It is creating a TransporterServer for the CustomerProcessImpl
  +      upon being started and will wait 60 seconds for invocations.</para>
  +
  +      <para>Next is the Client class. </para>
  +
  +      <programlisting>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();
  +      }
  +   }
  +
  +
  +}</programlisting>
  +
  +      <para>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). </para>
  +
  +      <para>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:</para>
  +
  +      <programlisting>ant run-transporter-proxy-server</programlisting>
  +
  +      <para>ant then:</para>
  +
  +      <programlisting>ant run-transporter-proxy-client</programlisting>
  +
  +      <para>The output for the client should look similar to:</para>
  +
  +      <programlisting>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
  +</programlisting>
  +
  +      <para>The output for the client should like as expected. 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).</para>
  +
  +      <para>Now, if look at output from the server will look similar
  +      to:</para>
  +
  +      <programlisting>processed customer with new id of 418
  +Customer.toString() being called.</programlisting>
  +
  +      <para>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.</para>
  +
  +      <para>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.</para>
  +    </section>
  +
  +    <section>
         <title>Transporter sample -complex</title>
   
         <para>The complex transporter example (found in
  @@ -1966,7 +2450,7 @@
         Serialization (and non-serialized data objects). To run this example,
         run the Server class and then the Client class. This can be done via ant
         targets 'run-transporter-complex-server' and then
  -      'run-transporter-complex-server' as well. For example:</para>
  +      'run-transporter-complex-client' as well. For example:</para>
   
         <programlisting>ant run-transporter-complex-server</programlisting>
   
  
  
  



More information about the jboss-cvs-commits mailing list