Java 为Couchbase lite 2.x和同步网关设置replicator时出现问题
我正在尝试通过同步网关在couchbase lite移动应用程序和couchbase服务器之间进行一些非常简单的同步。我已经让同步网关与服务器通信,因为使用对网关的curl-REST调用将与主服务器同步 但是,当尝试与couchbase lite同步时,couchbase lite根本不同步Java 为Couchbase lite 2.x和同步网关设置replicator时出现问题,java,android,couchbase,couchbase-lite,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";
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文档站点粘贴的,但不起作用
我得到了错误11001,这相当于//Peer必须关闭,例如,因为主机应用程序正在退出,这发生在replicator侦听器中
我使用的同步网关配置文件如下所示:
{
"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
}
}
}
@杰伊在评论中给出了答案。Replicator Replicator是一个局部变量。一旦活动停止,复制器就有资格进行垃圾收集。在对等方看来,似乎主机正在停止 您是否应该将replicator实例保留到任何属性?当退出此函数时,复制器实例是否将被限定范围?