private void resubscribe(Operation o) { if (getHost().isStopping()) { return; } // verify we are still included in the publisher's subscriptions ServiceSubscriptionState rsp = o.getBody(ServiceSubscriptionState.class); for (ServiceSubscriber item : rsp.subscribers.values()) { if (item.reference != null && item.reference.getPath().equals(getSelfLink())) { // we found our subscription, all is good. return; } } logWarning("Subscription missing from %s, resubscribing", o.getUri()); Operation reSubscribe = this.subscribeOp.clone() .setBody(this.subscribeRequest) .setCompletion((subOp, subE) -> { if (subE != null) { selfDeleteDueToFailure(subOp, subE); } }); sendRequest(reSubscribe); }
private void selfDeleteDueToFailure(Operation o, Throwable e) { if (getHost().isStopping()) { return; } logSevere("%s to %s failed with %s :", o.getAction(), o.getUri(), e.toString()); // self DELETE. The client, if they implemented handleRequest on the service instance // will be able to tell something went wrong sendRequest(Operation.createDelete(getUri()) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_NOTIFICATION) .addPragmaDirective(Operation.PRAGMA_DIRECTIVE_SKIPPED_NOTIFICATIONS)); } }
private void checkAndReSubscribe() { if (getHost().isStopping()) { return; } Operation getSubscriptions = this.subscribeOp.clone().setAction(Action.GET); sendRequest(getSubscriptions.setCompletion((o, e) -> { if (e != null) { selfDeleteDueToFailure(o, e); return; } resubscribe(o); })); }
@Override public void handleStart(Operation startPost) { this.subscribeRequest.reference = UriUtils.buildPublicUri(getHost(), getSelfLink()); // we subscribe to the node group associated with our instance. The client should // set the node selector on this service, to match the one on the publisher sendRequest(Operation.createGet(this, getPeerNodeSelectorPath()).setCompletion( (o, e) -> { if (e != null) { startPost.fail(e); return; } NodeSelectorState nss = o.getBody(NodeSelectorState.class); URI callbackUri = getHost().startSubscriptionService( Operation.createPost(this, nss.nodeGroupLink).setReferer(getUri()), this::handleNodeGroupNotification); this.nodeGroupCallbackUri = callbackUri; startPost.complete(); })); }
@Override public void handleStop(Operation op) { // Delete the node group subscription which was created at handleStart. sendRequest(Operation.createGet(this, getPeerNodeSelectorPath()).setCompletion( (o, e) -> { if (e != null) { op.fail(e); return; } NodeSelectorState nss = o.getBody(NodeSelectorState.class); Operation delete = Operation.createDelete(this, nss.nodeGroupLink).setReferer(getUri()); getHost().stopSubscriptionService(delete, this.nodeGroupCallbackUri); op.complete(); })); }