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.executionqueue.impl;
27 import com.att.eelf.configuration.EELFLogger;
28 import com.att.eelf.configuration.EELFManager;
29 import org.onap.appc.exceptions.APPCException;
30 import org.onap.appc.executionqueue.ExecutionQueueService;
31 import org.onap.appc.executionqueue.MessageExpirationListener;
32 import org.onap.appc.executionqueue.impl.object.QueueMessage;
34 import java.time.Instant;
35 import java.util.concurrent.TimeUnit;
37 public class ExecutionQueueServiceImpl<M extends Runnable> implements ExecutionQueueService<M> {
39 private static final EELFLogger logger =
40 EELFManager.getInstance().getLogger(ExecutionQueueServiceImpl.class);
42 private QueueManager queueManager;
44 public ExecutionQueueServiceImpl() {
49 * Injected by blueprint
51 * @param queueManager queue manager to be set
53 public void setQueueManager(QueueManager queueManager) {
54 this.queueManager = queueManager;
58 public void putMessage(M message) throws APPCException {
59 this.putMessage(message, -1, null);
63 public void putMessage(M message, long timeout, TimeUnit unit) throws APPCException {
64 Instant expirationTime = calculateExpirationTime(timeout, unit);
65 boolean enqueueTask = queueManager.enqueueTask(new QueueMessage<>(message, expirationTime));
67 logger.error("Error in putMessage method of ExecutionQueueServiceImpl");
68 throw new APPCException("Failed to put message in queue");
73 public void registerMessageExpirationListener(MessageExpirationListener listener) {
74 queueManager.setListener(listener);
77 private Instant calculateExpirationTime(long timeToLive, TimeUnit unit) {
78 if (timeToLive > 0 && unit != null) {
79 // as of Java 8, there is no built-in conversion method from
80 // TimeUnit to ChronoUnit; do it manually
81 return Instant.now().plusMillis(unit.toMillis(timeToLive));