public static void subscribeTo(ServiceHost host, ContinuousQueryId queryId,
Consumer<Operation> consumer) {
QueryTask task = getQueryTask(host, queryId);
Operation.createPost(host, ServiceUriPaths.CORE_LOCAL_QUERY_TASKS)
.setBody(task)
.setReferer(host.getUri())
.setCompletion((o, e) -> {
if (e != null && o.getStatusCode() != Operation.STATUS_CODE_CONFLICT) {
host.log(Level.SEVERE, Utils.toString(e));
return;
}
String taskUriPath = UriUtils.buildUriPath(
ServiceUriPaths.CORE_LOCAL_QUERY_TASKS, task.documentSelfLink);
Operation subscribePost = Operation.createPost(host, taskUriPath)
.setReferer(host.getUri())
.setCompletion((op, ex) -> {
if (ex != null) {
host.log(Level.SEVERE, Utils.toString(ex));
}
});
host.log(Level.INFO, "Subscribing to a continuous task: %s", taskUriPath);
host.startSubscriptionService(subscribePost, consumer,
ServiceSubscriber.create(false));
}).sendWith(host);
}