2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights
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=========================================================
22 package org.openecomp.appc.listener.CL.impl;
24 import java.text.DateFormat;
25 import java.text.SimpleDateFormat;
26 import java.util.Date;
27 import java.util.List;
28 import java.util.TimeZone;
29 import java.util.concurrent.RejectedExecutionException;
31 import org.openecomp.appc.listener.AbstractListener;
32 import org.openecomp.appc.listener.ListenerProperties;
33 import org.openecomp.appc.listener.CL.model.IncomingMessage;
34 import org.openecomp.appc.listener.CL.model.Status;
36 import com.att.eelf.configuration.EELFLogger;
37 import com.att.eelf.configuration.EELFManager;
38 import com.att.eelf.i18n.EELFResourceManager;
40 public class ListenerImpl extends AbstractListener {
42 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
44 private long startTime = 0;
46 public ListenerImpl(ListenerProperties props) {
48 String url = props.getProperty("provider.url");
49 LOG.info("DMaaP Provider Endpoint: " + url);
50 ProviderOperations.setUrl(url);
53 String user = props.getProperty("provider.user");
54 String pass = props.getProperty("provider.pass");
55 ProviderOperations.setAuthentication(user, pass);
60 // Some vars for benchmarking
61 startTime = System.currentTimeMillis();
63 LOG.info("Running DMaaP Listener");
66 // Only update if the queue is low. otherwise we read in more
67 // messages than we need
69 if (executor.getQueue().size() <= QUEUED_MIN) {
70 LOG.debug("DMaaP queue running low. Querying for more jobs");
71 List<IncomingMessage> messages = dmaap.getIncomingEvents(IncomingMessage.class, QUEUED_MAX);
72 LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
73 for (IncomingMessage incoming : messages) {
74 // Acknowledge that we read the event
75 LOG.info("Acknowledging Message: " + incoming.getId());
76 dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
78 for (IncomingMessage incoming : messages) {
79 // Add to pool if still running
81 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getId()));
82 if (incoming.isValid()) {
84 executor.execute(new WorkerImpl(incoming, dmaap));
85 } catch (RejectedExecutionException rejectEx) {
86 LOG.error("Task Rejected: ", rejectEx);
89 // Badly formed message
90 LOG.error("Message was not valid. Rejecting");
93 LOG.info("Run stopped. Orphaning Message: " + incoming.getId());
97 } catch (Exception e) {
98 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
99 LOG.error(EELFResourceManager.format(e));
100 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
104 LOG.info("Stopping DMaaP Listener thread");
106 // We've told the listener to stop
108 // 1) Put a message back on the queue indicating that APP-C never got to
111 // 2) Let downstream figure it out after timeout between PENDING and
116 public String getBenchmark() {
117 long time = System.currentTimeMillis();
118 DateFormat df = new SimpleDateFormat("HH:mm:ss");
119 df.setTimeZone(TimeZone.getTimeZone("UTC"));
120 String runningTime = df.format(new Date(time - startTime));
122 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
123 executor.getCompletedTaskCount(), executor.getPoolSize());
124 LOG.info("***BENCHMARK*** " + out);