Berkeley DB Java Edition Examples
version 4.1.21

je.rep.quote
Class UpdateForwardingStockQuotes

java.lang.Object
  extended by je.rep.quote.StockQuotes
      extended by je.rep.quote.RouterDrivenStockQuotes
          extended by je.rep.quote.UpdateForwardingStockQuotes

public class UpdateForwardingStockQuotes
extends RouterDrivenStockQuotes

This class is based on RouterDrivenStockQuotes and illustrates use of an HA unaware router (implemented by SimpleRouter), that load balances requests (both read and write) across all the nodes in a replication group. This example is meant to illustrate how a load balancer appliance might fit into the JE HA architecture, where SimpleRouter plays the role of the load balancer appliance for purposes of the example.

Be sure to read the Example Overview first to put this example into context.

The router is unaware of the state (Master or Replica) of each node, or the type (read or write) of the request. Nodes use the StateChangeListener to track the node that is currently the master and redirect write requests to it. That is, unlike the RouterDrivenStockQuotes example, it's the nodes and not the router that keeps track of the current master.

In this example, unlike StockQuotes, only the SimpleRouter has a console associated with it. It accepts commands typed into its console and forwards them as appropriate to the nodes in the group. The logic for tracking the Master resides in each node, and is supplied by the StateChangeListener.

Each node, which in this example is an instance of UpdateForwardingStockQuotes, establishes a server socket on which it can listen for requests from SimpleRouter. Read requests are processed directly by the node. Write requests are redirected to the current master and the result is communicated back to SimpleRouter.

The request flow between nodes in this example is shown below.

 ----------------       Read and Write requests
 | SimpleRouter |------------------------------------||
 | Instance     |---------------------||             ||
 ----------------      ||             ||             ||
                       ||             ||             ||
                       \/             ||             ||
 -------------------------------      ||             ||
 | UpdateForwardingStockQuotes |      ||             ||
 | Instance 1: Master          |      ||             ||
 -------------------------------      \/             ||
   /\           -------------------------------      ||
   ||           | UpdateForwardingStockQuotes |      ||
   ||---------- | Instance 2: Replica         |      ||
   || Write     -------------------------------      \/
   || requests                 -------------------------------
   ||                          | UpdateForwardingStockQuotes |
   ||--------------------------| Instance 3: Replica         |
                               -------------------------------

                                       ...more Replica instances...
 

This example is intended to be illustrative. It forwards requests as text, and receives responses in text form. Actual applications may for example, forward HTTP requests, or use some other application level network protocol to forward such requests.

Please review the javadoc in StockQuotes for a detailed description of the arguments that must be supplied at startup. The only difference is that you must use the name of this class when invoking the java vm.

For example, the first node can be started as follows:

 java je.rep.quote.UpdateForwardingStockQuotes -env /tmp/stockQuotes1 \
                                               -nodeName n1 \
                                               -nodeHost node.acme.com:5001 \
                                               -helperHost node.acme.com:5001
 

This instance of the application will therefore use port 5001 for HA, and, by convention, port 5101 (5001 + HARouter.APP_PORT_DISPLACEMENT) for application messages sent to it.

In addition to starting the nodes, you will also need to start the SimpleRouter as described in its javadoc.

See Also:
SimpleRouter

Method Summary
static void main(String[] argv)
           
 
Methods inherited from class je.rep.quote.StockQuotes
quit
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

main

public static void main(String[] argv)
                 throws Exception
Throws:
Exception

Berkeley DB Java Edition Examples
version 4.1.21

Copyright (c) 2004-2010 Oracle. All rights reserved.