2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.model.cache.workers;
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;
27 import java.util.concurrent.LinkedBlockingQueue;
28 import java.util.concurrent.TimeUnit;
31 * Created by mlando on 9/6/2016. the class represents a worker the pull job
32 * from a queue and evacuates them.
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;
45 * the name of the given worker
47 * the queue the worker will block on.
49 public CacheWorker(String workerName, LinkedBlockingQueue<Job> jobQueue) {
50 this.workerName = workerName;
51 this.jobQueue = jobQueue;
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
63 log.trace("CacheWorker:{} doing work", workerName);
65 Job job = jobQueue.poll(500, TimeUnit.MILLISECONDS);
68 log.trace("worker:{} done with work", workerName);
70 } catch (Exception e) {
71 log.debug("worker {} failed during job execution.", workerName);
72 log.debug("exception", e);
75 log.debug("worker:{} nothing to do stoping", workerName);
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
87 public void shutDown() {