Applying license changes to all files
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / openecomp / appc / executionqueue / impl / ExecutionQueueServiceImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
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
12  * 
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  * 
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.
20  * 
21  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22  * ============LICENSE_END=========================================================
23  */
24
25 package org.openecomp.appc.executionqueue.impl;
26
27 import java.time.Instant;
28 import java.util.Calendar;
29 import java.util.Date;
30 import java.util.concurrent.TimeUnit;
31
32 import org.openecomp.appc.exceptions.APPCException;
33 import org.openecomp.appc.executionqueue.ExecutionQueueService;
34 import org.openecomp.appc.executionqueue.MessageExpirationListener;
35 import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
36
37 import com.att.eelf.configuration.EELFLogger;
38 import com.att.eelf.configuration.EELFManager;
39
40 public class ExecutionQueueServiceImpl<M extends Runnable> implements ExecutionQueueService<M> {
41
42     private static final EELFLogger logger =
43             EELFManager.getInstance().getLogger(ExecutionQueueServiceImpl.class);
44
45     ExecutionQueueServiceImpl(){
46
47     }
48
49     @Override
50     public void putMessage(M message) throws APPCException {
51          this.putMessage(message,-1,null);
52     }
53
54     @Override
55     public void putMessage(M message, long timeout, TimeUnit unit) throws APPCException{
56         try {
57             Instant expirationTime = calculateExpirationTime(timeout,unit);
58             QueueManager queueManager = QueueManager.getInstance();
59             boolean enqueueTask = queueManager.enqueueTask(new QueueMessage<M>(message,expirationTime));
60             if(!enqueueTask){
61                 throw new APPCException("failed to put message in queue");
62             }
63         } catch (Exception e) {
64             logger.error("Error in putMessage method of ExecutionQueueServiceImpl" + e.getMessage());
65             throw new APPCException(e);
66         }
67     }
68
69     @Override
70     public void registerMessageExpirationListener(MessageExpirationListener listener) {
71         QueueManager.getInstance().setListener(listener);
72     }
73
74     private Instant calculateExpirationTime(long timeToLive, TimeUnit unit) {
75         if (timeToLive > 0 && unit != null) {
76             // as of Java 8, there is no built-in conversion method from
77             // TimeUnit to ChronoUnit; do it manually
78             return Instant.now().plusMillis(unit.toMillis(timeToLive));
79         } else {
80             // never expires
81             return Instant.MAX;
82         }
83     }
84
85 }