View Javadoc
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  	 * @return {@code ExecutionConfig} representing non-terminating execution behavior
17  	 */
18  	public ExecutionConfig nonTerminating() {
19  		this.executionBehavior = ExecutionBehavior.NON_TERMINATING;
20  		return this;
21  	}
22  	
23  	/**
24  	 * 
25  	 * @return {@code ExecutionConfig} representing terminating execution behaivor
26  	 */
27  	public ExecutionConfig terminating() {
28  		this.executionBehavior = ExecutionBehavior.TERMINATING;
29  		return this;
30  	}
31  	
32  	/**
33  	 * @param count number of times retry should happen
34  	 * @return {@code ExecutionConfig} representing immediate retry execution behaivor
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  	 * @param count the number of times retry should happen
45  	 * @param delay the delay
46  	 * @return {@code ExecutionConfig} representing scheduled retry terminating execution behaivor
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  	 * @return the execution behavior
57  	 */
58  	public ExecutionBehavior getExecutionBehavior() {
59  		return executionBehavior;
60  	}
61  	/**
62  	 * 
63  	 * @return the retry count
64  	 */
65  	public int getRetryCount() {
66  		return retryCount;
67  	}
68  	/**
69  	 * 
70  	 * @return the retry delay
71  	 */
72  	public Duration getRetryDelay() {
73  		return retryDelay;
74  	}
75  
76  	/**
77  	 * 
78  	 * @return {@code true} if the {@code ExecutionBehavior} is TERMINATING
79  	 * 			{@code false} otherwise
80  	 */
81  	public boolean isTerminating() {
82  		return ExecutionBehavior.TERMINATING.equals(this.executionBehavior);
83  	}
84  	/**
85  	 * 
86  	 * @return {@code true} if the {@code ExecutionBehavior} is NON_TERMINATING
87  	 * 			{@code false} otherwise
88  	 */
89  	public boolean isNonTerminating() {
90  		return ExecutionBehavior.NON_TERMINATING.equals(this.executionBehavior);
91  	}
92  	/**
93  	 * 
94  	 * @return {@code true} if the {@code ExecutionBehavior} is IMMEDIATE_RETRY_TERMINATING
95  	 * 			{@code false} otherwise
96  	 */
97  	public boolean isImmediatelyRetrying() {
98  		return ExecutionBehavior.IMMEDIATE_RETRY_TERMINATING.equals(this.executionBehavior);
99  	}
100 	
101 	/**
102 	 * 
103 	 * @return {@code true} if the {@code ExecutionBehavior} is SCHEDULED_RETRY_TERMINATING
104 	 * 			{@code false} otherwise
105 	 */
106 	public boolean isScheduledRetrying() {
107 		return ExecutionBehavior.SCHEDULED_RETRY_TERMINATING.equals(this.executionBehavior);
108 	}
109 	/**
110 	 * 
111 	 * @param currentCount the current count
112 	 * @return {@code true} if a retry should be attempted, based on current retries already happened.
113 	 * 			{@code false} otherwise
114 	 */
115 	public boolean shouldRetry(int currentCount) {
116 		return this.retryCount != 0 && this.retryCount >= currentCount;
117 	}
118 	/**
119 	 * Does basic validation to make sure object is valid
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 }