|
3 | 3 | import io.vertx.core.DeploymentOptions; |
4 | 4 | import io.vertx.core.Future; |
5 | 5 | import io.vertx.core.Promise; |
6 | | -import io.vertx.easyerp.microservice.administration.api.RestAdministrationAPIVerticle; |
| 6 | +import io.vertx.core.json.JsonObject; |
| 7 | +import io.vertx.core.logging.Logger; |
| 8 | +import io.vertx.core.logging.LoggerFactory; |
| 9 | +import io.vertx.easyerp.microservice.administration.api.AdministrationRestAPIVerticle; |
7 | 10 | import io.vertx.easyerp.microservice.administration.impl.AdministrationImpl; |
8 | 11 | import io.vertx.easyerp.microservice.common.BaseMicroserviceVerticle; |
| 12 | +import static io.vertx.easyerp.microservice.common.config.ConfigRetrieverHelper.configurationRetriever; |
9 | 13 | import io.vertx.serviceproxy.ProxyHelper; |
10 | 14 |
|
11 | 15 | public class AdministrationVerticle extends BaseMicroserviceVerticle { |
| 16 | + private static final long SCAN_PERIOD = 20000L; |
| 17 | + private static final String PATH = "/conf/vertx.conf"; |
| 18 | + private final static Logger logger = LoggerFactory.getLogger(AdministrationVerticle.class); |
12 | 19 |
|
13 | 20 | @Override |
14 | 21 | public void start(Promise<Void> startPromise) throws Exception { |
15 | 22 | super.start(); |
16 | 23 |
|
17 | | - // create the service instance |
18 | | - AdministrationService service = new AdministrationImpl(vertx, config()); |
19 | | - |
20 | | - // register the service proxy on event bus NB: must change from ProxyHelper to ServiceBinder |
21 | | - ProxyHelper.registerService(AdministrationService.class, vertx, service, service.SERVICE_ADDRESS); |
22 | | - |
23 | | - /* ping database and-then publish the service in the discovery infrastructure |
24 | | - */ |
25 | | - pingPersistence(service) |
26 | | - .compose(databaseOK -> publishEventBusService(service.SERVICE_NAME, service.SERVICE_ADDRESS, AdministrationService.class)) |
27 | | - .compose(servicePublised -> deployRestService(service)) |
28 | | - .onComplete(startPromise); |
| 24 | + //Smuggling the service instance |
| 25 | + loadConfigsAndRegisterService(PATH) |
| 26 | + /* ping database and-then publish the service in the discovery infrastructure |
| 27 | + */ |
| 28 | + .compose(loadOk -> pingPersistence(loadOk)) |
| 29 | + .compose(databaseOK -> publishEventBusService(AdministrationService.SERVICE_NAME, AdministrationService.SERVICE_ADDRESS, databaseOK)) |
| 30 | + .compose(servicePublisedOk -> deployRestService(servicePublisedOk)) |
| 31 | + .onComplete(startPromise); |
29 | 32 | } |
30 | 33 |
|
31 | | - private Future<Void> pingPersistence(AdministrationService service){ |
| 34 | + /** |
| 35 | + * Ping the persistence backend to see if its up |
| 36 | + * |
| 37 | + * @param service instance |
| 38 | + * @return service instance to be used in next stage |
| 39 | + */ |
| 40 | + private Future<AdministrationService> pingPersistence(final AdministrationService service){ |
32 | 41 | Promise<Void> initPromise = Promise.promise(); |
33 | 42 | service.initializePersistence(initPromise); |
34 | 43 | return initPromise.future().map(r->{ |
35 | 44 | ///Perform some init db operations here |
36 | | - return null; |
| 45 | + return service; |
37 | 46 | }); |
38 | 47 |
|
39 | 48 | } |
40 | 49 |
|
| 50 | + /** |
| 51 | + * Deploy the admin microservice |
| 52 | + * |
| 53 | + * @param service instance |
| 54 | + * @return |
| 55 | + */ |
41 | 56 | private Future<Void> deployRestService(AdministrationService service){ |
42 | 57 | Promise<String> promise = Promise.promise(); |
43 | | - vertx.deployVerticle(new RestAdministrationAPIVerticle(service), |
| 58 | + vertx.deployVerticle(new AdministrationRestAPIVerticle(service), |
44 | 59 | new DeploymentOptions().setConfig(config()), |
45 | 60 | promise); |
46 | | - return promise.future().map(r->null); |
| 61 | + return promise.future().map( |
| 62 | + ret -> null |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + |
| 67 | + /** |
| 68 | + * Load configurations, create and register the service for propagation |
| 69 | + * |
| 70 | + * @param path configuration file path |
| 71 | + * @return service instance to be use in next stage |
| 72 | + */ |
| 73 | + private Future<AdministrationService> loadConfigsAndRegisterService(final String path) { |
| 74 | + Promise<JsonObject> initConfigPromise = Promise.promise(); |
| 75 | + |
| 76 | + //get environmental variables using the path provided |
| 77 | + configurationRetriever |
| 78 | + .usingScanPeriod(SCAN_PERIOD) |
| 79 | + .withFileStore(path) |
| 80 | + .createConfig(vertx) |
| 81 | + .onComplete(initConfigPromise); |
| 82 | + return initConfigPromise.future().map(conf -> { |
| 83 | + |
| 84 | + // create the service instance |
| 85 | + final AdministrationService service = new AdministrationImpl(vertx, config().mergeIn(conf)); |
| 86 | + |
| 87 | + // register the service proxy on event bus NB: must change from ProxyHelper to ServiceBinder |
| 88 | + ProxyHelper.registerService(AdministrationService.class, vertx, service, service.SERVICE_ADDRESS); |
| 89 | + |
| 90 | + logger.info("Initializing config:::" + config()); |
| 91 | + return service; |
| 92 | + }); |
47 | 93 | } |
48 | 94 | } |
0 commit comments