Skip navigation links

Package com.thoughtworks.proxy.toys.failover

A toy to handle failover situations.

See: Description

Package com.thoughtworks.proxy.toys.failover Description

A toy to handle failover situations.

The package provides a proxy factory creating proxies, that handle failover. Main component is the Failover toy, a utility class creating these proxies. Such a proxy contains an instance of a FailoverInvoker that delegates all calls. The implementation subclasses the HotSwapping toy. The proxy itself has a pool of objects. Every call is delegated to the same object as long as no exception occurs. In this case the proxy will use the next of the provided elements.

One use case are different implementations, that can handle only specific elements:

Format[] formats = new Format[]{
    NumberFormat.getInstance(), 
    DateFormat.getDateInstance(), 
    new MessageFormat("{1}, {0}")
};
Format format = Failover.proxy(Format.class).with(formats).excepting(RuntimeException.class)
    .build(new CglibProxyFactory());
System.out.println("Format a date: " + format.format(new Date()));
System.out.println("Format a message: " + format.format(new String[]{"John", "Doe"}));
System.out.println("Format a number: " + format.format(42));

Note that this example is somewhat tweaked, since the different format classes of the JDK can handle different input, e.g. the DateFormat is able to interpret a number as date.

Another use case are distributed sources with a unified interface. The following example will concat the input of all DataInput instances as if only one DataInput is in use:

DataInput[] dataInputs = new DataInput[]{
        new DataInputStream(new ByteArrayInputStream(new byte[] {0, 'A', 0, 'n', 0, ' '})),
        new DataInputStream(new ByteArrayInputStream(new byte[] {0, 'e', 0, 'x', 0, 'a', 0, 'm', 0, 'p', 0, 'l', 0, 'e'})),
};
DataInput dataInput = Failover.proxy(DataInput.class).with(dataInputs).excepting(IOException.class)
    .build(new StandardProxyFactory());
StringBuffer buffer = new StringBuffer();
try {
    while (buffer.append(dataInput.readChar()) != null)
        ;
} catch (IOException e) {
}
System.out.println("Read: " + buffer.toString());
Skip navigation links

Copyright © 2005–2015 Codehaus. All rights reserved.