Add start registering service with thread
[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.fasterxml.jackson.databind.ObjectMapper;\r
20 import java.io.IOException;\r
21 import lombok.extern.slf4j.Slf4j;\r
22 import org.apache.commons.lang3.StringUtils;\r
23 import org.apache.http.HttpResponse;\r
24 import org.apache.http.client.methods.HttpPost;\r
25 import org.apache.http.client.methods.HttpRequestBase;\r
26 import org.apache.http.entity.ByteArrayEntity;\r
27 import org.apache.http.impl.client.CloseableHttpClient;\r
28 import org.apache.http.impl.client.HttpClients;\r
29 import org.apache.http.util.EntityUtils;\r
30 import org.jvnet.hk2.annotations.Service;\r
31 import org.openo.holmes.common.api.entity.ServiceRegisterEntity;\r
32 import org.openo.holmes.common.config.MicroServiceConfig;\r
33 import org.openo.holmes.common.constant.AlarmConst;\r
34 \r
35 @Slf4j\r
36 @Service\r
37 public class MSBRegisterUtil {\r
38 \r
39     public void register(ServiceRegisterEntity entity) throws IOException {\r
40         ((Runnable) () -> {\r
41             log.info("start inventory micro service register");\r
42             boolean flag = false;\r
43             int retry = 0;\r
44             while (!flag && retry < 20) {\r
45                 log.info("inventory micro service register.retry:" + retry);\r
46                 retry++;\r
47                 flag = inner_register(entity);\r
48                 if (!flag) {\r
49                     log.warn("micro service register failed, sleep 30S and try again.");\r
50                     threadSleep(30000);\r
51                 } else {\r
52                     log.info("micro service register success!");\r
53                     break;\r
54                 }\r
55             }\r
56             log.info("holmes micro service register end.");\r
57         }).run();\r
58     }\r
59 \r
60     private void setHeader(HttpRequestBase httpRequestBase) {\r
61         httpRequestBase.setHeader("Content-Type", "text/html;charset=UTF-8");\r
62         httpRequestBase.setHeader("Accept", "application/json");\r
63         httpRequestBase.setHeader("Content-Type", "application/json");\r
64     }\r
65 \r
66     private boolean inner_register(ServiceRegisterEntity entity) {\r
67         CloseableHttpClient httpClient = HttpClients.createDefault();\r
68         try {\r
69             ObjectMapper mapper = new ObjectMapper();\r
70             String content = mapper.writeValueAsString(entity);\r
71             HttpPost httpPost = new HttpPost(\r
72                     MicroServiceConfig.getMsbServerAddr() + "/api/microservices/v1/services?createOrUpdate=false");\r
73             if (StringUtils.isNotEmpty(content)) {\r
74                 httpPost.setEntity(new ByteArrayEntity(content.getBytes()));\r
75             }\r
76             this.setHeader(httpPost);\r
77             HttpResponse response;\r
78             try {\r
79                 response = httpClient.execute(httpPost);\r
80             } catch (Exception e) {\r
81                 log.warn("Registering the service to the bus failure", e);\r
82                 return false;\r
83             }\r
84             if (response.getStatusLine().getStatusCode() == AlarmConst.MICRO_SERVICE_STATUS_SUCCESS) {\r
85                 log.info("Registration successful service to the bus :" + EntityUtils.toString(response.getEntity()));\r
86                 return true;\r
87             } else {\r
88                 log.warn(\r
89                         "Registering the service to the bus failure:" + response.getStatusLine().getStatusCode() + " " +\r
90                                 response.getStatusLine().getReasonPhrase());\r
91                 return false;\r
92             }\r
93         } catch (IOException e) {\r
94             log.warn("ServiceRegisterEntity:" + entity + " parse failed",e);\r
95         } finally {\r
96             try {\r
97                 httpClient.close();\r
98             } catch (IOException e) {\r
99                 log.warn("At the time of registering service httpclient close failure",e);\r
100             }\r
101         }\r
102         return false;\r
103     }\r
104 \r
105     private void threadSleep(int second) {\r
106         log.info("start sleep ....");\r
107         try {\r
108             Thread.sleep(second);\r
109         } catch (InterruptedException error) {\r
110             log.error("thread sleep error.errorMsg:" + error.getMessage());\r
111         }\r
112         log.info("sleep end .");\r
113     }\r
114 }