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

标签 java android couchbase couchbase-lite

我正在尝试通过同步网关在 couchbase lite 移动应用程序和 couchbase 服务器之间实现一些非常简单的同步。我已经让同步网关与服务器进行通信,因为对网关使用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();
    }
}

此代码实际上是从 couchbase 文档网站 https://docs.couchbase.com/couchbase-lite/current/java.html 粘贴的。 ,但不起作用。

我收到错误 11001,这相当于复制器监听器中发生的“//Peer has to close, e.g.because host app is quitting”。

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

{
    "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
        }
    }
} 

最佳答案

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

关于java - 为 Couchbase lite 2.x 和同步网关设置复制器时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56676203/

相关文章:

java - 如何获取 JSplitPane 的特定组件?

java - 在 Android JUnit 类中测试 EditText 输入的首选方法

java - 将 csv 文件上传到 appengine

java - Firestore 中的索引是否有效?

android - 如何获取动态 fragment 的 View ?

node.js - 在 ubuntu 上配置/构建 couchnode 时遇到问题

java - 比较和交换: How to map the cas metadata field to document entity of my couchbase

c - 如何使用 lcb_create C API 连接到 Couchbase 2.0+?

java - 无法使用正则表达式检索行中的最后一个单词

java - 是否有一种干净的方法来确定 RandomAccessFile 是否是只读的?