2fe38174af7c12025f03562d0eccefba019bb8e7
[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] and others.
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   @Override
28   public void run() {
29     LOG.info("start wso2bpelEntity microservice register");
30     boolean flag = false;
31     int retry = 0;
32     while (!flag && retry < 1000) {
33       LOG.info("wso2bpel microservice register.retry:" + retry);
34       retry++;
35       flag = MicroserviceBusConsumer.registerService(wso2bpelEntity);
36       if (flag == false) {
37         LOG.warn("microservice register failed, sleep 30S and try again.");
38         threadSleep(30000);
39       } else {
40         LOG.info("microservice register success!");
41         break;
42       }
43     }
44     LOG.info("wso2bpel microservice register end.");
45   }
46
47   private void threadSleep(int second) {
48     LOG.info("start sleep ....");
49     try {
50       Thread.sleep(second);
51     } catch (InterruptedException error) {
52       LOG.error("thread sleep error.errorMsg:" + error.getMessage());
53     }
54     LOG.info("sleep end .");
55   }
56
57   private void initServiceEntity() {
58     wso2bpelEntity.setServiceName("wso2bpel");
59     wso2bpelEntity.setProtocol("REST");
60     wso2bpelEntity.setVersion("v1");
61     wso2bpelEntity.setUrl("/openoapi/wso2bpel/v1");
62     wso2bpelEntity.setSingleNode(null, "8101", 0);
63     wso2bpelEntity.setVisualRange("1");
64   }
65
66 }