0

I am developing an API in spring-integration using DSL, this how it works

JDBC Polling Adapter initiates the flow and gets some data from tables and send it to DefaultRequestChannel, from here the message is handled/flowing thru various channels.

Now I am trying to
1. send a email, if any errors (e.g connectivity issue, bad record found while polling the data) occurred/detected in my error channel.

  1. After sending email to my support group, I want to suspend my flow for 15 mins and then resume automatically.

I tried creating a sendEmailChannel (recipient of my errorChannel), it doesn't work for me. So just created a transformer method like below

this code is running fine, but is it a good practice? @

@Transformer(inputChannel="errorChannel", outputChannel="suspendChannel")
public Message<?> errorChannelHandler(ErrorMessage errorMessage) throws RuntimeException, MessagingException, InterruptedException {

Exception exception = (Exception) errorMessage.getPayload();
    String errorMsg = errorMessage.toString();
    String subject = "API issue";
    if (exception instanceof RuntimeException) {
        errorMsg = "Run time exception";
        subject = "Critical Alert";
    }
    if (exception instanceof JsonParseException) {
        errorMsg = ....;
        subject = .....;
    }

    MimeMessage message = sender.createMimeMessage();
    MimeMessageHelper helper = new MimeMessageHelper(message);
    helper.setFrom(senderEmail);
    helper.setTo(receiverEmail);
    helper.setText(errorMsg);
    helper.setSubject(subject);
    sender.send(message);
    kafkaProducerSwitch.isKafkaDown());
    return MessageBuilder.withPayload(exception.getMessage())

            .build();
}

I am looking for some better way of handling the above logic. And also any suggestions to suspend my flow for few mins.

user2462133
  • 64
  • 3
  • 7

1 Answers1

0

You definitely can use a mail sending channel adapter from Spring Integration box to send those messages from the error channel: https://docs.spring.io/spring-integration/docs/5.1.5.RELEASE/reference/html/#mail-outbound. The Java DSL variant is like this:

.handle(Mail.outboundAdapter("gmail")
                        .port(smtpServer.getPort())
                        .credentials("user", "pw")
                        .protocol("smtp")))

The suspend can be done via CompoundTriggerAdvice extension, when you check the some AtimocBoolean bean for the state to activate one or another trigger in the beforeReceive() implementation. Such a AtimocBoolean can change its state in one more subscriber to that errorChannel because this one is a PublishSubscribeChannel by default. Don't forget to bring the state back to normal after that you return a false from the beforeReceive(). Just because that is enough to mark your system as normal at this moment since it is is going to work only after 15 mins.

Artem Bilan
  • 113,505
  • 11
  • 91
  • 118