Modify the MSB Registration Logic
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / utils / MSBRegisterUtil.java
1 /**\r
2  * Copyright 2017 ZTE Corporation.\r
3  *\r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  */\r
16 \r
17 package org.openo.holmes.common.utils;\r
18 \r
19 import com.eclipsesource.jaxrs.consumer.ConsumerFactory;\r
20 import java.io.IOException;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.apache.http.client.methods.HttpRequestBase;\r
23 import org.glassfish.jersey.client.ClientConfig;\r
24 import org.jvnet.hk2.annotations.Service;\r
25 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
26 import org.openo.holmes.common.config.MicroServiceConfig;\r
27 import org.openo.holmes.common.msb.MicroserviceBusRest;\r
28 \r
29 @Slf4j\r
30 @Service\r
31 public class MSBRegisterUtil {\r
32 \r
33     public void register(ServiceRegisterEntity entity) throws IOException {\r
34         log.info("start holmes micro service register");\r
35         boolean flag = false;\r
36         int retry = 0;\r
37         while (!flag && retry < 20) {\r
38             log.info("Holmes microservice register. retry:" + retry);\r
39             retry++;\r
40             flag = inner_register(entity);\r
41             if (!flag) {\r
42                 log.warn("micro service register failed, sleep 30S and try again.");\r
43                 threadSleep(30000);\r
44             } else {\r
45                 log.info("micro service register success!");\r
46                 break;\r
47             }\r
48         }\r
49         log.info("holmes micro service register end.");\r
50     }\r
51 \r
52     private boolean inner_register(ServiceRegisterEntity entity) {\r
53         ClientConfig config = new ClientConfig();\r
54         try {\r
55             MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
56                     MicroServiceConfig.getMsbServerAddr(), config, MicroserviceBusRest.class);\r
57             resourceserviceproxy.registerServce("false", entity);\r
58         } catch (Exception error) {\r
59             log.error("microservice register failed!" + error.getMessage());\r
60             return false;\r
61         }\r
62         return true;\r
63     }\r
64 \r
65     private void threadSleep(int second) {\r
66         log.info("start sleep ....");\r
67         try {\r
68             Thread.sleep(second);\r
69         } catch (InterruptedException error) {\r
70             log.error("thread sleep error.errorMsg:" + error.getMessage());\r
71         }\r
72         log.info("sleep end .");\r
73     }\r
74 }