Skip to content

Instantly share code, notes, and snippets.

@hideki
Created March 7, 2015 07:11
Show Gist options
  • Save hideki/b0075c98d4fdee842ece to your computer and use it in GitHub Desktop.
Save hideki/b0075c98d4fdee842ece to your computer and use it in GitHub Desktop.
package com.couchbase.lite.test;
import java.net.URL;
import java.util.List;
import java.util.Set;
import com.couchbase.lite.Database;
import com.couchbase.lite.DocumentChange;
import com.couchbase.lite.JavaContext;
import com.couchbase.lite.Manager;
import com.couchbase.lite.replicator.Replication;
import com.couchbase.lite.util.Log;
/**
* Created by hideki on 3/6/15.
*/
public class Test implements Database.ChangeListener, Replication.ChangeListener {
public static String TAG = "Test";
private static final String SYNC_URL = "http://10.17.2.82:5984/test2";
Manager manager = null;
Database database = null;
public void startCBL() throws Exception {
manager = new Manager(new JavaContext(), Manager.DEFAULT_OPTIONS);
database = manager.getDatabase("db");
URL syncUrl = new URL(SYNC_URL);
Replication push = database.createPushReplication(syncUrl);
push.setContinuous(true);
push.addChangeListener(this);
push.start();
Replication pull = database.createPullReplication(syncUrl);
pull.setContinuous(true);
pull.addChangeListener(this);
pull.start();
}
public void stopCBL() {
manager.close();
}
public static void main(String[] args) {
System.out.println("Begin Test");
Test test = new Test();
try {
test.startCBL();
Thread.sleep(1000 * 3);
test.stopCBL();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
// wait all threads
int i = 0;
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for (Thread t : threadSet) {
System.out.println("THREAD " + (i++) + ": " + t);
}
System.out.println("End Test");
}
@Override
public void changed(Database.ChangeEvent event) {
List<DocumentChange> changes = event.getChanges();
for (DocumentChange c : changes) {
Log.d(TAG, c.toString());
}
}
@Override
public void changed(final Replication.ChangeEvent event) {
if (event != null) {
Log.e(TAG, event.toString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment