Facades

Overview

The Facade pattern is what you should have in mind as you design an interface for publishing over JRemoting.  It goes hand-in-hand with Immutable objects. Understanding both is key to making JRemoting work for you.

Facades have methods that are going to be invoked over the wire.  The results of those methods can either be immutable (also known as value objects), or other (secondary) facades.  If the method returns a secondary facade, it could be the interface declared on the return type or a more derived interface.  If an immutable it can be anything that is serializable, but cannot have a reference to anything that's a facade.

Sample Facades and Immutables (Value Objects)

interface Bank {
    Account findAccount(int accountID)
}

interface Account {
    Debit debitFunds(Amount amount, Account toAccount) throws UnsufficientFundsException
    Credit creditFunds(Amount amount, Account fromAccount)
}

interface SavingsAccount extends Account {
    Rate getInterestRate()
}

interface CheckingAccount extends Account {
    Amount getOverdraftLimit()
    void setupAutoTransfer(Amount maxBalance, Account destinationForSurplass)
}


class Txn implements Serializable {
    private Amount amount;
    private Date date;
    private String ref;
    public Amount getAmount() { .... }
    public Date getDate() { .... }
    public String getReference() { .... }
}

class Debit extends Txn {
    private int toAccountID;
    public int getToAccount() { .... }
}

class Credit extends Txn {
    private int fromAccountID;
    public int getFromAccount() { .... }
}

class Amount implements Serializable {
    private BigDecimal value;
    public BigDecimal  getValue() { .... }
}

The interface Bank above is the one that is the primary facade.  SavingsAccount and CheckingAccount are the secondary facade.  They are used as return types and parameters to methods on the primary facade and each other. The plain classes Txn, Debit, Credit and Amount are plain classes that are serializable. Date and BigDecimal are from Java's libraries and are Serializable out of the box. Note that Credit and Debit have a corresponding account ID rather than a reference to the Account itself.

Note also that the debitFunds method on the primary facade has an exception.  That exception is plain (it does not extend anything special from the JRemoting API) and is replicated on the client side by JRemoting.  The same would be true of exceptions that are drived from RuntimeException.

With primary and secondary facades, you have to specify both on the server side. The primary one is the one you resolve over the wire, the secondary ones are activated during operation. Like primary facades, the secondary ones are eligible for garbage collection.

Publishing

    Bank myBank = new BankImpl();
    server = new SocketServer(new ConsoleServerMonitor(), new InetSocketAddress(10333));
    server.publish(myBank, "BankService", Bank.class, 
                   SavingsAccount.class, CheckingAccount.class); // secondary facades are varargs
    server.start();
  

Client side usage

    serviceResolver = new ServiceResolver(new SocketTransport(new ConsoleClientMonitor(), new SocketDetails("localhost", 10333)));
Bank remoteBank = serviceResolver.resolveService("BankService");