1 package com.github.dexecutor.core;
2
3 import static com.github.dexecutor.core.support.Preconditions.*;
4
5 public class ExecutionConfig {
6
7 private ExecutionBehavior executionBehavior;
8 private int retryCount = 0;
9 private Duration retryDelay = Duration.MINIMAL_DURATION;
10
11 public static final ExecutionConfig TERMINATING = new ExecutionConfig().terminating();
12 public static final ExecutionConfig NON_TERMINATING = new ExecutionConfig().nonTerminating();
13
14
15
16
17
18 public ExecutionConfig nonTerminating() {
19 this.executionBehavior = ExecutionBehavior.NON_TERMINATING;
20 return this;
21 }
22
23
24
25
26
27 public ExecutionConfig terminating() {
28 this.executionBehavior = ExecutionBehavior.TERMINATING;
29 return this;
30 }
31
32
33
34
35
36 public ExecutionConfig immediateRetrying(int count) {
37 this.executionBehavior = ExecutionBehavior.IMMEDIATE_RETRY_TERMINATING;
38 this.retryCount = count;
39 return this;
40 }
41
42
43
44
45
46 public ExecutionConfig scheduledRetrying(int count, Duration delay) {
47 this.executionBehavior = ExecutionBehavior.SCHEDULED_RETRY_TERMINATING;
48 this.retryCount = count;
49 this.retryDelay = delay;
50 return this;
51 }
52
53
54
55
56 public ExecutionBehavior getExecutionBehavior() {
57 return executionBehavior;
58 }
59
60
61
62
63 public int getRetryCount() {
64 return retryCount;
65 }
66
67
68
69
70 public Duration getRetryDelay() {
71 return retryDelay;
72 }
73
74
75
76
77
78
79 public boolean isTerminating() {
80 return ExecutionBehavior.TERMINATING.equals(this.executionBehavior);
81 }
82
83
84
85
86
87 public boolean isNonTerminating() {
88 return ExecutionBehavior.NON_TERMINATING.equals(this.executionBehavior);
89 }
90
91
92
93
94
95 public boolean isImmediatelyRetrying() {
96 return ExecutionBehavior.IMMEDIATE_RETRY_TERMINATING.equals(this.executionBehavior);
97 }
98
99
100
101
102
103
104 public boolean isScheduledRetrying() {
105 return ExecutionBehavior.SCHEDULED_RETRY_TERMINATING.equals(this.executionBehavior);
106 }
107
108
109
110
111
112
113 public boolean shouldRetry(int currentCount) {
114 return this.retryCount != 0 && this.retryCount > currentCount;
115 }
116
117
118
119 public void validate() {
120 if (isScheduledRetrying()) {
121 checkNotNull(this.retryDelay, "retryDelay should be specified for " + ExecutionBehavior.SCHEDULED_RETRY_TERMINATING);
122 checkArgument(this.getRetryDelay().getDuration() > 0, "Retry delay duration should be greater than ZERO");
123 }
124 }
125 }