Using a dynamic proxy for the addressbook service

Instead of constructing the RemoteRequest-objects ourself we will use a dynamic proxy for processing client request. This way we don't have to use EJClient#execute directly and we will be able to invoke remote methods like local methods. Additionally to the already discussed AddressBookService we need an clientside interface for the serverside AddressBook class first, then we can use EJOEs proxygenerator de.netseeker.ejoe.RemotingService to create an applicable dynamic proxy.

The interface

public interface IAddressBook
{
    public void addEntry( String name, Address address );

    public void addEntry( String firstName, String lastName, Address address );

    public Address getAddressFromName( String name ) throws IllegalArgumentException;

The new client application

public class AddressClientWithJdkProxy
{

    public static void main( String[] args )
    {
        EJClient client = new EJClient( "localhost", EJConstants.EJOE_PORT );
        client.enablePersistentConnection( true );
        IAddressBook addressBook = (IAddressBook) RemotingService.createService( AddressBook.class.getName(),
                                                                                 IAddressBook.class, client );

        // 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 );

        // add Jimmys address
        System.out.println( "adding Jimmys address..." );
        addressBook.addEntry( "Jimmy Who", address );

        // 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
        System.out.println( "adding Janes address..." );
        addressBook.addEntry( "Jane", "Who", address );

        // now query both addresses
        System.out.println( "querying Jimmys address..." );
        Address adrJimmy = addressBook.getAddressFromName( "Jimmy Who" );
        System.out.println( "Jimmys address: " );
        XStream xstream = new XStream();
        System.out.println( xstream.toXML( adrJimmy ) );

        System.out.println( "" );

        System.out.println( "querying Janes address..." );
        Address adrJane = addressBook.getAddressFromName( "Jane Who" );
        System.out.println( "Janes address: " );
        System.out.println( xstream.toXML( adrJane ) );
    }
}