2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Copyright (C) 2017 Amdocs
8 * =============================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
21 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 * ============LICENSE_END=========================================================
25 package org.onap.appc.listener.demo.impl;
27 import java.text.DateFormat;
28 import java.text.SimpleDateFormat;
29 import java.util.Date;
30 import java.util.List;
31 import java.util.TimeZone;
32 import java.util.concurrent.RejectedExecutionException;
34 import org.onap.appc.listener.AbstractListener;
35 import org.onap.appc.listener.ListenerProperties;
36 import org.onap.appc.listener.demo.model.IncomingMessage;
38 import com.att.eelf.configuration.EELFLogger;
39 import com.att.eelf.configuration.EELFManager;
40 import com.att.eelf.i18n.EELFResourceManager;
42 public class ListenerImpl extends AbstractListener {
44 private final EELFLogger LOG = EELFManager.getInstance().getLogger(ListenerImpl.class);
46 private long startTime = 0;
48 public ListenerImpl(ListenerProperties props) {
50 String url = props.getProperty("provider.url");
51 LOG.info("DMaaP Provider Endpoint: " + url);
52 ProviderOperations.setUrl(url);
55 String user = props.getProperty("provider.user");
56 String pass = props.getProperty("provider.pass");
57 ProviderOperations.setAuthentication(user, pass);
62 // Some vars for benchmarking
63 startTime = System.currentTimeMillis();
65 LOG.info("Running DMaaP Listener");
68 // Only update if the queue is low. otherwise we read in more
69 // messages than we need
71 if (executor.getQueue().size() <= QUEUED_MIN) {
72 LOG.debug("DMaaP queue running low. Querying for more jobs");
73 List<IncomingMessage> messages = dmaap.getIncomingEvents(IncomingMessage.class, QUEUED_MAX);
74 LOG.debug(String.format("Read %d messages from dmaap", messages.size()));
75 for (IncomingMessage incoming : messages) {
76 // Acknowledge that we read the event
77 LOG.info("Acknowledging Message: " + incoming.getHeader().getRequestID());
79 //TODO: Should we post a pending status for 1607
80 //dmaap.postStatus(incoming.toOutgoing(Status.PENDING, null).toString());
82 for (IncomingMessage incoming : messages) {
83 // Add to pool if still running
85 LOG.info(String.format("Adding DMaaP message to pool queue [%s]", incoming.getHeader().getRequestID()));
86 if (incoming.isValid()) {
88 executor.execute(new WorkerImpl(incoming, dmaap));
89 } catch (RejectedExecutionException rejectEx) {
90 LOG.error("Task Rejected: ", rejectEx);
93 // Badly formed message
94 LOG.error("Message was not valid. Rejecting");
97 LOG.info("Run stopped. Orphaning Message: " + incoming.getHeader().getRequestID());
101 } catch (Exception e) {
102 LOG.error("Exception " + e.getClass().getSimpleName() + " caught in DMaaP listener");
103 LOG.error(EELFResourceManager.format(e));
104 LOG.error("DMaaP Listener logging and ignoring the exception, continue...");
108 LOG.info("Stopping DMaaP Listener thread");
110 // We've told the listener to stop
112 // 1) Put a message back on the queue indicating that APP-C never got to
115 // 2) Let downstream figure it out after timeout between PENDING and
120 public String getBenchmark() {
121 long time = System.currentTimeMillis();
122 DateFormat df = new SimpleDateFormat("HH:mm:ss");
123 df.setTimeZone(TimeZone.getTimeZone("UTC"));
124 String runningTime = df.format(new Date(time - startTime));
126 String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime,
127 executor.getCompletedTaskCount(), executor.getPoolSize());
128 LOG.info("***BENCHMARK*** " + out);