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.CL1607.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.CL1607.model.IncomingMessage;
34 import org.openecomp.appc.listener.CL1607.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.getHeader().getRequestID());
77 //TODO: Should we post a pending status for 1607
78 //dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
80 for (IncomingMessage incoming : messages) {
81 // Add to pool if still running
83 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getHeader().getRequestID()));
84 if (incoming.isValid()) {
86 executor.execute(new WorkerImpl(incoming, dmaap));
87 } catch (RejectedExecutionException rejectEx) {
88 LOG.error("Task Rejected: ", rejectEx);
91 // Badly formed message
92 LOG.error("Message was not valid. Rejecting");
95 LOG.info("Run stopped. Orphaning Message: " + incoming.getHeader().getRequestID());
99 } catch (Exception e) {
100 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
101 LOG.error(EELFResourceManager.format(e));
102 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
106 LOG.info("Stopping DMaaP Listener thread");
108 // We've told the listener to stop
110 // 1) Put a message back on the queue indicating that APP-C never got to
113 // 2) Let downstream figure it out after timeout between PENDING and
118 public String getBenchmark() {
119 long time = System.currentTimeMillis();
120 DateFormat df = new SimpleDateFormat("HH:mm:ss");
121 df.setTimeZone(TimeZone.getTimeZone("UTC"));
122 String runningTime = df.format(new Date(time - startTime));
124 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
125 executor.getCompletedTaskCount(), executor.getPoolSize());
126 LOG.info("***BENCHMARK*** " + out);