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