e98e66148db2f44fd36ebfcaf19e67fe062ec47d
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.context.impl.distribution;
23
24 import java.util.Timer;
25 import java.util.TimerTask;
26 import lombok.ToString;
27 import org.onap.policy.apex.context.ContextException;
28 import org.onap.policy.apex.context.Distributor;
29 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
30 import org.onap.policy.apex.context.parameters.PersistorParameters;
31 import org.onap.policy.common.parameters.ParameterService;
32 import org.slf4j.ext.XLogger;
33 import org.slf4j.ext.XLoggerFactory;
34
35 /**
36  * This class is used to periodically flush a context distributor.
37  *
38  * @author eeilfn
39  */
40 @ToString
41 public class DistributorFlushTimerTask extends TimerTask {
42     private static final XLogger LOGGER = XLoggerFactory.getXLogger(DistributorFlushTimerTask.class);
43
44     // The timer for flushing
45     @ToString.Exclude
46     private Timer timer = null;
47
48     // The context distributor to flush
49     @ToString.Exclude
50     private final Distributor contextDistributor;
51
52     // Timing information
53     private long flushPeriod = 0;
54     private long flushCount = 0;
55
56     /**
57      * Constructor, save a reference to the event stream handler.
58      *
59      * @param contextDistributor the distributor that this timer task is flushing
60      * @throws ContextException On flush setup errors
61      */
62     public DistributorFlushTimerTask(final Distributor contextDistributor) throws ContextException {
63         // Save the context distributor and period
64         this.contextDistributor = contextDistributor;
65
66         // Set the period for persistence flushing
67         final PersistorParameters persistorParameters = ParameterService
68                         .get(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
69         flushPeriod = persistorParameters.getFlushPeriod();
70
71         // Set up the timer
72         timer = new Timer(DistributorFlushTimerTask.class.getSimpleName(), true);
73         timer.schedule(this, flushPeriod, flushPeriod);
74
75         LOGGER.debug("context distributor {} flushing set up with interval: {} ms", contextDistributor.getKey().getId(),
76                         flushPeriod);
77     }
78
79     /**
80      * Flush the context distributor.
81      */
82     @Override
83     public void run() {
84         // Increment the flush counter
85         flushCount++;
86
87         LOGGER.debug("context distributor {} flushing: period={}: count={}", contextDistributor.getKey().getId(),
88                         flushPeriod, flushCount);
89         try {
90             contextDistributor.flush();
91             LOGGER.debug("context distributor {} flushed: period={}: count={}", contextDistributor.getKey().getId(),
92                             flushPeriod, flushCount);
93         } catch (final ContextException e) {
94             LOGGER.error("flush error on context distributor {}: period={}: count={}",
95                             contextDistributor.getKey().getId(), flushPeriod, flushCount, e);
96         }
97     }
98
99     /**
100      * Cancel the timer.
101      *
102      * @return true, if cancel
103      */
104     @Override
105     public boolean cancel() {
106         // Cancel the timer
107         if (timer != null) {
108             timer.cancel();
109         }
110         return true;
111     }
112 }