• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

nats-io / nats.java / #2020

29 Jun 2025 06:09PM UTC coverage: 95.643% (+0.05%) from 95.598%
#2020

push

github

web-flow
Merge pull request #1335 from nats-io/replace-timer

Replace Timer with scheduled tasks

66 of 77 new or added lines in 6 files covered. (85.71%)

1 existing line in 1 file now uncovered.

11767 of 12303 relevant lines covered (95.64%)

0.96 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.24
/src/main/java/io/nats/client/support/ScheduledTask.java
1
// Copyright 2025 The NATS Authors
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at:
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13

14
package io.nats.client.support;
15

16
import java.util.concurrent.ScheduledExecutorService;
17
import java.util.concurrent.ScheduledFuture;
18
import java.util.concurrent.TimeUnit;
19
import java.util.concurrent.atomic.AtomicBoolean;
20
import java.util.concurrent.atomic.AtomicReference;
21

22
/**
23
 * This is a utility class for a task being scheduled
24
 */
25
public class ScheduledTask implements Runnable {
26
    private final Runnable runnable;
27
    protected final AtomicReference<ScheduledFuture<?>> scheduledFutureRef;
28

29
    protected final AtomicBoolean keepGoing;
30

31
    public ScheduledTask(ScheduledExecutorService ses, long initialAndPeriodMillis, Runnable runnable) {
32
        this(ses, initialAndPeriodMillis, initialAndPeriodMillis, TimeUnit.MILLISECONDS, runnable);
1✔
33
    }
1✔
34

35
    public ScheduledTask(ScheduledExecutorService ses, long initialAndPeriod, TimeUnit unit, Runnable runnable) {
36
        this(ses, initialAndPeriod, initialAndPeriod, unit, runnable);
1✔
37
    }
1✔
38

39
    public ScheduledTask(ScheduledExecutorService ses, long initialDelay, long period, TimeUnit unit, Runnable runnable) {
1✔
40
        this.runnable = runnable;
1✔
41
        keepGoing = new AtomicBoolean(true);
1✔
42
        scheduledFutureRef = new AtomicReference<>(
1✔
43
            ses.scheduleAtFixedRate(this, initialDelay, period, unit));
1✔
44
    }
1✔
45

46
    @Override
47
    public void run() {
48
        if (keepGoing.get() && !Thread.interrupted()) {
1✔
49
            runnable.run();
1✔
50
        }
51
    }
1✔
52

53
    public void shutdown() {
54
        try {
55
            keepGoing.getAndSet(false);
1✔
56
            ScheduledFuture<?> f = scheduledFutureRef.get();
1✔
57
            scheduledFutureRef.set(null); // just releasing resources
1✔
58
            if (f != null) {
1✔
59
                f.cancel(true);
1✔
60
            }
61
        }
NEW
62
        catch (Exception ignore) {
×
63
            // don't want this to be passed along
64
        }
1✔
65
    }
1✔
66
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc