Database Reference
In-Depth Information
The start method is used to give applications the chance to bind Listener
methods, to the client before events are sent. This is particularly important
when listening for connection state events. For example, the following code
snippet, found in wiley.streaming.curator.ConnectTest , causes
state changes to be printed to the console:
CuratorFramework client =
CuratorFrameworkFactory. builder ()
.connectString("localhost:2181")
.retryPolicy( new ExponentialBackoffRetry(1000,3))
.namespace("subspace")
.build();
client.getConnectionStateListenable().addListener(
new ConnectionStateListener() {
public void stateChanged(
CuratorFramework arg0,
ConnectionState arg1
) {
System. out .println("State Changed: "+arg1);
}
});
client.start();
Thread.sleep(1000);
In this example, the client is first built using the Builder interface with
a namespace associated with it. Then a Listener is implemented to print
out the changes in state as they happen. Finally, the client is started using
start . Running this code should output a connected command if a short
Thread.sleep command is added after the start to give the client some
time to connect:
log4j:WARN No appenders could be found for logger
(org.apache.curator.framework.imps.CuratorFrameworkImpl).
log4j:WARN Please initialize the log4j system properly.
State Changed: CONNECTED
Search WWH ::




Custom Search