The addressbook program consists of two parts:
public class AddressBook { private static Hashtable name2AddressTable = new Hashtable(); public AddressBook() { } public void addEntry( String name, Address address ) { name2AddressTable.put( name, address ); } public void addEntry( String firstName, String lastName, Address address ) { name2AddressTable.put( firstName + " " + lastName, address ); } public Address getAddressFromName( String name ) throws IllegalArgumentException { return (Address) name2AddressTable.get( name ); } }
public class Address implements java.io.Serializable { private static final long serialVersionUID = 1L; private int streetNum; private java.lang.String streetName; private java.lang.String city; private java.lang.String state; private int zip; public Address() { } public int getStreetNum() { return streetNum; } public void setStreetNum( int streetNum ) { this.streetNum = streetNum; } public java.lang.String getStreetName() { return streetName; } public void setStreetName( java.lang.String streetName ) { this.streetName = streetName; } public java.lang.String getCity() { return city; } public void setCity( java.lang.String city ) { this.city = city; } public java.lang.String getState() { return state; } public void setState( java.lang.String state ) { this.state = state; } public int getZip() { return zip; } public void setZip( int zip ) { this.zip = zip; } }
import java.io.IOException; import de.netseeker.ejoe.EJServer; import de.netseeker.ejoe.handler.DefaultRemotingHandler; public class AddressServer { public static void main( String[] args ) { EJServer server = new EJServer( new DefaultRemotingHandler() ); server.setMaxReadProcessors( 1 ); server.setMaxWriteProcessors( 1 ); server.enableNonBlockingIO( true ); server.enablePersistentConnections( true ); try { server.start(); } catch ( IOException e ) { e.printStackTrace(); System.exit( -1 ); } } }
##################################################################################### # This is a customized configuration for remote reflection in EJOE. # EJOE handles received reflection requests by clients VERY restrictive: # Only packages/classes which are listed in this file # can be used for remote reflection. # To overwrite the reflection settings, provide a customized # ejoe-reflection-conf.properties on the classpath. ##################################################################################### de.netseeker.ejoe.examples.remoting.*
import java.io.IOException; import com.thoughtworks.xstream.XStream; import de.netseeker.ejoe.EJClient; import de.netseeker.ejoe.EJConstants; import de.netseeker.ejoe.handler.RemotingRequest; public class AddressClient { public static void main( String[] args ) { EJClient client = new EJClient( "localhost", EJConstants.EJOE_PORT ); client.enablePersistentConnection( true ); try { // create an address object for Jimmy Who Address address = new Address(); address.setStreetNum( 20 ); address.setStreetName( "Peachtree Avenue" ); address.setCity( "Atlanta" ); address.setState( "GA" ); address.setZip( 39892 ); //create a RemotingRequest to invoke the remote AddressBook RemotingRequest request = new RemotingRequest( AddressBook.class.getName(), "addEntry", new Object[] { "Jimmy Who", address } ); //add Jimmys address System.out.println("adding Jimmys address..."); client.execute( request ); // create an address object for Jane Who address.setStreetNum( 21 ); address.setStreetName( "Peachtree Avenue" ); address.setCity( "Atlanta" ); address.setState( "GA" ); address.setZip( 39892 ); //add Janes address request.setArgs( new Object[] { "Jane", "Who", address } ); System.out.println("adding Janes address..."); client.execute( request ); // now query both addresses request.setMethod( "getAddressFromName" ); request.setArgs( new Object[] { "Jimmy Who" } ); System.out.println("querying Jimmys address..."); Address adrJimmy = (Address) client.execute( request ); System.out.println("Jimmys address: "); XStream xstream = new XStream(); System.out.println( xstream.toXML( adrJimmy )); System.out.println(""); request.setArgs( new Object[] { "Jane Who" } ); System.out.println("querying Janes address..."); Address adrJane = (Address) client.execute( request ); System.out.println("Janes address: "); System.out.println( xstream.toXML( adrJane )); } catch ( IOException e ) { e.printStackTrace(); } } }