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

TAKETODAY / today-infrastructure / 8308118446

16 Mar 2024 01:26PM UTC coverage: 78.393% (+0.02%) from 78.374%
8308118446

push

github

TAKETODAY
:white_check_mark:

63592 of 86119 branches covered (73.84%)

Branch coverage included in aggregate %.

157000 of 195273 relevant lines covered (80.4%)

3.41 hits per line

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

85.71
today-context/src/main/java/cn/taketoday/scheduling/support/TaskUtils.java
1
/*
2
 * Copyright 2017 - 2024 the original author or authors.
3
 *
4
 * This program is free software: you can redistribute it and/or modify
5
 * it under the terms of the GNU General Public License as published by
6
 * the Free Software Foundation, either version 3 of the License, or
7
 * (at your option) any later version.
8
 *
9
 * This program is distributed in the hope that it will be useful,
10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
 * GNU General Public License for more details.
13
 *
14
 * You should have received a copy of the GNU General Public License
15
 * along with this program. If not, see [https://www.gnu.org/licenses/]
16
 */
17

18
package cn.taketoday.scheduling.support;
19

20
import java.util.concurrent.Future;
21

22
import cn.taketoday.lang.Nullable;
23
import cn.taketoday.logging.Logger;
24
import cn.taketoday.logging.LoggerFactory;
25
import cn.taketoday.util.ErrorHandler;
26
import cn.taketoday.util.ReflectionUtils;
27

28
/**
29
 * Utility methods for decorating tasks with error handling.
30
 *
31
 * <p><b>NOTE:</b> This class is intended for internal use by  scheduler
32
 * implementations. It is only public so that it may be accessed from impl classes
33
 * within other packages. It is <i>not</i> intended for general use.
34
 *
35
 * @author Mark Fisher
36
 * @author Juergen Hoeller
37
 * @author <a href="https://github.com/TAKETODAY">Harry Yang</a>
38
 * @since 4.0
39
 */
40
public abstract class TaskUtils {
×
41

42
  /**
43
   * An ErrorHandler strategy that will log the Exception but perform
44
   * no further handling. This will suppress the error so that
45
   * subsequent executions of the task will not be prevented.
46
   */
47
  public static final ErrorHandler LOG_AND_SUPPRESS_ERROR_HANDLER = new LoggingErrorHandler();
4✔
48

49
  /**
50
   * An ErrorHandler strategy that will log at error level and then
51
   * re-throw the Exception. Note: this will typically prevent subsequent
52
   * execution of a scheduled task.
53
   */
54
  public static final ErrorHandler LOG_AND_PROPAGATE_ERROR_HANDLER = new PropagatingErrorHandler();
5✔
55

56
  /**
57
   * Decorate the task for error handling. If the provided {@link ErrorHandler}
58
   * is not {@code null}, it will be used. Otherwise, repeating tasks will have
59
   * errors suppressed by default whereas one-shot tasks will have errors
60
   * propagated by default since those errors may be expected through the
61
   * returned {@link Future}. In both cases, the errors will be logged.
62
   */
63
  public static DelegatingErrorHandlingRunnable decorateTaskWithErrorHandler(
64
          Runnable task, @Nullable ErrorHandler errorHandler, boolean isRepeatingTask) {
65

66
    if (task instanceof DelegatingErrorHandlingRunnable) {
3✔
67
      return (DelegatingErrorHandlingRunnable) task;
3✔
68
    }
69
    ErrorHandler eh = (errorHandler != null ? errorHandler : getDefaultErrorHandler(isRepeatingTask));
7✔
70
    return new DelegatingErrorHandlingRunnable(task, eh);
6✔
71
  }
72

73
  /**
74
   * Return the default {@link ErrorHandler} implementation based on the boolean
75
   * value indicating whether the task will be repeating or not. For repeating tasks
76
   * it will suppress errors, but for one-time tasks it will propagate. In both
77
   * cases, the error will be logged.
78
   */
79
  public static ErrorHandler getDefaultErrorHandler(boolean isRepeatingTask) {
80
    return (isRepeatingTask ? LOG_AND_SUPPRESS_ERROR_HANDLER : LOG_AND_PROPAGATE_ERROR_HANDLER);
6✔
81
  }
82

83
  /**
84
   * An {@link ErrorHandler} implementation that logs the Throwable at error
85
   * level. It does not perform any additional error handling. This can be
86
   * useful when suppression of errors is the intended behavior.
87
   */
88
  private static class LoggingErrorHandler implements ErrorHandler {
2✔
89

90
    private final Logger logger = LoggerFactory.getLogger(LoggingErrorHandler.class);
5✔
91

92
    @Override
93
    public void handleError(Throwable t) {
94
      logger.error("Unexpected error occurred in scheduled task", t);
5✔
95
    }
1✔
96
  }
97

98
  /**
99
   * An {@link ErrorHandler} implementation that logs the Throwable at error
100
   * level and then propagates it.
101
   */
102
  private static class PropagatingErrorHandler extends LoggingErrorHandler {
103

104
    @Override
105
    public void handleError(Throwable t) {
106
      super.handleError(t);
3✔
107
      ReflectionUtils.rethrowRuntimeException(t);
×
108
    }
×
109
  }
110

111
}
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

© 2025 Coveralls, Inc