re base code
[sdc.git] / catalog-model / src / main / java / org / openecomp / sdc / be / model / cache / workers / CacheWorker.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.model.cache.workers;
22
23 import org.openecomp.sdc.be.model.cache.jobs.Job;
24 import org.openecomp.sdc.be.workers.Worker;
25 import org.openecomp.sdc.common.log.wrappers.Logger;
26
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.TimeUnit;
29
30 /**
31  * Created by mlando on 9/6/2016. the class represents a worker the pull job
32  * from a queue and evacuates them.
33  *
34  */
35 public class CacheWorker implements Runnable, IWorker {
36     private String workerName;
37     private static final Logger log = Logger.getLogger(Worker.class.getName());
38     private LinkedBlockingQueue<Job> jobQueue;
39     private volatile boolean shutdown = false;
40
41     /**
42      * constructor
43      *
44      * @param workerName
45      *            the name of the given worker
46      * @param jobQueue
47      *            the queue the worker will block on.
48      */
49     public CacheWorker(String workerName, LinkedBlockingQueue<Job> jobQueue) {
50         this.workerName = workerName;
51         this.jobQueue = jobQueue;
52     }
53
54     /**
55      * the method will try to get a job if one is avilable it will be retrived
56      * and handled. if no jobs are available the worker will block for 500
57      * milliseconds and then it wil check if it needs to shutdown. if not it
58      * will block again and so on until sutdown or a new job is available
59      */
60     @Override
61     public void run() {
62         while (true) {
63             log.trace("CacheWorker:{} doing work", workerName);
64             try {
65                 Job job = jobQueue.poll(500, TimeUnit.MILLISECONDS);
66                 if (job != null) {
67                     job.doWork();
68                     log.trace("worker:{} done with work", workerName);
69                 }
70             } catch (Exception e) {
71                 log.debug("worker {} failed during job execution.", workerName);
72                 log.debug("exception", e);
73             }
74             if (shutdown) {
75                 log.debug("worker:{} nothing to do stoping", workerName);
76                 break;
77             }
78         }
79
80     }
81
82     /**
83      * the method sets the shutdown flag, when set the worker will stop it's
84      * execution as soon as possible with out completing its work
85      */
86     @Override
87     public void shutDown() {
88         this.shutdown = true;
89     }
90
91 }