|
| 1 | +package com.javaquery.util; |
| 2 | + |
| 3 | +import com.fasterxml.jackson.annotation.JsonProperty; |
| 4 | +import com.javaquery.util.logging.Action; |
| 5 | +import com.javaquery.util.time.Dates; |
| 6 | + |
| 7 | +import java.util.Date; |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +/** |
| 12 | + * @author vicky.thakor |
| 13 | + * @since 1.2.0 |
| 14 | + */ |
| 15 | +public class ExecutionContext<T> { |
| 16 | + |
| 17 | + @JsonProperty("request_id") |
| 18 | + private final String requestId; |
| 19 | + |
| 20 | + @JsonProperty("reference_id") |
| 21 | + private T referenceId; |
| 22 | + private final Action action; |
| 23 | + private Map<String, Object> meta; |
| 24 | + |
| 25 | + @JsonProperty("max_retries") |
| 26 | + private Integer maxRetries = 5; |
| 27 | + |
| 28 | + @JsonProperty("retries_attempted") |
| 29 | + private Integer retriesAttempted = 0; |
| 30 | + |
| 31 | + @JsonProperty("created_at") |
| 32 | + private final Date createdAt; |
| 33 | + |
| 34 | + public ExecutionContext(String requestId, T referenceId, Action action) { |
| 35 | + this.requestId = requestId; |
| 36 | + this.referenceId = referenceId; |
| 37 | + this.action = action; |
| 38 | + this.meta = new HashMap<>(); |
| 39 | + this.createdAt = Dates.current(); |
| 40 | + } |
| 41 | + |
| 42 | + public ExecutionContext(T referenceId, Action action) { |
| 43 | + this.requestId = UniqueIdGenerator.generate(); |
| 44 | + this.referenceId = referenceId; |
| 45 | + this.action = action; |
| 46 | + this.meta = new HashMap<>(); |
| 47 | + this.createdAt = Dates.current(); |
| 48 | + } |
| 49 | + |
| 50 | + public ExecutionContext(Action action) { |
| 51 | + this.requestId = UniqueIdGenerator.generate(); |
| 52 | + this.action = action; |
| 53 | + this.meta = new HashMap<>(); |
| 54 | + this.createdAt = Dates.current(); |
| 55 | + } |
| 56 | + |
| 57 | + public ExecutionContext(T referenceId, Action action, Integer maxRetries) { |
| 58 | + this.requestId = UniqueIdGenerator.generate(); |
| 59 | + this.referenceId = referenceId; |
| 60 | + this.action = action; |
| 61 | + this.maxRetries = maxRetries; |
| 62 | + this.meta = new HashMap<>(); |
| 63 | + this.createdAt = Dates.current(); |
| 64 | + } |
| 65 | + |
| 66 | + public ExecutionContext(Action action, Integer maxRetries) { |
| 67 | + this.requestId = UniqueIdGenerator.generate(); |
| 68 | + this.action = action; |
| 69 | + this.maxRetries = maxRetries; |
| 70 | + this.meta = new HashMap<>(); |
| 71 | + this.createdAt = Dates.current(); |
| 72 | + } |
| 73 | + |
| 74 | + public String getRequestId() { |
| 75 | + return requestId; |
| 76 | + } |
| 77 | + |
| 78 | + public T getReferenceId() { |
| 79 | + return referenceId; |
| 80 | + } |
| 81 | + |
| 82 | + public Action getAction() { |
| 83 | + return action; |
| 84 | + } |
| 85 | + |
| 86 | + public Map<String, Object> getMeta() { |
| 87 | + return meta; |
| 88 | + } |
| 89 | + |
| 90 | + public void setMeta(Map<String, Object> meta) { |
| 91 | + this.meta = meta; |
| 92 | + } |
| 93 | + |
| 94 | + public Object getMeta(String key, Object defaultValue){ |
| 95 | + return meta.getOrDefault(key, defaultValue); |
| 96 | + } |
| 97 | + |
| 98 | + public void addMeta(String key, Object value){ |
| 99 | + this.meta.put(key, value); |
| 100 | + } |
| 101 | + |
| 102 | + public Integer getMaxRetries() { |
| 103 | + return maxRetries; |
| 104 | + } |
| 105 | + |
| 106 | + public Integer getRetriesAttempted() { |
| 107 | + return retriesAttempted; |
| 108 | + } |
| 109 | + |
| 110 | + public Date getCreatedAt() { |
| 111 | + return createdAt; |
| 112 | + } |
| 113 | + |
| 114 | + public void addRetriesAttempted(Integer retriesAttempted) { |
| 115 | + this.retriesAttempted += retriesAttempted; |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public String toString() { |
| 120 | + return "ExecutionContext{" + |
| 121 | + "requestId='" + requestId + '\'' + |
| 122 | + ", referenceId=" + referenceId + |
| 123 | + ", action=" + action + |
| 124 | + ", maxRetries=" + maxRetries + |
| 125 | + ", retriesAttempted=" + retriesAttempted + |
| 126 | + ", createdAt=" + createdAt + |
| 127 | + '}'; |
| 128 | + } |
| 129 | +} |
0 commit comments