Add serviceIp property which is used to config wso2-ext microservice ipaddress
[vfc/nfvo/wfengine.git] / wso2bpel-ext / wso2bpel-core / wso2bpel-mgr / src / main / java / org / openo / carbon / bpel / common / ServiceRegistrer.java
1 /**
2  * Copyright 2016 ZTE Corporation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.openo.carbon.bpel.common;
17
18 import org.openo.carbon.bpel.externalservice.entity.ServiceRegisterEntity;
19 import org.openo.carbon.bpel.externalservice.msb.MicroserviceBusConsumer;
20 import org.slf4j.Logger;
21 import org.slf4j.LoggerFactory;
22
23 public class ServiceRegistrer implements Runnable {
24   private static final Logger LOG = LoggerFactory.getLogger(ServiceRegistrer.class);
25   private final ServiceRegisterEntity wso2bpelEntity = new ServiceRegisterEntity();
26
27   public ServiceRegistrer() {
28     initServiceEntity();
29   }
30
31   @Override
32   public void run() {
33     LOG.info("start wso2bpelEntity microservice register");
34     boolean flag = false;
35     int retry = 0;
36     while (!flag && retry < 1000) {
37       LOG.info("wso2bpel microservice register.retry:" + retry);
38       retry++;
39       flag = MicroserviceBusConsumer.registerService(wso2bpelEntity);
40       if (flag == false) {
41         LOG.warn("microservice register failed, sleep 30S and try again.");
42         threadSleep(30000);
43       } else {
44         LOG.info("microservice register success!");
45         break;
46       }
47     }
48     LOG.info("wso2bpel microservice register end.");
49   }
50
51   private void threadSleep(int second) {
52     LOG.info("start sleep ....");
53     try {
54       Thread.sleep(second);
55     } catch (InterruptedException error) {
56       LOG.error("thread sleep error.errorMsg:" + error.getMessage());
57     }
58     LOG.info("sleep end .");
59   }
60
61   private void initServiceEntity() {
62     wso2bpelEntity.setServiceName("wso2bpel");
63     wso2bpelEntity.setProtocol("REST");
64     wso2bpelEntity.setVersion("v1");
65     wso2bpelEntity.setUrl("/openoapi/wso2bpel/v1");
66     wso2bpelEntity.setSingleNode(Config.getConfigration().getServiceIp(), "8101", 0);
67     wso2bpelEntity.setVisualRange("1");
68   }
69
70 }