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