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.MessageProducer;
28 import javax.jms.Session;
29 import javax.jms.Topic;
31 import org.apache.activemq.command.ActiveMQTopic;
32 import org.onap.policy.apex.apps.uservice.test.adapt.events.EventGenerator;
33 import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
34 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class JMSEventProducer implements Runnable {
42 private static final Logger LOGGER = LoggerFactory.getLogger(JMSEventProducer.class);
44 private final String topic;
45 private final int eventCount;
46 private final boolean sendObjects;
47 private final long eventInterval;
48 private long eventsSentCount = 0;
50 private final Thread producerThread;
51 private boolean sendEventsFlag = false;
52 private boolean stopFlag = false;
53 private final Connection connection;
55 public JMSEventProducer(final String topic, final ConnectionFactory connectionFactory, final String username,
56 final String password, final int eventCount, final boolean sendObjects, final long eventInterval)
59 this.eventCount = eventCount;
60 this.sendObjects = sendObjects;
61 this.eventInterval = eventInterval;
62 connection = connectionFactory.createConnection(username, password);
65 producerThread = new Thread(this);
66 producerThread.start();
71 final Topic jmsTopic = new ActiveMQTopic(topic);
72 try (final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
73 final MessageProducer jmsProducer = jmsSession.createProducer(jmsTopic);) {
75 while (producerThread.isAlive() && !stopFlag) {
76 ThreadUtilities.sleep(50);
79 sendEventsToTopic(jmsSession, jmsProducer);
80 sendEventsFlag = false;
84 } catch (final Exception e) {
85 throw new ApexEventRuntimeException("JMS event consumption failed", e);
89 public void sendEvents() {
90 sendEventsFlag = true;
93 private void sendEventsToTopic(final Session jmsSession, final MessageProducer jmsProducer) throws JMSException {
95 LOGGER.info("{} : sending events to JMS server, event count {}", this.getClass().getCanonicalName(),
98 for (int i = 0; i < eventCount; i++) {
99 ThreadUtilities.sleep(eventInterval);
101 Message jmsMessage = null;
103 jmsMessage = jmsSession.createObjectMessage(new TestPing());
105 jmsMessage = jmsSession.createTextMessage(EventGenerator.jsonEvent());
107 jmsProducer.send(jmsMessage);
110 LOGGER.info("{} : completed, number of events sent", this.getClass().getCanonicalName(), eventsSentCount);
113 public long getEventsSentCount() {
114 return eventsSentCount;
117 public void shutdown() {
118 LOGGER.info("{} : stopping", this.getClass().getCanonicalName());
121 while (producerThread.isAlive()) {
122 ThreadUtilities.sleep(10);
124 LOGGER.info("{} : stopped", this.getClass().getCanonicalName());