e18a15ccf02586af6b597dd60ab49551c9415eea
[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.glassfish.jersey.client.ClientConfig;\r
23 import org.jvnet.hk2.annotations.Service;\r
24 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
25 import org.openo.holmes.common.config.MicroServiceConfig;\r
26 import org.openo.holmes.common.msb.MicroserviceBusRest;\r
27 \r
28 @Slf4j\r
29 @Service\r
30 public class MSBRegisterUtil {\r
31 \r
32     public void register(ServiceRegisterEntity entity) throws IOException {\r
33         log.info("start holmes micro service register");\r
34         boolean flag = false;\r
35         int retry = 0;\r
36         while (!flag && retry < 20) {\r
37             log.info("Holmes microservice register. retry:" + retry);\r
38             retry++;\r
39             flag = inner_register(entity);\r
40             if (!flag) {\r
41                 log.warn("micro service register failed, sleep 30S and try again.");\r
42                 threadSleep(30000);\r
43             } else {\r
44                 log.info("micro service register success!");\r
45                 break;\r
46             }\r
47         }\r
48         log.info("holmes micro service register end.");\r
49     }\r
50 \r
51     private boolean inner_register(ServiceRegisterEntity entity) {\r
52         ClientConfig config = new ClientConfig();\r
53         try {\r
54             log.info("msbServerAddr:" + MicroServiceConfig.getMsbServerAddr());\r
55             log.info("entity:" + entity);\r
56             MicroserviceBusRest resourceserviceproxy = ConsumerFactory.createConsumer(\r
57                     MicroServiceConfig.getMsbServerAddr(), config, MicroserviceBusRest.class);\r
58             resourceserviceproxy.registerServce("false", entity);\r
59         } catch (Exception error) {\r
60             log.error("microservice register failed!" + error.getMessage());\r
61             return false;\r
62         }\r
63         return true;\r
64     }\r
65 \r
66     private void threadSleep(int second) {\r
67         log.info("start sleep ....");\r
68         try {\r
69             Thread.sleep(second);\r
70         } catch (InterruptedException error) {\r
71             log.error("thread sleep error.errorMsg:" + error.getMessage());\r
72         }\r
73         log.info("sleep end .");\r
74     }\r
75 }