1 | package fr.sii.ogham.sms.sender.impl.cloudhopper.session; | |
2 | ||
3 | import static java.util.concurrent.TimeUnit.MILLISECONDS; | |
4 | ||
5 | import java.util.concurrent.ExecutorService; | |
6 | import java.util.concurrent.Executors; | |
7 | import java.util.concurrent.ScheduledExecutorService; | |
8 | import java.util.concurrent.ScheduledFuture; | |
9 | import java.util.function.Supplier; | |
10 | ||
11 | import org.slf4j.Logger; | |
12 | import org.slf4j.LoggerFactory; | |
13 | ||
14 | import com.cloudhopper.smpp.SmppSession; | |
15 | import com.cloudhopper.smpp.pdu.EnquireLink; | |
16 | ||
17 | import fr.sii.ogham.core.exception.MessageException; | |
18 | import fr.sii.ogham.core.retry.RetryExecutor; | |
19 | import fr.sii.ogham.core.service.MessagingService; | |
20 | import fr.sii.ogham.sms.builder.cloudhopper.SmppClientSupplier; | |
21 | import fr.sii.ogham.sms.builder.cloudhopper.SmppSessionHandlerSupplier; | |
22 | import fr.sii.ogham.sms.message.Sms; | |
23 | import fr.sii.ogham.sms.sender.impl.cloudhopper.ExtendedSmppSessionConfiguration; | |
24 | import fr.sii.ogham.sms.sender.impl.cloudhopper.exception.SmppException; | |
25 | ||
26 | /** | |
27 | * Strategy that regularly sends {@link EnquireLink} requests to keep session | |
28 | * alive. | |
29 | * | |
30 | * <p> | |
31 | * A session is initiated either at startup or when the first message is about | |
32 | * to be sent. Once the session is initialized, several messages can be sent | |
33 | * using the same session. Then it may take some time before a message is sent | |
34 | * again. In order to avoid initializing a new SMPP session again, the | |
35 | * connection with the server is maintained. To do so, a task is started in the | |
36 | * background in order to regularly send {@link EnquireLink} requests to the | |
37 | * server. This way the connection is maintained actively. | |
38 | * | |
39 | * <p> | |
40 | * The opened session is automatically closed when the {@link MessagingService} | |
41 | * is released or when a cleanup is explicitly requested. | |
42 | * | |
43 | * <p> | |
44 | * If the {@link EnquireLinkTask} fails, the error is analyzed using an | |
45 | * {@link ErrorAnalyzer}. This analyzer indicates if the triggered error is | |
46 | * raised because the connection with the server seems to be lost. In this case, | |
47 | * the current connection must be closed and a new session is created. | |
48 | * | |
49 | * <p> | |
50 | * If a message can't be sent, the raised error is also analyzed to check | |
51 | * whether a new session has to be created. The failed SMS is not re-sent. | |
52 | * Instead the original error is thrown. This way, the global retry handling can | |
53 | * be used. A new session is requested in background. | |
54 | * | |
55 | * <p> | |
56 | * If the reconnection fails (for example, if the server is down). The | |
57 | * reconnection process will be attempted only once (however there may have | |
58 | * several bind requests using the {@link RetryExecutor}). When a new message | |
59 | * has to be sent, the reconnection will be attempted at this moment. | |
60 | * | |
61 | * @author Aurélien Baudet | |
62 | * | |
63 | */ | |
64 | public class KeepSessionAliveStrategy extends BaseSessionHandlingStrategy implements ErrorHandler { | |
65 | private static final Logger LOG = LoggerFactory.getLogger(KeepSessionAliveStrategy.class); | |
66 | ||
67 | private final Supplier<ScheduledExecutorService> timerSupplier; | |
68 | private final ErrorAnalyzer errorAnalyzer; | |
69 | private final ErrorHandler reconnectionErrorHandler; | |
70 | private ScheduledExecutorService currentTimer; | |
71 | private ScheduledFuture<?> enquireLinkTask; | |
72 | private volatile boolean reconnecting; | |
73 | ||
74 | public KeepSessionAliveStrategy(ExtendedSmppSessionConfiguration configuration, SmppClientSupplier clientSupplier, SmppSessionHandlerSupplier smppSessionHandlerSupplier, RetryExecutor retry, | |
75 | Supplier<ScheduledExecutorService> timerSupplier, ErrorAnalyzer errorAnalyzer, ErrorHandler reconnectionErrorHandler) { | |
76 | super(LOG, configuration, clientSupplier, smppSessionHandlerSupplier, retry); | |
77 | this.timerSupplier = timerSupplier; | |
78 | this.errorAnalyzer = errorAnalyzer; | |
79 | this.reconnectionErrorHandler = reconnectionErrorHandler; | |
80 |
2
1. <init> : negated conditional → NO_COVERAGE 2. <init> : negated conditional → KILLED |
if (configuration.getKeepAlive().isConnectAtStartup(false)) { |
81 |
2
1. <init> : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryConnect → NO_COVERAGE 2. <init> : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryConnect → KILLED |
tryConnect(); |
82 | } | |
83 | } | |
84 | ||
85 | @Override | |
86 | public SmppSession getSession() throws SmppException { | |
87 |
2
1. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → NO_COVERAGE 2. getSession : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → KILLED |
initSessionIfNeeded(); |
88 |
2
1. getSession : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::getSession → NO_COVERAGE 2. getSession : replaced return value with null for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::getSession → KILLED |
return currentSession; |
89 | } | |
90 | ||
91 | @Override | |
92 | public void messageSent(Sms sms) { | |
93 | // nothing to do | |
94 | } | |
95 | ||
96 | @Override | |
97 | public void messageNotSent(Sms sms, SmppException e) throws MessageException { | |
98 |
2
1. messageNotSent : negated conditional → NO_COVERAGE 2. messageNotSent : negated conditional → KILLED |
if (errorAnalyzer.requiresNewConnection(e)) { |
99 |
2
1. messageNotSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnectInBackground → NO_COVERAGE 2. messageNotSent : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnectInBackground → KILLED |
reconnectInBackground(e); |
100 | // Throw the original exception so that message may be handled | |
101 | // (maybe send will be retried) | |
102 | // Adds some contextual information if possible | |
103 | throw new MessageException("Failed to send SMS because it seems that the current session is broken. A new SMPP session is requested in background", sms, e); | |
104 | } | |
105 | throw new MessageException("Failed to send SMS", sms, e); | |
106 | } | |
107 | ||
108 | @Override | |
109 | public void handleFailure(Throwable e) { | |
110 |
2
1. handleFailure : negated conditional → NO_COVERAGE 2. handleFailure : negated conditional → TIMED_OUT |
if (errorAnalyzer.requiresNewConnection(e)) { |
111 |
2
1. handleFailure : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnectInBackground → NO_COVERAGE 2. handleFailure : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnectInBackground → TIMED_OUT |
reconnectInBackground(e); |
112 | } | |
113 | } | |
114 | ||
115 | @Override | |
116 | public void messageProcessed(Sms sms) { | |
117 | // nothing to do | |
118 | } | |
119 | ||
120 | @Override | |
121 | public void clean() { | |
122 |
2
1. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::stopEnquiredLinkTask → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::stopEnquiredLinkTask → KILLED |
stopEnquiredLinkTask(); |
123 |
2
1. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::destroySession → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::destroySession → TIMED_OUT |
destroySession(); |
124 |
2
1. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::destroyClient → NO_COVERAGE 2. clean : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::destroyClient → TIMED_OUT |
destroyClient(); |
125 | } | |
126 | ||
127 | private void tryConnect() { | |
128 | try { | |
129 |
2
1. tryConnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → NO_COVERAGE 2. tryConnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → KILLED |
initSessionIfNeeded(); |
130 | } catch (SmppException e) { | |
131 | LOG.warn("Connection at startup was requested but couldn't be achived. Connection will be re-attempted when first message is sent", e); | |
132 | } | |
133 | } | |
134 | ||
135 | private void initSessionIfNeeded() throws SmppException { | |
136 |
2
1. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initClient → NO_COVERAGE 2. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initClient → KILLED |
initClient(); |
137 |
2
1. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSession → NO_COVERAGE 2. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSession → KILLED |
initSession(); |
138 |
2
1. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::startEnquiredLinkTask → NO_COVERAGE 2. initSessionIfNeeded : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::startEnquiredLinkTask → TIMED_OUT |
startEnquiredLinkTask(); |
139 | } | |
140 | ||
141 | private synchronized void startEnquiredLinkTask() { | |
142 |
4
1. startEnquiredLinkTask : negated conditional → NO_COVERAGE 2. startEnquiredLinkTask : negated conditional → NO_COVERAGE 3. startEnquiredLinkTask : negated conditional → TIMED_OUT 4. startEnquiredLinkTask : negated conditional → TIMED_OUT |
if (currentSession == null || isEnquireLinkTaskRunning()) { |
143 | return; | |
144 | } | |
145 | // TODO: only run when no activity ? take the date of the last sent | |
146 | // message ? | |
147 | long enquireLinkRequestTimeout = configuration.getKeepAlive().getEnquireLinkTimeout(); | |
148 | long enquireLinkInterval = configuration.getKeepAlive().getEnquireLinkInterval(); | |
149 | LOG.debug("Start sending EnquireLink requests every {}ms", enquireLinkInterval); | |
150 | currentTimer = timerSupplier.get(); | |
151 | enquireLinkTask = currentTimer.scheduleWithFixedDelay(new EnquireLinkTask(currentSession, this, enquireLinkRequestTimeout), enquireLinkInterval, enquireLinkInterval, MILLISECONDS); | |
152 | } | |
153 | ||
154 | private boolean isEnquireLinkTaskRunning() { | |
155 |
8
1. isEnquireLinkTaskRunning : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::isEnquireLinkTaskRunning → NO_COVERAGE 2. isEnquireLinkTaskRunning : negated conditional → NO_COVERAGE 3. isEnquireLinkTaskRunning : negated conditional → NO_COVERAGE 4. isEnquireLinkTaskRunning : negated conditional → NO_COVERAGE 5. isEnquireLinkTaskRunning : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::isEnquireLinkTaskRunning → TIMED_OUT 6. isEnquireLinkTaskRunning : negated conditional → KILLED 7. isEnquireLinkTaskRunning : negated conditional → KILLED 8. isEnquireLinkTaskRunning : negated conditional → KILLED |
return enquireLinkTask != null && !enquireLinkTask.isCancelled() && !enquireLinkTask.isDone(); |
156 | } | |
157 | ||
158 | private void stopEnquiredLinkTask() { | |
159 |
2
1. stopEnquiredLinkTask : negated conditional → NO_COVERAGE 2. stopEnquiredLinkTask : negated conditional → KILLED |
if (currentTimer != null) { |
160 | currentTimer.shutdownNow(); | |
161 | currentTimer = null; | |
162 | } | |
163 |
2
1. stopEnquiredLinkTask : negated conditional → NO_COVERAGE 2. stopEnquiredLinkTask : negated conditional → KILLED |
if (enquireLinkTask != null) { |
164 | LOG.debug("Stop sending EnquireLink requests"); | |
165 | enquireLinkTask.cancel(true); | |
166 | enquireLinkTask = null; | |
167 | } | |
168 | } | |
169 | ||
170 | private boolean tryReconnect(Throwable failureRequiringReconnection) { | |
171 | try { | |
172 | LOG.debug("Reconnecting due to error that requires a new session", failureRequiringReconnection); | |
173 |
2
1. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnect → NO_COVERAGE 2. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::reconnect → TIMED_OUT |
reconnect(); |
174 | LOG.debug("Reconnected"); | |
175 |
2
1. tryReconnect : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryReconnect → SURVIVED 2. tryReconnect : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryReconnect → NO_COVERAGE |
return true; |
176 | } catch (SmppException se) { | |
177 | LOG.debug("Failed to reconnect. Stopping active keep alive... Reconnection and active keep alive will be attempted later (either using another strategy or when sending next message)", | |
178 | failureRequiringReconnection); | |
179 |
2
1. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::clean → NO_COVERAGE 2. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::clean → TIMED_OUT |
clean(); |
180 |
2
1. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/ErrorHandler::handleFailure → NO_COVERAGE 2. tryReconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/ErrorHandler::handleFailure → SURVIVED |
reconnectionErrorHandler.handleFailure(se); |
181 | LOG.debug("Active keep alive stopped...", se); | |
182 | } | |
183 |
2
1. tryReconnect : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryReconnect → NO_COVERAGE 2. tryReconnect : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::tryReconnect → SURVIVED |
return false; |
184 | } | |
185 | ||
186 | private void reconnect() throws SmppException { | |
187 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::clean → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::clean → TIMED_OUT |
clean(); |
188 |
2
1. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → NO_COVERAGE 2. reconnect : removed call to fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::initSessionIfNeeded → TIMED_OUT |
initSessionIfNeeded(); |
189 | } | |
190 | ||
191 | private boolean alreadyReconnecting() { | |
192 |
4
1. alreadyReconnecting : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::alreadyReconnecting → NO_COVERAGE 2. alreadyReconnecting : replaced boolean return with false for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::alreadyReconnecting → SURVIVED 3. alreadyReconnecting : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::alreadyReconnecting → NO_COVERAGE 4. alreadyReconnecting : replaced boolean return with true for fr/sii/ogham/sms/sender/impl/cloudhopper/session/KeepSessionAliveStrategy::alreadyReconnecting → TIMED_OUT |
return reconnecting; |
193 | } | |
194 | ||
195 | private void reconnectInBackground(Throwable failureRequiringReconnection) { | |
196 |
2
1. reconnectInBackground : negated conditional → NO_COVERAGE 2. reconnectInBackground : negated conditional → TIMED_OUT |
if (alreadyReconnecting()) { |
197 | return; | |
198 | } | |
199 | ExecutorService executor = Executors.newSingleThreadExecutor(); | |
200 |
2
1. reconnectInBackground : removed call to java/util/concurrent/ExecutorService::execute → NO_COVERAGE 2. reconnectInBackground : removed call to java/util/concurrent/ExecutorService::execute → TIMED_OUT |
executor.execute(new ReconnectionTask(failureRequiringReconnection)); |
201 |
2
1. reconnectInBackground : removed call to java/util/concurrent/ExecutorService::shutdown → SURVIVED 2. reconnectInBackground : removed call to java/util/concurrent/ExecutorService::shutdown → NO_COVERAGE |
executor.shutdown(); |
202 | } | |
203 | ||
204 | private class ReconnectionTask implements Runnable { | |
205 | private final Throwable failureRequiringReconnection; | |
206 | ||
207 | public ReconnectionTask(Throwable failureRequiringReconnection) { | |
208 | super(); | |
209 | this.failureRequiringReconnection = failureRequiringReconnection; | |
210 | } | |
211 | ||
212 | @Override | |
213 | public void run() { | |
214 | reconnecting = true; | |
215 | tryReconnect(failureRequiringReconnection); | |
216 | reconnecting = false; | |
217 | } | |
218 | ||
219 | } | |
220 | } | |
Mutations | ||
80 |
1.1 2.2 |
|
81 |
1.1 2.2 |
|
87 |
1.1 2.2 |
|
88 |
1.1 2.2 |
|
98 |
1.1 2.2 |
|
99 |
1.1 2.2 |
|
110 |
1.1 2.2 |
|
111 |
1.1 2.2 |
|
122 |
1.1 2.2 |
|
123 |
1.1 2.2 |
|
124 |
1.1 2.2 |
|
129 |
1.1 2.2 |
|
136 |
1.1 2.2 |
|
137 |
1.1 2.2 |
|
138 |
1.1 2.2 |
|
142 |
1.1 2.2 3.3 4.4 |
|
155 |
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 |
|
159 |
1.1 2.2 |
|
163 |
1.1 2.2 |
|
173 |
1.1 2.2 |
|
175 |
1.1 2.2 |
|
179 |
1.1 2.2 |
|
180 |
1.1 2.2 |
|
183 |
1.1 2.2 |
|
187 |
1.1 2.2 |
|
188 |
1.1 2.2 |
|
192 |
1.1 2.2 3.3 4.4 |
|
196 |
1.1 2.2 |
|
200 |
1.1 2.2 |
|
201 |
1.1 2.2 |