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
index 2ac3836..3092bd8 100644 (file)
@@ -1,10 +1,11 @@
 /*-
  * ============LICENSE_START=======================================================
- * openECOMP : APP-C
+ * ONAP : APPC
  * ================================================================================
- * Copyright (C) 2017 AT&T Intellectual Property. All rights
- *                                             reserved.
+ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
+ * Copyright (C) 2017 Amdocs
+ * =============================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
+ * 
+ * ECOMP is a trademark and service mark of AT&T Intellectual Property.
  * ============LICENSE_END=========================================================
  */
 
 package org.openecomp.appc.executionqueue.impl;
 
+import java.time.Instant;
 import java.util.Calendar;
 import java.util.Date;
 import java.util.concurrent.TimeUnit;
@@ -29,6 +33,7 @@ import org.openecomp.appc.exceptions.APPCException;
 import org.openecomp.appc.executionqueue.ExecutionQueueService;
 import org.openecomp.appc.executionqueue.MessageExpirationListener;
 import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
+
 import com.att.eelf.configuration.EELFLogger;
 import com.att.eelf.configuration.EELFManager;
 
@@ -49,7 +54,7 @@ public class ExecutionQueueServiceImpl<M extends Runnable> implements ExecutionQ
     @Override
     public void putMessage(M message, long timeout, TimeUnit unit) throws APPCException{
         try {
-            Date expirationTime = calculateExpirationTime(timeout,unit);
+            Instant expirationTime = calculateExpirationTime(timeout,unit);
             QueueManager queueManager = QueueManager.getInstance();
             boolean enqueueTask = queueManager.enqueueTask(new QueueMessage<M>(message,expirationTime));
             if(!enqueueTask){
@@ -66,15 +71,15 @@ public class ExecutionQueueServiceImpl<M extends Runnable> implements ExecutionQ
         QueueManager.getInstance().setListener(listener);
     }
 
-    private Date calculateExpirationTime(long timeToLive, TimeUnit unit) {
-        Date expirationTime = null;
-        if(timeToLive > 0){
-            long currentTime = System.currentTimeMillis();
-            Calendar cal = Calendar.getInstance();
-            cal.setTimeInMillis(currentTime + unit.toMillis(timeToLive));
-            expirationTime = cal.getTime();
+    private Instant calculateExpirationTime(long timeToLive, TimeUnit unit) {
+        if (timeToLive > 0 && unit != null) {
+            // as of Java 8, there is no built-in conversion method from
+            // TimeUnit to ChronoUnit; do it manually
+            return Instant.now().plusMillis(unit.toMillis(timeToLive));
+        } else {
+            // never expires
+            return Instant.MAX;
         }
-        return expirationTime;
     }
 
 }