46455f5dfc6efe84de6f157564f26e62c71e1ece
[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.apps.uservice.test.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
38 /**
39  * @author Liam Fallon (liam.fallon@ericsson.com)
40  */
41 public class JMSEventSubscriber implements Runnable {
42     private final String topic;
43     private long eventsReceivedCount = 0;
44
45     private final Thread subscriberThread;
46     private final Connection connection;
47
48
49     public JMSEventSubscriber(final String topic, final ConnectionFactory connectionFactory, final String username,
50             final String password) throws JMSException {
51         this.topic = topic;
52         connection = connectionFactory.createConnection(username, password);
53         connection.start();
54
55         subscriberThread = new Thread(this);
56         subscriberThread.start();
57     }
58
59     @Override
60     public void run() {
61         try {
62             final Topic jmsTopic = new ActiveMQTopic(topic);
63             final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
64             final MessageConsumer jmsConsumer = jmsSession.createConsumer(jmsTopic);
65
66             System.out.println(JMSEventSubscriber.class.getCanonicalName()
67                     + ": receiving events from Kafka server on topic " + topic);
68
69             while (subscriberThread.isAlive() && !subscriberThread.isInterrupted()) {
70                 try {
71                     final Message message = jmsConsumer.receive(100);
72                     if (message == null) {
73                         continue;
74                     }
75
76                     if (message instanceof ObjectMessage) {
77                         final TestPing testPing = (TestPing) ((ObjectMessage) message).getObject();
78                         System.out.println("Received message: " + testPing.toString());
79                         testPing.verify();
80                     } else if (message instanceof TextMessage) {
81                         final String textMessage = ((TextMessage) message).getText();
82                         System.out.println("Received message: " + textMessage);
83                     } else {
84                         throw new ApexEventException("unknowm message \"" + message + "\" of type \""
85                                 + message.getClass().getCanonicalName() + "\" received");
86                     }
87                     eventsReceivedCount++;
88                 } catch (final Exception e) {
89                     break;
90                 }
91             }
92
93             jmsConsumer.close();
94             jmsSession.close();
95         } catch (final Exception e) {
96             throw new ApexEventRuntimeException("JMS event consumption failed", e);
97         }
98
99         System.out.println(JMSEventSubscriber.class.getCanonicalName() + ": event reception completed");
100     }
101
102     public long getEventsReceivedCount() {
103         return eventsReceivedCount;
104     }
105
106     public void shutdown() throws JMSException {
107         subscriberThread.interrupt();
108
109         while (subscriberThread.isAlive()) {
110             ThreadUtilities.sleep(10);
111         }
112
113         connection.close();
114         System.out.println(JMSEventSubscriber.class.getCanonicalName() + ": stopped");
115     }
116
117 }