RouterDrivenStockQuotes (Oracle - Berkeley DB Java Edition Examples)
Date: 2014-12-16

je.rep.quote

## Class RouterDrivenStockQuotes

- java.lang.Object
- - je.rep.quote.StockQuotes
  - - je.rep.quote.RouterDrivenStockQuotes

- Direct Known Subclasses:
    UpdateForwardingStockQuotes

      public class RouterDrivenStockQuotes
      extends StockQuotes

  This class is based on `StockQuotes` and illustrates use of an HA-aware router (implemented by `HARouter`), in conjunction with the `Monitor` class, to direct application requests, based upon the type of request (read or write) and the state (Master or Replica) of a node in the replication group. This example is meant to illustrate how a software load balancer might be integrated with JE HA, where `HARouter` plays the role of the load balancer for purposes of the example.

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

  In this example, unlike `StockQuotes`, only the HARouter has a console associated with it. It accepts commands typed into its console and forwards them as appropriate to the Master and Replicas in the group. The logic for tracking the Master resides in `HARouter`, and information about the state of the replication group is supplied by the `Monitor`. While this example uses just one HARouter instance for the entire group, production applications could use multiple router instances to avoid single points of failure.

  Each node, which in this example is an instance of `RouterDrivenStockQuotes`, establishes a server socket on which it can listen for requests from HARouter. The node that is currently the Master will expect both write and read requests from HARouter, while nodes that are Replicas will only expect read requests from the router.

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

       ------------               Read requests
       | HARouter |------------------------------------||
       | Instance |---------------------||             ||
       ------------                     ||             ||
        ||                              ||             ||
        || Write requests               ||             ||
        \/                              ||             ||
       ---------------------------      ||             ||
       | RouterDrivenStockQuotes |      ||             ||
       | Instance 1: Master      |      ||             ||
       ---------------------------      \/             ||
                      ---------------------------      ||
                      | RouterDrivenStockQuotes |      ||
                      | Instance 2: Replica     |      ||
                      ---------------------------      \/
                                     ---------------------------
                                     | RouterDrivenStockQuotes |
                                     | 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 JVM. For example, the first node can be started as follows:

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

  In addition to starting the nodes, you will also need to start the `HARouter` as described in its javadoc.
  See Also:
    `HARouter`

- - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `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`

Copyright (c) 2004, 2014 Oracle and/or its affiliates. All rights reserved.