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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.apps.uservice.test.adapt.jms;
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;
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.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * @author Liam Fallon (liam.fallon@ericsson.com)
43 public class JMSEventSubscriber implements Runnable {
44 private static final Logger LOGGER = LoggerFactory.getLogger(JMSEventSubscriber.class);
46 private final String topic;
47 private long eventsReceivedCount = 0;
49 private final Thread subscriberThread;
50 private final Connection connection;
53 public JMSEventSubscriber(final String topic, final ConnectionFactory connectionFactory, final String username,
54 final String password) throws JMSException {
56 connection = connectionFactory.createConnection(username, password);
59 subscriberThread = new Thread(this);
60 subscriberThread.start();
65 final Topic jmsTopic = new ActiveMQTopic(topic);
66 try (final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
67 final MessageConsumer jmsConsumer = jmsSession.createConsumer(jmsTopic);) {
69 while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
71 final Message message = jmsConsumer.receive(100);
72 if (message == null) {
76 if (message instanceof ObjectMessage) {
77 final TestPing testPing = (TestPing) ((ObjectMessage) message).getObject();
79 } else if (message instanceof TextMessage) {
80 ((TextMessage) message).getText();
82 throw new ApexEventException("unknowm message \"" + message + "\" of type \""
83 + message.getClass().getCanonicalName() + "\" received");
85 eventsReceivedCount++;
86 } catch (final Exception e) {
91 } catch (final Exception e) {
92 throw new ApexEventRuntimeException("JMS event consumption failed", e);
95 LOGGER.info("{} : event reception completed", this.getClass().getCanonicalName());
98 public long getEventsReceivedCount() {
99 return eventsReceivedCount;
102 public void shutdown() throws JMSException {
103 subscriberThread.interrupt();
105 while (subscriberThread.isAlive()) {
106 ThreadUtilities.sleep(10);
110 LOGGER.info("{} : stopped", this.getClass().getCanonicalName());