Skip to content

Instantly share code, notes, and snippets.

@hideki
Created September 28, 2017 23:21
Show Gist options
  • Save hideki/e79bf9426e87b3623340ffdc12fd764b to your computer and use it in GitHub Desktop.
Save hideki/e79bf9426e87b3623340ffdc12fd764b to your computer and use it in GitHub Desktop.
package sample;
import com.couchbase.lite.*;
import com.couchbase.lite.replicator.Replication;
import com.couchbase.lite.util.Log;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Insets;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Map;
public class Main extends Application implements Replication.ChangeListener, LiveQuery.ChangeListener {
public static final String TAG = Main.class.getSimpleName();
private static final String DB_NAME = "db";
Manager mgr;
Database db;
LiveQuery live;
Replication pull;
Replication push;
@Override
public void start(Stage primaryStage) throws Exception{
primaryStage.setTitle("CBL + JavaFX");
HBox hbox = new HBox();
hbox.setPadding(new Insets(15, 12, 15, 12));
hbox.setSpacing(10);
hbox.setStyle("-fx-background-color: #336699;");
Button btnStart = new Button();
btnStart.setText("Start");
btnStart.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
startReplicator();
}
});
Button btnStop = new Button();
btnStop.setText("Stop");
btnStop.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
stopReplicator();
}
});
StackPane root = new StackPane();
hbox.getChildren().addAll(btnStart, btnStop);
root.getChildren().add(hbox);
primaryStage.setScene(new Scene(root, 300, 250));
primaryStage.show();
openDatabase();
}
public static void main(String[] args) {
launch(args);
}
private void openDatabase(){
Log.e(TAG, "OPEN");
try {
mgr = new Manager(new JavaContext(), Manager.DEFAULT_OPTIONS);
} catch (IOException e) {
Log.e(TAG, "Failed to initialize Manager instance", e);
}
DatabaseOptions ops = new DatabaseOptions();
ops.setCreate(true);
try {
db = mgr.openDatabase(DB_NAME, ops);
} catch (CouchbaseLiteException e) {
Log.e(TAG, "Failed to open database", e);
}
db.addChangeListener(new Database.ChangeListener() {
@Override
public void changed(Database.ChangeEvent changeEvent) {
Log.e(TAG, "changeEvent -> " + changeEvent);
}
});
View v = db.getView("liveQuery");
v.setMap(new Mapper() {
@Override
public void map(Map<String, Object> map, Emitter emitter) {
emitter.emit(map.get("type"), null);
}
}, "1");
Query q = v.createQuery();
live = q.toLiveQuery();
live.addChangeListener(this);
live.start();
}
private void closeDatabase(){
Log.e(TAG, "CLOSE");
if(live!= null)
live.stop();
db.close();
mgr.close();
}
private void startReplicator() {
Log.e(TAG, "START");
URL url = null;
try {
url = new URL("http://10.17.1.243:4984/db");
} catch (MalformedURLException e) {
Log.e(TAG, "Error with URL: http://10.17.1.243:4984/db", e);
}
pull = db.createPullReplication(url);
push = db.createPushReplication(url);
pull.setContinuous(true);
push.setContinuous(true);
pull.addChangeListener(this);
push.addChangeListener(this);
pull.start();
push.start();
}
private void stopReplicator() {
Log.e(TAG, "STOP");
if (pull != null)
pull.stop();
if (push != null)
push.stop();
closeDatabase();
Platform.exit(); // JavaFX terminate app
}
@Override
public void changed(Replication.ChangeEvent changeEvent) {
Log.e(TAG, "Replication.ChangeListener.changed(): evnet -> " + changeEvent);
}
@Override
public void changed(LiveQuery.ChangeEvent changeEvent) {
Log.e(TAG, "LiveQuery.ChangeListener.changed(): evnet -> " + changeEvent.getRows().getSequenceNumber());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment