Fix aciton module code
[holmes/common.git] / holmes-actions / src / main / java / org / openo / holmes / common / producer / MQProducer.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 package org.openo.holmes.common.producer;\r
17 \r
18 import javax.inject.Inject;\r
19 import javax.jms.Connection;\r
20 import javax.jms.ConnectionFactory;\r
21 import javax.jms.Destination;\r
22 import javax.jms.JMSException;\r
23 import javax.jms.MessageProducer;\r
24 import javax.jms.ObjectMessage;\r
25 import javax.jms.Session;\r
26 import lombok.extern.slf4j.Slf4j;\r
27 import org.glassfish.hk2.api.IterableProvider;\r
28 import org.jvnet.hk2.annotations.Service;\r
29 import org.openo.holmes.common.api.entity.CorrelationResult;\r
30 import org.openo.holmes.common.api.stat.Alarm;\r
31 import org.openo.holmes.common.api.stat.AplusResult;\r
32 import org.openo.holmes.common.config.MQConfig;\r
33 import org.openo.holmes.common.constant.AlarmConst;\r
34 \r
35 @Service\r
36 @Slf4j\r
37 public class MQProducer {\r
38 \r
39     @Inject\r
40     private IterableProvider<MQConfig> mqConfigProvider;\r
41     private ConnectionFactory connectionFactory;\r
42 \r
43     public void sendAlarmMQTopicMsg(Alarm alarm) {\r
44 \r
45         Connection connection = null;\r
46         Session session;\r
47         Destination destination;\r
48         MessageProducer messageProducer;\r
49         try {\r
50             connection = connectionFactory.createConnection();\r
51             connection.start();\r
52             session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);\r
53             destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARM);\r
54             messageProducer = session.createProducer(destination);\r
55             ObjectMessage message = session.createObjectMessage(alarm);\r
56             messageProducer.send(message);\r
57             session.commit();\r
58         } catch (Exception e) {\r
59             log.error("Failed send alarm." + e.getMessage(), e);\r
60         } finally {\r
61             if (connection != null) {\r
62                 try {\r
63                     connection.close();\r
64                 } catch (JMSException e) {\r
65                     log.error("Failed close connection." + e.getMessage(), e);\r
66                 }\r
67             }\r
68         }\r
69     }\r
70 \r
71     public void sendCorrelationMQTopicMsg(String ruleId, long createTimeL, Alarm parentAlarm,\r
72             Alarm childAlarm) {\r
73 \r
74         CorrelationResult correlationResult = new CorrelationResult();\r
75         correlationResult.setRuleId(ruleId);\r
76         correlationResult.setCreateTimeL(createTimeL);\r
77         correlationResult.setResultType(AplusResult.APLUS_CORRELATION);\r
78         correlationResult.setAffectedAlarms(new Alarm[]{parentAlarm, childAlarm});\r
79 \r
80         Connection connection = null;\r
81         Session session;\r
82         Destination destination;\r
83         MessageProducer messageProducer;\r
84 \r
85         try {\r
86             connection = connectionFactory.createConnection();\r
87             connection.start();\r
88             session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);\r
89             destination = session.createTopic(AlarmConst.MQ_TOPIC_NAME_ALARMS_CORRELATION);\r
90             messageProducer = session.createProducer(destination);\r
91             ObjectMessage message = session.createObjectMessage(correlationResult);\r
92             messageProducer.send(message);\r
93             session.commit();\r
94         } catch (Exception e) {\r
95             log.error("Failed send correlation." + e.getMessage(), e);\r
96         } finally {\r
97             if (connection != null) {\r
98                 try {\r
99                     connection.close();\r
100                 } catch (JMSException e) {\r
101                     log.error("Failed close connection." + e.getMessage(), e);\r
102                 }\r
103             }\r
104         }\r
105     }\r
106 }\r