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