2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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
10 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.testsuites.integration.uservice.adapt.jms;
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 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;
42 * The Class JmsEventSubscriber.
44 * @author Liam Fallon (liam.fallon@ericsson.com)
46 public class JmsEventSubscriber implements Runnable {
47 private static final Logger LOGGER = LoggerFactory.getLogger(JmsEventSubscriber.class);
49 private final String topic;
50 private long eventsReceivedCount = 0;
52 private final Thread subscriberThread;
53 private final Connection connection;
56 * Instantiates a new jms event subscriber.
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
64 public JmsEventSubscriber(final String topic, final ConnectionFactory connectionFactory, final String username,
65 final String password) throws JMSException {
67 connection = connectionFactory.createConnection(username, password);
70 subscriberThread = new Thread(this);
71 subscriberThread.start();
79 final Topic jmsTopic = new ActiveMQTopic(topic);
80 try (final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
81 final MessageConsumer jmsConsumer = jmsSession.createConsumer(jmsTopic)) {
83 while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
85 final Message message = jmsConsumer.receive(100);
86 if (message == null) {
90 if (message instanceof ObjectMessage) {
91 final PingTestClass testPing = (PingTestClass) ((ObjectMessage) message).getObject();
93 } else if (message instanceof TextMessage) {
94 ((TextMessage) message).getText();
96 throw new ApexEventException("unknowm message \"" + message + "\" of type \""
97 + message.getClass().getName() + "\" received");
99 eventsReceivedCount++;
100 } catch (final Exception e) {
101 if (!(e.getCause() instanceof InterruptedException)) {
102 throw new ApexEventRuntimeException("JMS message reception failed", e);
107 } catch (final Exception e) {
108 throw new ApexEventRuntimeException("JMS event consumption failed", e);
111 LOGGER.info("{} : event reception completed, {} events received", this.getClass().getName(),
112 eventsReceivedCount);
116 * Gets the events received count.
118 * @return the events received count
120 public long getEventsReceivedCount() {
121 return eventsReceivedCount;
127 * @throws JMSException the JMS exception
129 public void shutdown() throws JMSException {
130 LOGGER.info("{} : stopping...", this.getClass().getName());
132 subscriberThread.interrupt();
134 while (subscriberThread.isAlive()) {
135 ThreadUtilities.sleep(10);
139 LOGGER.info("{} : stopped", this.getClass().getName());