gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-11-01 09:20:15 33 4
gpt4 key购买 nike

我正在尝试通过同步网关在 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 在他的评论中给出了答案。 Replicator 复制器 是一个局部变量。一旦 Activity 停止,复制器就有资格进行垃圾回收。这对对等方来说就像主机正在停止一样。

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

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com