2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.event;
23 import org.onap.aai.event.api.EventConsumer;
25 import org.apache.camel.Exchange;
26 import org.apache.camel.Message;
27 import org.apache.camel.Processor;
28 import org.apache.camel.support.ScheduledPollConsumer;
29 import org.onap.aai.logging.RouterCoreMsgs;
30 import org.onap.aai.cl.api.Logger;
31 import org.onap.aai.cl.eelf.LoggerFactory;
32 import org.onap.aai.cl.mdc.MdcContext;
34 import java.util.UUID;
35 import java.util.concurrent.ScheduledThreadPoolExecutor;
38 * The consumer component which is used to pull messages off of the event bus and send them to the
39 * next processor in the route chain. This type of consumer is based off of a scheduled poller so
40 * that events are pulled on a regular basis.
42 public class EventBusConsumer extends ScheduledPollConsumer {
44 private Logger logger = LoggerFactory.getInstance().getLogger(EventBusConsumer.class);
45 private Logger auditLogger = LoggerFactory.getInstance().getAuditLogger(EventBusConsumer.class);
46 private final AbstractEventBusEndpoint endpoint;
48 private EventConsumer consumer;
51 * EventBusConsumer Constructor.
53 public EventBusConsumer(AbstractEventBusEndpoint endpoint, Processor processor, EventConsumer consumer) {
54 super(endpoint, processor);
55 super.setDelay(endpoint.getPollingDelay());
56 this.endpoint = endpoint;
58 setScheduledExecutorService(new ScheduledThreadPoolExecutor(endpoint.getPoolSize()));
60 this.consumer = consumer;
64 * Method which is called by the Camel process on a scheduled basis. This specific implementation
65 * reads messages off of the configured topic and schedules tasks to process them .
67 * @return the number of messages that were processed off the event queue
70 protected int poll() throws Exception {
72 String topic = endpoint.getEventTopic();
74 logger.debug("Checking for event on topic: " + topic);
79 Iterable<String> messages = consumer.consumeAndCommit();
81 for (String message : messages) {
82 Exchange exchange = endpoint.createExchange();
83 exchange.getIn().setBody(message);
84 getScheduledExecutorService().submit(new EventProcessor(exchange, topic));
87 } catch (Exception e) {
88 logger.error(RouterCoreMsgs.EVENT_PROCESSING_EXCEPTION, e, e.getLocalizedMessage());
91 logger.debug(RouterCoreMsgs.PROCESS_EVENT, topic, Integer.toString(processCount));
96 protected void doStop() throws Exception {
98 if (endpoint != null) {
103 protected void doShutdown() throws Exception {
105 if (endpoint != null) {
111 * Class responsible for processing messages pulled off of the event bus.
113 private class EventProcessor implements Runnable {
115 private Exchange message;
117 private String topic;
119 EventProcessor(Exchange message, String topic) {
120 this.message = message;
127 MdcContext.initialize(UUID.randomUUID().toString(), "DataRouter", "", "Event-Bus", "");
129 // Sends the message to the next processor in the defined Camel route
130 getProcessor().process(message);
132 Message response = message.getOut();
133 if (response != null) {
134 logger.debug("Routing response: " + response.getBody());
137 } catch (Exception e) {
138 logger.error(RouterCoreMsgs.EVENT_PROCESSING_EXCEPTION,e,e.getLocalizedMessage());
140 // log exception if an exception occurred and was not handled
141 if (message.getException() != null) {
142 logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
143 auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "FAILURE");
145 logger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");
146 auditLogger.info(RouterCoreMsgs.PROCESS_EVENT, topic, "SUCCESS");