use of com.codingchili.realm.controller.RealmInstanceHandler in project chili-core by codingchili.
the class Service method deploy.
/**
* Dynamically deploy a new realm, verifies that no existing nodes are already listening
* on the same address by sending a ping.
*
* @param realm the realm to be deployed dynamically.
*/
private void deploy(Future<Void> future, RealmSettings realm) {
Consumer<RealmContext> deployer = (rc) -> {
// Check if the routing id for the realm is unique
context.bus().send(realm.getName(), getPing(), getDeliveryOptions(), response -> {
if (response.failed()) {
// If no response then the id is not already in use.
ListenerSettings settings = rc.getListenerSettings();
CoreListener listener = new WebsocketListener().settings(() -> settings).handler(new RealmClientHandler(rc));
// deploy handler for incoming messages from instances.
rc.handler(() -> new RealmInstanceHandler(rc)).setHandler(instances -> {
if (instances.succeeded()) {
// deploy handler for incoming messages from clients.
rc.listener(() -> listener).setHandler(deploy -> {
if (deploy.failed()) {
rc.onDeployRealmFailure(realm.getName());
throw new RuntimeException(deploy.cause());
}
}).setHandler(clients -> {
if (clients.succeeded()) {
future.complete();
} else {
future.fail(clients.cause());
}
});
} else {
future.fail(instances.cause());
}
});
} else {
future.fail(new RealmNotUniqueException());
}
});
};
// set up the realm context asynchronously.
RealmContext.create(context, realm).setHandler(create -> {
if (create.succeeded()) {
deployer.accept(create.result());
} else {
future.fail(new RuntimeException(create.cause()));
}
});
}
Aggregations