1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
|
/*-
* ============LICENSE_START=======================================================
* Copyright (C) 2016-2018 Ericsson. All rights reserved.
* ================================================================================
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
* ============LICENSE_END=========================================================
*/
package org.onap.policy.apex.plugins.event.carrier.jms;
import java.util.EnumMap;
import java.util.Map;
import javax.jms.Connection;
import javax.jms.ConnectionFactory;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageListener;
import javax.jms.Session;
import javax.jms.Topic;
import javax.naming.InitialContext;
import org.onap.policy.apex.core.infrastructure.threading.ApplicationThreadFactory;
import org.onap.policy.apex.core.infrastructure.threading.ThreadUtilities;
import org.onap.policy.apex.service.engine.event.ApexEventConsumer;
import org.onap.policy.apex.service.engine.event.ApexEventException;
import org.onap.policy.apex.service.engine.event.ApexEventReceiver;
import org.onap.policy.apex.service.engine.event.ApexEventRuntimeException;
import org.onap.policy.apex.service.engine.event.PeeredReference;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerParameters;
import org.onap.policy.apex.service.parameters.eventhandler.EventHandlerPeeredMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class implements an Apex event consumer that receives events using JMS.
*
* @author Liam Fallon (liam.fallon@ericsson.com)
*/
public class ApexJmsConsumer implements MessageListener, ApexEventConsumer, Runnable {
// Get a reference to the logger
private static final Logger LOGGER = LoggerFactory.getLogger(ApexJmsConsumer.class);
// The Apex and JMS parameters read from the parameter service
private JmsCarrierTechnologyParameters jmsConsumerProperties;
// The event receiver that will receive events from this consumer
private ApexEventReceiver eventReceiver;
// The consumer thread and stopping flag
private Thread consumerThread;
private boolean stopOrderedFlag = false;
// The connection to the JMS server
private Connection connection;
// The topic on which we receive events from JMS
private Topic jmsIncomingTopic;
// The name for this consumer
private String name = null;
// The peer references for this event handler
private Map<EventHandlerPeeredMode, PeeredReference> peerReferenceMap = new EnumMap<>(EventHandlerPeeredMode.class);
@Override
public void init(final String consumerName, final EventHandlerParameters consumerParameters,
final ApexEventReceiver incomingEventReceiver) throws ApexEventException {
this.eventReceiver = incomingEventReceiver;
this.name = consumerName;
// Check and get the JMS Properties
if (!(consumerParameters.getCarrierTechnologyParameters() instanceof JmsCarrierTechnologyParameters)) {
final String errorMessage = "specified consumer properties of type \""
+ consumerParameters.getCarrierTechnologyParameters().getClass().getCanonicalName()
+ "\" are not applicable to a JMS consumer";
LOGGER.warn(errorMessage);
throw new ApexEventException(errorMessage);
}
jmsConsumerProperties = (JmsCarrierTechnologyParameters) consumerParameters.getCarrierTechnologyParameters();
// Look up the JMS connection factory
InitialContext jmsContext = null;
ConnectionFactory connectionFactory = null;
try {
jmsContext = new InitialContext(jmsConsumerProperties.getJmsConsumerProperties());
connectionFactory = (ConnectionFactory) jmsContext.lookup(jmsConsumerProperties.getConnectionFactory());
// Check if we actually got a connection factory
if (connectionFactory == null) {
throw new IllegalArgumentException(
"JMS context lookup of \"" + jmsConsumerProperties.getConnectionFactory() + "\" returned null");
}
} catch (final Exception e) {
final String errorMessage = "lookup of JMS connection factory \""
+ jmsConsumerProperties.getConnectionFactory() + "\" failed for JMS consumer properties \""
+ jmsConsumerProperties.getJmsConsumerProperties() + "\"";
LOGGER.warn(errorMessage, e);
throw new ApexEventException(errorMessage, e);
}
// Lookup the topic on which we will receive events
try {
jmsIncomingTopic = (Topic) jmsContext.lookup(jmsConsumerProperties.getConsumerTopic());
// Check if we actually got a topic
if (jmsIncomingTopic == null) {
throw new IllegalArgumentException(
"JMS context lookup of \"" + jmsConsumerProperties.getConsumerTopic() + "\" returned null");
}
} catch (final Exception e) {
final String errorMessage = "lookup of JMS topic \"" + jmsConsumerProperties.getConsumerTopic()
+ "\" failed for JMS consumer properties \"" + jmsConsumerProperties.getJmsConsumerProperties()
+ "\"";
LOGGER.warn(errorMessage, e);
throw new ApexEventException(errorMessage, e);
}
// Create and start a connection to the JMS server
try {
connection = connectionFactory.createConnection(jmsConsumerProperties.getSecurityPrincipal(),
jmsConsumerProperties.getSecurityCredentials());
connection.start();
} catch (final Exception e) {
final String errorMessage = "connection to the JMS server failed for JMS properties \""
+ jmsConsumerProperties.getJmsConsumerProperties() + "\"";
LOGGER.warn(errorMessage, e);
throw new ApexEventException(errorMessage, e);
}
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#start()
*/
@Override
public void start() {
// Configure and start the event reception thread
final String threadName = this.getClass().getName() + ":" + this.name;
consumerThread = new ApplicationThreadFactory(threadName).newThread(this);
consumerThread.setDaemon(true);
consumerThread.start();
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#getName()
*/
@Override
public String getName() {
return name;
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#getPeeredReference(org.onap.policy.apex.service.
* parameters. eventhandler.EventHandlerPeeredMode)
*/
@Override
public PeeredReference getPeeredReference(final EventHandlerPeeredMode peeredMode) {
return peerReferenceMap.get(peeredMode);
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.service.engine.event.ApexEventConsumer#setPeeredReference(org.onap.policy.apex.service.
* parameters. eventhandler.EventHandlerPeeredMode, org.onap.policy.apex.service.engine.event.PeeredReference)
*/
@Override
public void setPeeredReference(final EventHandlerPeeredMode peeredMode, final PeeredReference peeredReference) {
peerReferenceMap.put(peeredMode, peeredReference);
}
/*
* (non-Javadoc)
*
* @see java.lang.Runnable#run()
*/
@Override
public void run() {
// JMS session and message consumer for receiving messages
try (final Session jmsSession = connection.createSession(false, Session.AUTO_ACKNOWLEDGE)) {
// Create a message consumer for reception of messages and set this class as a message listener
createMessageConsumer(jmsSession);
} catch (final Exception e) {
final String errorMessage = "failed to create a JMS session towards the JMS server for receiving messages";
LOGGER.warn(errorMessage, e);
throw new ApexEventRuntimeException(errorMessage, e);
}
// Everything is now set up
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("event receiver " + this.getClass().getName() + ":" + this.name + " subscribed to JMS topic: "
+ jmsConsumerProperties.getConsumerTopic());
}
}
/**
* The helper function to create a message consumer from a given JMS session.
*
* @param jmsSession a JMS session
*/
private void createMessageConsumer(final Session jmsSession) {
try (final MessageConsumer messageConsumer = jmsSession.createConsumer(jmsIncomingTopic)) {
messageConsumer.setMessageListener(this);
// The endless loop that receives events over JMS
while (consumerThread.isAlive() && !stopOrderedFlag) {
ThreadUtilities.sleep(jmsConsumerProperties.getConsumerWaitTime());
}
} catch (final Exception e) {
final String errorMessage = "failed to create a JMS message consumer for receiving messages";
LOGGER.warn(errorMessage, e);
throw new ApexEventRuntimeException(errorMessage, e);
}
}
/*
* (non-Javadoc)
*
* @see javax.jms.MessageListener#onMessage(javax.jms.Message)
*/
@Override
public void onMessage(final Message jmsMessage) {
try {
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("event received for {} for forwarding to Apex engine : {} {}",
this.getClass().getName() + ":" + this.name, jmsMessage.getJMSMessageID(),
jmsMessage.getJMSType());
}
eventReceiver.receiveEvent(jmsMessage);
} catch (final Exception e) {
final String errorMessage = "failed to receive message from JMS";
LOGGER.warn(errorMessage, e);
throw new ApexEventRuntimeException(errorMessage, e);
}
}
/*
* (non-Javadoc)
*
* @see org.onap.policy.apex.apps.uservice.producer.ApexEventProducer#stop()
*/
@Override
public void stop() {
stopOrderedFlag = true;
while (consumerThread.isAlive()) {
ThreadUtilities.sleep(jmsConsumerProperties.getConsumerWaitTime());
}
// Close the connection to the JMS server
try {
if (connection != null) {
connection.close();
}
} catch (final Exception e) {
final String errorMessage = "close of connection to the JMS server failed";
LOGGER.warn(errorMessage, e);
}
}
}
|