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.plugins.event.carrier.jms;
23 import java.io.Serializable;
24 import java.util.EnumMap;
26 import java.util.Properties;
28 import javax.jms.Connection;
29 import javax.jms.ConnectionFactory;
30 import javax.jms.Message;
31 import javax.jms.MessageProducer;
32 import javax.jms.Session;
33 import javax.jms.Topic;
34 import javax.naming.InitialContext;
36 import org.onap.policy.apex.service.engine.event.ApexEventException;
37 import org.onap.policy.apex.service.engine.event.ApexEventProducer;
38 import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
39 import org.onap.policy.apex.service.engine.event.PeeredReference;
40 import org.onap.policy.apex.service.engine.event.SynchronousEventCache;
41 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
42 import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * Concrete implementation of an Apex event producer that sends events using JMS.
49 * @author Liam Fallon (liam.fallon@ericsson.com)
51 public class ApexJmsProducer implements ApexEventProducer {
52 // Get a reference to the logger
53 private static final Logger LOGGER = LoggerFactory.getLogger(ApexJmsProducer.class);
55 // Recurring string constants
56 private static final String COULD_NOT_SEND_PREFIX = "could not send event \"";
57 private static final String FOR_PRODUCER_TAG = "\" for producer (";
58 private static final String JMS_MESSAGE_PRODUCER_TAG = "\" on JMS message producer ";
60 // The JMS parameters read from the parameter service
61 private JmsCarrierTechnologyParameters jmsProducerProperties;
63 // The connection to the JMS server
64 private Connection connection;
66 // The JMS session on which we will send events
67 private Session jmsSession;
69 // The producer on which we will send events
70 private MessageProducer messageProducer;
72 // The name for this producer
73 private String name = null;
75 // The peer references for this event handler
76 private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
82 public void init(final String producerName, final EventHandlerParameters producerParameters)
83 throws ApexEventException {
84 this.name = producerName;
86 // Check and get the JMS Properties
87 if (!(producerParameters.getCarrierTechnologyParameters() instanceof JmsCarrierTechnologyParameters)) {
88 final String errorMessage =
89 "specified producer properties are not applicable to a JMS producer (" + this.name + ")";
90 LOGGER.warn(errorMessage);
91 throw new ApexEventException(errorMessage);
93 jmsProducerProperties = (JmsCarrierTechnologyParameters) producerParameters.getCarrierTechnologyParameters();
95 // Look up the JMS connection factory
96 InitialContext jmsContext = null;
97 ConnectionFactory connectionFactory = null;
99 jmsContext = new InitialContext(jmsProducerProperties.getJmsProducerProperties());
100 connectionFactory = (ConnectionFactory) jmsContext.lookup(jmsProducerProperties.getConnectionFactory());
102 // Check if we actually got a connection factory
103 if (connectionFactory == null) {
104 throw new IllegalArgumentException(
105 "JMS context lookup of \"" + jmsProducerProperties.getConnectionFactory()
106 + "\" returned null for producer (" + this.name + ")");
108 } catch (final Exception e) {
109 final String errorMessage = "lookup of JMS connection factory \""
110 + jmsProducerProperties.getConnectionFactory() + "\" failed for JMS producer properties \""
111 + jmsProducerProperties.getJmsConsumerProperties() + FOR_PRODUCER_TAG + this.name + ")";
112 LOGGER.warn(errorMessage, e);
113 throw new ApexEventException(errorMessage, e);
116 // Lookup the topic on which we will send events
117 Topic jmsOutgoingTopic;
119 jmsOutgoingTopic = (Topic) jmsContext.lookup(jmsProducerProperties.getProducerTopic());
121 // Check if we actually got a topic
122 if (jmsOutgoingTopic == null) {
123 throw new IllegalArgumentException("JMS context lookup of \"" + jmsProducerProperties.getProducerTopic()
124 + "\" returned null for producer (" + this.name + ")");
126 } catch (final Exception e) {
127 final String errorMessage = "lookup of JMS topic \"" + jmsProducerProperties.getProducerTopic()
128 + "\" failed for JMS producer properties \"" + jmsProducerProperties.getJmsProducerProperties()
129 + FOR_PRODUCER_TAG + this.name + ")";
130 LOGGER.warn(errorMessage, e);
131 throw new ApexEventException(errorMessage, e);
134 // Create and start a connection to the JMS server
136 connection = connectionFactory.createConnection(jmsProducerProperties.getSecurityPrincipal(),
137 jmsProducerProperties.getSecurityCredentials());
139 } catch (final Exception e) {
140 final String errorMessage = "connection to JMS server failed for JMS properties \""
141 + jmsProducerProperties.getJmsConsumerProperties() + FOR_PRODUCER_TAG + this.name + ")";
142 LOGGER.warn(errorMessage, e);
143 throw new ApexEventException(errorMessage, e);
146 // Create a JMS session for sending events
148 jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
149 } catch (final Exception e) {
150 final String errorMessage = "creation of session to JMS server failed for JMS properties \""
151 + jmsProducerProperties.getJmsConsumerProperties() + FOR_PRODUCER_TAG + this.name + ")";
152 LOGGER.warn(errorMessage, e);
153 throw new ApexEventException(errorMessage, e);
156 // Create a JMS message producer for sending events
158 messageProducer = jmsSession.createProducer(jmsOutgoingTopic);
159 } catch (final Exception e) {
160 final String errorMessage =
161 "creation of producer for sending events " + "to JMS server failed for JMS properties \""
162 + jmsProducerProperties.getJmsConsumerProperties() + "\"";
163 LOGGER.warn(errorMessage, e);
164 throw new ApexEventException(errorMessage, e);
172 public String getName() {
180 public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
181 return peerReferenceMap.get(peeredMode);
188 public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
189 peerReferenceMap.put(peeredMode, peeredReference);
196 public void sendEvent(final long executionId, final Properties executionProperties, final String eventname,
197 final Object eventObject) {
198 // Check if this is a synchronized event, if so we have received a reply
199 final SynchronousEventCache synchronousEventCache =
200 (SynchronousEventCache) peerReferenceMap.get(EventHandlerPeeredMode.SYNCHRONOUS);
201 if (synchronousEventCache != null) {
202 synchronousEventCache.removeCachedEventToApexIfExists(executionId);
205 // Check if the object to be sent is serializable
206 if (!Serializable.class.isAssignableFrom(eventObject.getClass())) {
207 final String errorMessage = COULD_NOT_SEND_PREFIX + eventname + JMS_MESSAGE_PRODUCER_TAG + this.name
208 + ", object of type \"" + eventObject.getClass().getCanonicalName() + "\" is not serializable";
209 LOGGER.warn(errorMessage);
210 throw new ApexEventRuntimeException(errorMessage);
213 // The JMS message to send is constructed using the JMS session
214 Message jmsMessage = null;
216 // Check the type of JMS message to send
217 if (jmsProducerProperties.isObjectMessageSending()) {
218 // We should send a JMS Object Message
220 jmsMessage = jmsSession.createObjectMessage((Serializable) eventObject);
221 } catch (final Exception e) {
222 final String errorMessage = COULD_NOT_SEND_PREFIX + eventname + JMS_MESSAGE_PRODUCER_TAG + this.name
223 + ", could not create JMS Object Message for object \"" + eventObject;
224 LOGGER.warn(errorMessage, e);
225 throw new ApexEventRuntimeException(errorMessage);
228 // We should send a JMS Text Message
230 jmsMessage = jmsSession.createTextMessage(eventObject.toString());
231 } catch (final Exception e) {
232 final String errorMessage = COULD_NOT_SEND_PREFIX + eventname + JMS_MESSAGE_PRODUCER_TAG + this.name
233 + ", could not create JMS Text Message for object \"" + eventObject;
234 LOGGER.warn(errorMessage, e);
235 throw new ApexEventRuntimeException(errorMessage);
240 messageProducer.send(jmsMessage);
241 } catch (final Exception e) {
242 final String errorMessage = COULD_NOT_SEND_PREFIX + eventname + JMS_MESSAGE_PRODUCER_TAG + this.name
243 + ", send failed for object \"" + eventObject;
244 LOGGER.warn(errorMessage, e);
245 throw new ApexEventRuntimeException(errorMessage);
254 // Close the message producer
256 messageProducer.close();
257 } catch (final Exception e) {
258 final String errorMessage = "failed to close JMS message producer " + this.name + " for sending messages";
259 LOGGER.warn(errorMessage, e);
265 } catch (final Exception e) {
266 final String errorMessage = "failed to close the JMS session for " + this.name + " for sending messages";
267 LOGGER.warn(errorMessage, e);
270 // Close the connection to the JMS server
273 } catch (final Exception e) {
274 final String errorMessage = "close of connection to the JMS server for " + this.name + " failed";
275 LOGGER.warn(errorMessage, e);