9dd58c817163339b6552abe3b20baf7c0099c3f3
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
22
23 import javax.jms.Connection;
24 import javax.jms.ConnectionFactory;
25 import javax.jms.JMSException;
26 import javax.jms.Message;
27 import javax.jms.MessageConsumer;
28 import javax.jms.ObjectMessage;
29 import javax.jms.Session;
30 import javax.jms.TextMessage;
31 import javax.jms.Topic;
32
33 import org.apache.activemq.command.ActiveMQTopic;
34 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
35 import org.onap.policy.apex.service.engine.event.ApexEventException;
36 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
37 import org.onap.policy.apex.testsuites.integration.common.testclasses.PingTestClass;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 /**
42  * The Class JmsEventSubscriber.
43  *
44  * @author Liam Fallon (liam.fallon@ericsson.com)
45  */
46 public class JmsEventSubscriber implements Runnable {
47     private static final Logger LOGGER = LoggerFactory.getLogger(JmsEventSubscriber.class);
48
49     private final String topic;
50     private long eventsReceivedCount = 0;
51
52     private final Thread subscriberThread;
53     private final Connection connection;
54
55     /**
56      * Instantiates a new jms event subscriber.
57      *
58      * @param topic the topic
59      * @param connectionFactory the connection factory
60      * @param username the username
61      * @param password the password
62      * @throws JMSException the JMS exception
63      */
64     public JmsEventSubscriber(final String topic, final ConnectionFactory connectionFactory, final String username,
65                     final String password) throws JMSException {
66         this.topic = topic;
67         connection = connectionFactory.createConnection(username, password);
68         connection.start();
69
70         subscriberThread = new Thread(this);
71         subscriberThread.start();
72     }
73
74     /*
75      * (non-Javadoc)
76      * 
77      * @see java.lang.Runnable#run()
78      */
79     @Override
80     public void run() {
81         final Topic jmsTopic = new ActiveMQTopic(topic);
82         try (final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
83                         final MessageConsumer jmsConsumer = jmsSession.createConsumer(jmsTopic)) {
84
85             while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
86                 try {
87                     final Message message = jmsConsumer.receive(100);
88                     if (message == null) {
89                         continue;
90                     }
91
92                     if (message instanceof ObjectMessage) {
93                         final PingTestClass testPing = (PingTestClass) ((ObjectMessage) message).getObject();
94                         testPing.verify();
95                     } else if (message instanceof TextMessage) {
96                         ((TextMessage) message).getText();
97                     } else {
98                         throw new ApexEventException("unknowm message \"" + message + "\" of type \""
99                                         + message.getClass().getCanonicalName() + "\" received");
100                     }
101                     eventsReceivedCount++;
102                 } catch (final Exception e) {
103                     break;
104                 }
105             }
106
107         } catch (final Exception e) {
108             throw new ApexEventRuntimeException("JMS event consumption failed", e);
109         }
110
111         LOGGER.info("{} : event reception completed", this.getClass().getCanonicalName());
112     }
113
114     /**
115      * Gets the events received count.
116      *
117      * @return the events received count
118      */
119     public long getEventsReceivedCount() {
120         return eventsReceivedCount;
121     }
122
123     /**
124      * Shutdown.
125      *
126      * @throws JMSException the JMS exception
127      */
128     public void shutdown() throws JMSException {
129         subscriberThread.interrupt();
130
131         while (subscriberThread.isAlive()) {
132             ThreadUtilities.sleep(10);
133         }
134
135         connection.close();
136         LOGGER.info("{} : stopped", this.getClass().getCanonicalName());
137     }
138
139 }