Open Kilda Java Documentation
HeartBeat.java
Go to the documentation of this file.
1 /* Copyright 2018 Telstra Open Source
2  *
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 package org.openkilda.floodlight.kafka;
17 
18 import static com.google.common.base.Preconditions.checkArgument;
19 import static java.util.Objects.requireNonNull;
20 
22 
23 import java.util.Timer;
24 import java.util.TimerTask;
25 
26 public class HeartBeat {
27  private final Producer producer;
28  private final long interval;
29  private final String topoDiscoTopic;
30 
31  private final Timer timer;
32  private TimerTask task;
33 
34  public HeartBeat(Producer producer, long interval, String topoDiscoTopic) {
35  this.producer = producer;
36 
37  checkArgument(interval > 0, "interval must be positive");
38  this.interval = interval;
39 
40  this.topoDiscoTopic = requireNonNull(topoDiscoTopic, "topoDiscoTopic cannot be null");
41 
42  task = new HeartBeatAction(producer, topoDiscoTopic);
43  timer = new Timer("kafka.HeartBeat", true);
44  timer.scheduleAtFixedRate(task, interval, interval);
45  }
46 
50  public void reschedule() {
51  TimerTask replace = new HeartBeatAction(producer, topoDiscoTopic);
52  timer.scheduleAtFixedRate(replace, interval, interval);
53 
54  synchronized (this) {
55  task.cancel();
56  task = replace;
57  }
58  }
59 }
HeartBeat(Producer producer, long interval, String topoDiscoTopic)
Definition: HeartBeat.java:34