Include impacted changes for APPC-346,APPC-348
[appc.git] / appc-dispatcher / appc-dispatcher-common / execution-queue-management-lib / src / main / java / org / onap / 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.onap.appc.executionqueue.impl;
26
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;
33
34 import java.util.Calendar;
35 import java.util.Date;
36 import java.util.concurrent.TimeUnit;
37
38 public class ExecutionQueueServiceImpl<M extends Runnable> implements ExecutionQueueService<M> {
39
40     private final EELFLogger logger = EELFManager.getInstance().getLogger(ExecutionQueueServiceImpl.class);
41
42     private QueueManager queueManager;
43
44     public ExecutionQueueServiceImpl(){
45
46     }
47
48     @Override
49     public void putMessage(M message) throws APPCException {
50          this.putMessage(message,-1,null);
51     }
52
53     /**
54      * Injected by blueprint
55      * @param queueManager
56      */
57     public void setQueueManager(QueueManager queueManager) {
58         this.queueManager = queueManager;
59     }
60
61     @Override
62     public void putMessage(M message, long timeout, TimeUnit unit) throws APPCException{
63         QueueMessage queueMessage;
64
65         try {
66             Date expirationTime = calculateExpirationTime(timeout,unit);
67             queueMessage = new QueueMessage(message,expirationTime);
68             boolean enqueueTask = queueManager.enqueueTask(queueMessage);
69             if(!enqueueTask){
70                 throw new APPCException("failed to put message in queue");
71             }
72         } catch (Exception e) {
73             logger.error("Error in putMessage method of ExecutionQueueServiceImpl" + e.getMessage());
74             throw new APPCException(e);
75         }
76     }
77
78     private Date calculateExpirationTime(long timeToLive, TimeUnit unit) {
79         Date expirationTime = null;
80         if(timeToLive > 0){
81             long currentTime = System.currentTimeMillis();
82             Calendar cal = Calendar.getInstance();
83             cal.setTimeInMillis(currentTime + unit.toMillis(timeToLive));
84             expirationTime = cal.getTime();
85         }
86         return expirationTime;
87     }
88
89 }