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
47
48 public ExecutionConfig scheduledRetrying(int count, Duration delay) {
49 this.executionBehavior = ExecutionBehavior.SCHEDULED_RETRY_TERMINATING;
50 this.retryCount = count;
51 this.retryDelay = delay;
52 return this;
53 }
54
55
56
57
58 public ExecutionBehavior getExecutionBehavior() {
59 return executionBehavior;
60 }
61
62
63
64
65 public int getRetryCount() {
66 return retryCount;
67 }
68
69
70
71
72 public Duration getRetryDelay() {
73 return retryDelay;
74 }
75
76
77
78
79
80
81 public boolean isTerminating() {
82 return ExecutionBehavior.TERMINATING.equals(this.executionBehavior);
83 }
84
85
86
87
88
89 public boolean isNonTerminating() {
90 return ExecutionBehavior.NON_TERMINATING.equals(this.executionBehavior);
91 }
92
93
94
95
96
97 public boolean isImmediatelyRetrying() {
98 return ExecutionBehavior.IMMEDIATE_RETRY_TERMINATING.equals(this.executionBehavior);
99 }
100
101
102
103
104
105
106 public boolean isScheduledRetrying() {
107 return ExecutionBehavior.SCHEDULED_RETRY_TERMINATING.equals(this.executionBehavior);
108 }
109
110
111
112
113
114
115 public boolean shouldRetry(int currentCount) {
116 return this.retryCount != 0 && this.retryCount >= currentCount;
117 }
118
119
120
121 public void validate() {
122 if (isScheduledRetrying()) {
123 checkNotNull(this.retryDelay, "retryDelay should be specified for " + ExecutionBehavior.SCHEDULED_RETRY_TERMINATING);
124 checkArgument(this.getRetryDelay().getDuration() > 0, "Retry delay duration should be greater than ZERO");
125 }
126 }
127 }