为Couchbase lite 2.x和同步网关设置复制器时出现问题

时间:2019-06-19 21:36:35

标签: java android couchbase couchbase-lite

我正在尝试通过同步网关在沙发上lite移动应用程序和沙发上服务器之间进行一些非常简单的同步。我已经获得了同步网关与服务器进行通信,因为对网关使用curl REST调用将与主服务器同步。

但是,当尝试与couchbase-lite进行同步时,couchbase-lite根本无法同步。

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "LOG";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the database (and create it if it doesn’t exist).
        DatabaseConfiguration config = new DatabaseConfiguration(getApplicationContext());
        Database database = null;
        try {
            database = new Database("mydb", config);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

// Create a new document (i.e. a record) in the database.
        MutableDocument mutableDoc = new MutableDocument()
                .setFloat("version", 2.0F)
                .setString("type", "SDK");

// Save it to the database.
        try {
            database.save(mutableDoc);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }

// Update a document.
        mutableDoc = database.getDocument(mutableDoc.getId()).toMutable();
        mutableDoc.setString("language", "Java");
        try {
            database.save(mutableDoc);
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        Document document = database.getDocument(mutableDoc.getId());
// Log the document ID (generated by the database) and properties
        Log.i(TAG, "Document ID :: " + document.getId());
        Log.i(TAG, "Learning " + document.getString("language"));

// Create a query to fetch documents of type SDK.
        Query query = QueryBuilder.select(SelectResult.all())
                .from(DataSource.database(database))
                .where(Expression.property("type").equalTo(Expression.string("SDK")));
        ResultSet result = null;
        try {
            result = query.execute();
        } catch (CouchbaseLiteException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "Number of rows ::  " + result.allResults().size());

// Create replicators to push and pull changes to and from the cloud.
        Endpoint targetEndpoint = null;
        try {
            targetEndpoint = new URLEndpoint(new URI("ws://10.0.2.2:4984/demobucket"));
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        ReplicatorConfiguration replConfig = new ReplicatorConfiguration(database, targetEndpoint);
        replConfig.setReplicatorType(ReplicatorConfiguration.ReplicatorType.PUSH_AND_PULL);

// Add authentication.
        replConfig.setAuthenticator(new BasicAuthenticator("admin", "pass"));

// Create replicator.
        Replicator replicator = new Replicator(replConfig);

// Listen to replicator change events.
        replicator.addChangeListener(change -> {
            if (change.getStatus().getError() != null) {
                Log.i(TAG, "Error code ::  " + change.getStatus().getError().getCode());
            }
        });

// Start replication.
        replicator.start();
    }
}

此代码从字面上是从沙发数据库文档站点https://docs.couchbase.com/couchbase-lite/current/java.html粘贴的,但是不起作用。

我收到错误11001,它等同于复制器侦听器中出现的“ //对等必须关闭,例如因为主机应用程序退出了”。

我使用的同步网关配置文件如下:

{
    "interface":":4984",
     "logging": {
      "log_file_path": "/var/tmp/sglogs",
      "console": {
        "log_level": "debug",
        "log_keys": ["*"]
      },
      "error": {
        "enabled": true,
        "rotation": {
          "max_size": 20,
          "max_age": 180
        }
      },
      "warn": {
        "enabled": true,
        "rotation": {
          "max_size": 20,
          "max_age": 90
        }
      },
      "info": {
        "enabled": false
      },
      "debug": {
        "enabled": false
      }
    },
    "databases": {
      "demobucket": {
        "import_docs": "continuous",
        "enable_shared_bucket_access":true,  
        "bucket":"demobucket",
        "server": "http://cb-server:8091",
        "username": "admin",
        "password": "password",
        "num_index_replicas":0,
        "users":{
            "GUEST": {"disabled":true},
            "admin": {"password": "password", "admin_channels": ["*"]}
        },
        "revs_limit":20
        }
    }
} 

1 个答案:

答案 0 :(得分:0)

@Jay在他的评论中给出了答案。 Replicator replicator是局部变量。 Activity停止后,复制器就有资格进行垃圾回收。在对等方看来,主机似乎正在停止。