- A Lightweight Java Middleware
<< Example 3a   Example 4a >>

Example 3b

is a different implementation of the same scenario as in the previous example 3a, with the Agent run HERE, the Listener THERE.


How to do it

This implementation uses an interface proxy instead of a class proxy.


package tw.net.ocs.example;

import tw.net.ocs.*;
import tw.net.ocs.proxy.*;

public class Example3b {

   public static void main(String[] args) {

      System.out.println("Running Example3b: Agent HERE, Listener THERE");

         // Create an OCSClient instance that
      OCSClient client=OCSClient.createInstance(
         "localhost",  // communicates with a server at this host
         23229,  // and this port,
         null,  // is unnamed, and
         true  // made the standard client

      try {
         // Ask the client for an object that allows to invoke user methods on the server
      OCSServerUserInterface server = client.getServerUserProxy();
         // Create a Listener and install it on the server
      server.addSubject(new ListenerImpl());

         // Create a proxy that implements interface Listener and 
         // associate it with the real remote Listener just installed
      Listener listener=(Listener)OCSInterfaceProxyFactory.generateProxyFor(
         Listener.class,  // the interface to implement
         ListenerImpl.class,  // the class of the subject to attach to
         null  // the ID of the subject (unspecified, so the proxy attaches to arbitrary subject)
         // Create an Agent and pass it the proxy Listener
         // (that delegates its invocations to the remote real Listener)
      Agent agent = new Agent(listener);
         // ... and let the Agent act
      catch (OCSException x) {


To run the demo,
start a server in a separate shell:

java -cp ocs.jar tw.net.ocs.OCSServer -start -port 23229

and run Example3b:

java -cp ocs.jar;ocs-examples.jar tw.net.ocs.example.Example3b
Last modified Jun 8, 2006