summaryrefslogtreecommitdiffstats
path: root/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib
diff options
context:
space:
mode:
authorGary Wu <gary.i.wu@huawei.com>2017-05-08 12:59:44 -0700
committerPatrick Brady <pb071s@att.com>2017-05-16 18:13:41 +0000
commit753cb638fcb58f355f2c979e9e0a43cba2832984 (patch)
tree55526dce196663d6c5c4265e504882e3deec2cc2 /appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib
parentf16391cacb2e91063bbe9ad66cb2310855c93695 (diff)
Migrate to Java 8 Time API
Migrate the uses of the old Date class in CommonHeader, RuntimeContext, QueueMessage, and TransactionRecord to Java 8 Time API. Change-Id: I3b0c18b2e63cdab06e37587d2010cb7f626a0396 Signed-off-by: Gary Wu <gary.i.wu@huawei.com>
Diffstat (limited to 'appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib')
-rw-r--r--appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/ExecutionQueueServiceImpl.java20
-rw-r--r--appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/QueueManager.java10
-rw-r--r--appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/object/QueueMessage.java15
3 files changed, 21 insertions, 24 deletions
diff --git a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/ExecutionQueueServiceImpl.java b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/ExecutionQueueServiceImpl.java
index 2ac383696..672f4aa9b 100644
--- a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/ExecutionQueueServiceImpl.java
+++ b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/ExecutionQueueServiceImpl.java
@@ -21,6 +21,7 @@
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 +30,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 +51,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 +68,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;
}
}
diff --git a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/QueueManager.java b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/QueueManager.java
index cf625b4d5..056bfda29 100644
--- a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/QueueManager.java
+++ b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/QueueManager.java
@@ -28,6 +28,7 @@ import java.util.concurrent.LinkedBlockingQueue;
import org.openecomp.appc.executionqueue.MessageExpirationListener;
import org.openecomp.appc.executionqueue.helper.Util;
import org.openecomp.appc.executionqueue.impl.object.QueueMessage;
+
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
@@ -69,7 +70,7 @@ public class QueueManager {
while (true){
try{
QueueMessage<? extends Runnable> queueMessage = queue.take();
- if(messageExpired(queueMessage)){
+ if (queueMessage.isExpired()) {
logger.debug("Message expired "+ queueMessage.getMessage());
if(listener != null){
listener.onMessageExpiration(queueMessage.getMessage());
@@ -98,11 +99,4 @@ public class QueueManager {
return queue.offer(queueMessage);
}
- private boolean messageExpired(QueueMessage<? extends Runnable> queueMessage) {
- if(queueMessage.getExpirationTime() != null){
- return queueMessage.getExpirationTime().getTime() < System.currentTimeMillis();
- }
- return false;
- }
-
}
diff --git a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/object/QueueMessage.java b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/object/QueueMessage.java
index bbf805871..d3097e650 100644
--- a/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/object/QueueMessage.java
+++ b/appc-dispatcher/appc-dispatcher-common/execution-queue-management-lib/src/main/java/org/openecomp/appc/executionqueue/impl/object/QueueMessage.java
@@ -21,22 +21,23 @@
package org.openecomp.appc.executionqueue.impl.object;
-import java.util.Date;
+import java.time.Instant;
+import java.util.Objects;
public class QueueMessage<M extends Runnable> {
- M message;
- Date expirationTime;
- public QueueMessage(M message, Date expirationTime){
+ private final M message;
+ private final Instant expirationTime;
+ public QueueMessage(M message, Instant expirationTime){
this.message = message;
- this.expirationTime = expirationTime;
+ this.expirationTime = Objects.requireNonNull(expirationTime);
}
public M getMessage() {
return message;
}
- public Date getExpirationTime() {
- return expirationTime;
+ public boolean isExpired() {
+ return expirationTime.isBefore(Instant.now());
}
}