To implement a cluster with Java, first an array of
Cluster
nodes is initialized and passed to theClusterDatabase.open()
method, then a ClusterConnection is instantiated for this database and a Listener thread is created. Finally the ClusterConnection’sstop()
method is called to terminate the Cluster session. The following code snippet demonstrates how a simple cluster Java application performs these steps:void clusterListen() { ClusterConnection con = new ClusterConnection(db); con.listen(); con.disconnect(); } { Database.Parameters params = new Database.Parameters(); // fill usual params ... //declare and fill cluster node's parameters Database.ClusterNodeParams[] node_params = new Database.ClusterNodeParams[2]; node_params[0] = new Database.ClusterNodeParams("192.168.0.1:20000"); node_params[1] = new Database.ClusterNodeParams("192.168.0.2:20000"); // set Cluster parameters params.clusterParams = new Database.ClusterParams(node_params, node_id); // Create cluster database. Transaction manager MUST be MVCC db = new Database(Database.MCO_CFG_MVCC_TRANSACTION_MANAGER | Database.MCO_CFG_CLUSTER_SUPPORT); // Open database db.open("cluster-db", params, DATABASE_SIZE); // start cluster listener thread Thread listenThread = new Thread(new Runnable() { public void run() { clusterListen(); } }); listenThread.start(); // connect to the database using ClusterConnection ClusterConnection con = new ClusterConnection(db); /***************************/ //do some work /***************************/ // Stop the connection and listener thread, disconnect and close database con.stop(); listenThread.join(); // wait for listener thread con.disconnect(); db.close(); }