%line | %branch | |||||||||
---|---|---|---|---|---|---|---|---|---|---|
net.sf.infrared.agent.transport.impl.PeriodicFlushPolicy |
|
|
1 | /* |
|
2 | * Copyright 2005 Tavant Technologies and Contributors |
|
3 | * |
|
4 | * Licensed under the Apache License, Version 2.0 (the "License") |
|
5 | * you may not use this file except in compliance with the License. |
|
6 | * You may obtain a copy of the License at |
|
7 | * |
|
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|
9 | * |
|
10 | * Unless required by applicable law or agreed to in writing, software |
|
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
13 | * See the License for the specific language governing permissions and |
|
14 | * limitations under the License. |
|
15 | * |
|
16 | * |
|
17 | * |
|
18 | * Original Author: binil.thomas (Tavant Technologies) |
|
19 | * Contributor(s): -; |
|
20 | * |
|
21 | */ |
|
22 | package net.sf.infrared.agent.transport.impl; |
|
23 | ||
24 | import java.util.Timer; |
|
25 | import java.util.TimerTask; |
|
26 | ||
27 | import org.apache.log4j.Logger; |
|
28 | ||
29 | import net.sf.infrared.agent.transport.Aggregator; |
|
30 | import net.sf.infrared.agent.transport.FlushPolicy; |
|
31 | import net.sf.infrared.base.util.LoggingFactory; |
|
32 | ||
33 | 8 | public class PeriodicFlushPolicy implements FlushPolicy { |
34 | public static final long DEFAULT_FREQUENCY = 30 * 1000L; |
|
35 | ||
36 | 2 | private static Logger log = LoggingFactory.getLogger(PeriodicFlushPolicy.class); |
37 | ||
38 | private Aggregator aggregator; |
|
39 | ||
40 | 8 | private long frequency = DEFAULT_FREQUENCY; |
41 | ||
42 | 8 | private Timer timer = null; |
43 | ||
44 | public boolean activate() { |
|
45 | 4 | if (isActive()) { |
46 | 1 | return false; |
47 | } |
|
48 | 3 | timer = new Timer(true); |
49 | 3 | timer.schedule(new TimerTask() { |
50 | public void run() { |
|
51 | if (aggregator != null) { |
|
52 | aggregator.flush(); |
|
53 | } |
|
54 | } |
|
55 | }, 0, frequency); |
|
56 | 3 | return true; |
57 | } |
|
58 | ||
59 | public boolean shutDown() { |
|
60 | 10 | if (!isActive()) { |
61 | 7 | return false; |
62 | } |
|
63 | 3 | timer.cancel(); |
64 | 3 | timer = null; |
65 | 3 | return true; |
66 | } |
|
67 | ||
68 | public void setAggregator(Aggregator aggregator) { |
|
69 | 0 | this.aggregator = aggregator; |
70 | 0 | } |
71 | ||
72 | public boolean isActive() { |
|
73 | 19 | return (timer != null); |
74 | } |
|
75 | ||
76 | /** |
|
77 | * Sets the frequency in which this polciy should flush the aggregator. |
|
78 | * |
|
79 | * @param time |
|
80 | * the frequency in milliseconds |
|
81 | */ |
|
82 | public void setFrequency(long time) { |
|
83 | 11 | if (time <= 0) { |
84 | 2 | log.error("Attempt to set frequency to " + time + "ms ignored. " |
85 | + "Flush frequency should be greater than zero ms"); |
|
86 | 2 | return; |
87 | } |
|
88 | 9 | this.frequency = time; |
89 | 9 | if (log.isDebugEnabled()) { |
90 | 0 | log.debug("Flush frequency set to " + time + " ms"); |
91 | } |
|
92 | 9 | } |
93 | ||
94 | /** |
|
95 | * Gets the frequency in which this policy is set to flush the associated |
|
96 | * aggregator. A newly created policy will return the public constant |
|
97 | * specified by DEFAULT_FREQUENCY. |
|
98 | * |
|
99 | * @return |
|
100 | */ |
|
101 | public long getFrequency() { |
|
102 | 3 | return frequency; |
103 | } |
|
104 | } |
This report is generated by jcoverage, Maven and Maven JCoverage Plugin. |