//String host = getRedisHost();
final RedisClient client = RedisClient.create(vertx, new RedisOptions().setHost(host).setPort(6379));
client.subscribe("channelForServerToPublish", handler -> {
if(handler.succeeded()) {
System.out.println(" ServiceOne Subscribed to channel name channelForServerToPublish ");
}
});
vertx.eventBus().consumer("io.vertx.redis.channelForServerToPublish", received -> {
System.out.println("Recevied message: " + received.body());
JsonObject jsonObject = (JsonObject) received.body();
if(jsonObject.getString("status").equals("ok") && jsonObject.getJsonObject("value").getString("message").equals("ServiceOne"))
{
client.get("request", handler -> {
if(handler.succeeded()) {
System.out.println("ServiceOne got msg from Redis Server successfully as :" + handler.result());
//received.reply(new JsonObject().put("responseCode", "OK").put("message", "This is a passed response "));
//frame json object here and set it to redis server and publish notification on channel so that
//server will get notified that response is there in redis server
JsonObject requestJson = new JsonObject(handler.result());
JsonObject response = new JsonObject().put("status", "ok").put("statusCode", 200)
.put("body", new JsonObject().put("key1", "value1").put("key2", "value2")).
put("uuid", requestJson.getString("uuid"));
System.out.println("Setting UUID in response : " + response.getString("uuid"));
client.set("response", response.toString(), responseSetHandler -> {
if(responseSetHandler.succeeded()) {
client.publish("channelForClientToPublish", "ResponseNotification", res -> {
System.out.println(" ServiceOne is going to publish msg on channel so that Server will get notified ");
if (res.succeeded()) {
// it here the last part is printing the channel number e.g. 1
System.out.println("ServerOne Publish msg successfully on channel : " + res.result().toString());
}if(res.failed()) {
System.out.println("ServiceOne failed to publish msg on channel");
}
});
}
if(responseSetHandler.failed()) {
System.out.println(" Service1 failed set the response to Redis server with reason : " + responseSetHandler.cause().getMessage());