org.jgroups.demos
Class QuoteServer

java.lang.Object
  extended by org.jgroups.ReceiverAdapter
      extended by org.jgroups.demos.QuoteServer
All Implemented Interfaces:
MembershipListener, MessageListener, Receiver

public class QuoteServer
extends ReceiverAdapter

Example of a replicated quote server. The server maintains state which consists of a list of quotes and their corresponding values. When it is started, it tries to reach other quote servers to get its initial state. If it does not receive any response after 5 seconds, it assumes it is the first server and starts processing requests.

Any updates are multicast across the cluster

Author:
Bela Ban

Constructor Summary
QuoteServer()
           
 
Method Summary
 java.util.Hashtable getAllStocks()
           
 float getQuote(java.lang.String stock_name)
           
 void getState(java.io.OutputStream ostream)
          Allows an application to write a state through a provided OutputStream.
static void main(java.lang.String[] args)
           
 void printAllStocks()
           
 void setQuote(java.lang.String stock_name, java.lang.Float value)
           
 void setState(java.io.InputStream istream)
          Allows an application to read a state through a provided InputStream.
 void start()
           
 void viewAccepted(View new_view)
          Called when a change in membership has occurred.
 
Methods inherited from class org.jgroups.ReceiverAdapter
block, receive, suspect, unblock
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

QuoteServer

public QuoteServer()
Method Detail

viewAccepted

public void viewAccepted(View new_view)
Description copied from class: ReceiverAdapter
Called when a change in membership has occurred. No long running actions, sending of messages or anything that could block should be done in this callback. If some long running action needs to be performed, it should be done in a separate thread.

Note that on reception of the first view (a new member just joined), the channel will not yet be in the connected state. This only happens when Channel.connect(String) returns.

Specified by:
viewAccepted in interface MembershipListener
Overrides:
viewAccepted in class ReceiverAdapter

start

public void start()

getQuote

public float getQuote(java.lang.String stock_name)
               throws java.lang.Exception
Throws:
java.lang.Exception

setQuote

public void setQuote(java.lang.String stock_name,
                     java.lang.Float value)

getAllStocks

public java.util.Hashtable getAllStocks()

printAllStocks

public void printAllStocks()

getState

public void getState(java.io.OutputStream ostream)
              throws java.lang.Exception
Description copied from class: ReceiverAdapter
Allows an application to write a state through a provided OutputStream. After the state has been written the OutputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.

Specified by:
getState in interface MessageListener
Overrides:
getState in class ReceiverAdapter
Parameters:
ostream - the OutputStream
Throws:
java.lang.Exception - if the streaming fails, any exceptions should be thrown so that the state requester can re-throw them and let the caller know what happened
See Also:
OutputStream.close()

setState

public void setState(java.io.InputStream istream)
              throws java.lang.Exception
Description copied from class: ReceiverAdapter
Allows an application to read a state through a provided InputStream. After the state has been read the InputStream doesn't need to be closed as stream closing is automatically done when a calling thread returns from this callback.

Specified by:
setState in interface MessageListener
Overrides:
setState in class ReceiverAdapter
Parameters:
istream - the InputStream
Throws:
java.lang.Exception - if the streaming fails, any exceptions should be thrown so that the state requester can catch them and thus know what happened
See Also:
InputStream.close()

main

public static void main(java.lang.String[] args)


Copyright © 1998-2012 Bela Ban / Red Hat. All Rights Reserved.