1 | package fr.sii.ogham.core.sender; | |
2 | ||
3 | import static fr.sii.ogham.core.util.LogUtils.logString; | |
4 | ||
5 | import java.util.ArrayList; | |
6 | import java.util.Arrays; | |
7 | import java.util.List; | |
8 | ||
9 | import org.slf4j.Logger; | |
10 | import org.slf4j.LoggerFactory; | |
11 | ||
12 | import fr.sii.ogham.core.exception.MessageException; | |
13 | import fr.sii.ogham.core.exception.MessageNotSentException; | |
14 | import fr.sii.ogham.core.exception.MultipleCauseExceptionWrapper; | |
15 | import fr.sii.ogham.core.message.Message; | |
16 | ||
17 | /** | |
18 | * Decorator implementation that will try to send the message until one | |
19 | * decorated sender is able to send it. The aim is that if a sender fails to | |
20 | * send the message, then another will send it. It can ensure that message will | |
21 | * be sent at any costs. | |
22 | * | |
23 | * @author Aurélien Baudet | |
24 | * | |
25 | */ | |
26 | public class FallbackSender implements MessageSender { | |
27 | private static final Logger LOG = LoggerFactory.getLogger(FallbackSender.class); | |
28 | ||
29 | /** | |
30 | * The list of senders to try one by one until one succeeds | |
31 | */ | |
32 | private List<MessageSender> senders; | |
33 | ||
34 | /** | |
35 | * Initialize either none, one or several senders to try one by one until | |
36 | * one succeeds. | |
37 | * | |
38 | * @param senders | |
39 | * the senders to register | |
40 | */ | |
41 | public FallbackSender(MessageSender... senders) { | |
42 | this(Arrays.asList(senders)); | |
43 | } | |
44 | ||
45 | /** | |
46 | * Initialize with the provided list of senders to try one by one until one | |
47 | * succeeds. | |
48 | * | |
49 | * @param senders | |
50 | * the senders to register | |
51 | */ | |
52 | public FallbackSender(List<MessageSender> senders) { | |
53 | super(); | |
54 | this.senders = senders; | |
55 | } | |
56 | ||
57 | @Override | |
58 | @SuppressWarnings("squid:S2221") | |
59 | public void send(Message message) throws MessageException { | |
60 | List<Exception> causes = new ArrayList<>(); | |
61 | for (MessageSender sender : senders) { | |
62 | try { | |
63 | LOG.debug("Try to send message {} using sender {}", logString(message), sender); | |
64 |
2
1. send : removed call to fr/sii/ogham/core/sender/MessageSender::send → NO_COVERAGE 2. send : removed call to fr/sii/ogham/core/sender/MessageSender::send → KILLED |
sender.send(message); |
65 | LOG.debug("Message {} sent using sender {}", logString(message), sender); | |
66 | return; | |
67 | } catch (Exception e) { | |
68 | LOG.debug("Message {} couldn't be sent using sender {}. Cause: {}", logString(message), sender, e.getMessage()); | |
69 | LOG.trace("", e); | |
70 | causes.add(e); | |
71 | } | |
72 | } | |
73 | throw new MessageNotSentException("No sender could handle the message", message, new MultipleCauseExceptionWrapper(causes)); | |
74 | } | |
75 | ||
76 | /** | |
77 | * Register a new sender to try. The sender is added at the end. It will be | |
78 | * used only after all previously registered senders have failed. | |
79 | * | |
80 | * @param sender | |
81 | * the sender to register | |
82 | */ | |
83 | public void addSender(MessageSender sender) { | |
84 | senders.add(sender); | |
85 | } | |
86 | } | |
Mutations | ||
64 |
1.1 2.2 |