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