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.demo.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.demo.model.IncomingMessage;
35 import com.att.eelf.configuration.EELFLogger;
36 import com.att.eelf.configuration.EELFManager;
37 import com.att.eelf.i18n.EELFResourceManager;
39 public class ListenerImpl extends AbstractListener {
41 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
43 private long startTime = 0;
45 public ListenerImpl(ListenerProperties props) {
47 String url = props.getProperty("provider.url");
48 LOG.info("DMaaP Provider Endpoint: " + url);
49 ProviderOperations.setUrl(url);
52 String user = props.getProperty("provider.user");
53 String pass = props.getProperty("provider.pass");
54 ProviderOperations.setAuthentication(user, pass);
59 // Some vars for benchmarking
60 startTime = System.currentTimeMillis();
62 LOG.info("Running DMaaP Listener");
65 // Only update if the queue is low. otherwise we read in more
66 // messages than we need
68 if (executor.getQueue().size() <= QUEUED_MIN) {
69 LOG.debug("DMaaP queue running low. Querying for more jobs");
70 List<IncomingMessage> messages = dmaap.getIncomingEvents(IncomingMessage.class, QUEUED_MAX);
71 LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
72 for (IncomingMessage incoming : messages) {
73 // Acknowledge that we read the event
74 LOG.info("Acknowledging Message: " + incoming.getHeader().getRequestID());
76 //TODO: Should we post a pending status for 1607
77 //dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
79 for (IncomingMessage incoming : messages) {
80 // Add to pool if still running
82 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getHeader().getRequestID()));
83 if (incoming.isValid()) {
85 executor.execute(new WorkerImpl(incoming, dmaap));
86 } catch (RejectedExecutionException rejectEx) {
87 LOG.error("Task Rejected: ", rejectEx);
90 // Badly formed message
91 LOG.error("Message was not valid. Rejecting");
94 LOG.info("Run stopped. Orphaning Message: " + incoming.getHeader().getRequestID());
98 } catch (Exception e) {
99 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
100 LOG.error(EELFResourceManager.format(e));
101 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
105 LOG.info("Stopping DMaaP Listener thread");
107 // We've told the listener to stop
109 // 1) Put a message back on the queue indicating that APP-C never got to
112 // 2) Let downstream figure it out after timeout between PENDING and
117 public String getBenchmark() {
118 long time = System.currentTimeMillis();
119 DateFormat df = new SimpleDateFormat("HH:mm:ss");
120 df.setTimeZone(TimeZone.getTimeZone("UTC"));
121 String runningTime = df.format(new Date(time - startTime));
123 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
124 executor.getCompletedTaskCount(), executor.getPoolSize());
125 LOG.info("***BENCHMARK*** " + out);