Skip to content

Bug: batch-module gives error when there is a custom mailer #375

Closed

Description

@andrus

I am using SJM 6.x via Bootique integration. One feature that I need myself and is being requested by other Bootique users is the ability to override recipients of a message for testing purposes. Something like this:

transport.sendMessage(message, testRecipients);

This is ideal for QA, as it preserves the real To, Cc, Bcc, but delivers to a designated test mailbox. I tried implementing it as a CustomMailer in Bootique. Unfortunately it is not easy to do it, as the presence of CustomMailer prevents SJM stack from configuring itself properly. So a call like this results in an NPE:

@Override
public void sendMessage(
    OperationalConfig operationalConfig, 
    Session session, 
    Email email, 
    MimeMessage message) {
    try {
        TransportRunner.sendMessage(operationalConfig.getClusterKey(), session, message, overriddenRecipients);
    } catch (MessagingException e) {
        ...
    }
}
java.lang.NullPointerException: Connection pool used before it was initialized. This shouldn't be possible.
at java.base/java.util.Objects.requireNonNull(Objects.java:246)
at org.simplejavamail.internal.batchsupport.BatchSupport.acquireTransport(BatchSupport.java:110)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendUsingConnectionPool(TransportRunner.java:84)
at org.simplejavamail.mailer.internal.util.TransportRunner.runOnSessionTransport(TransportRunner.java:72)
at org.simplejavamail.mailer.internal.util.TransportRunner.sendMessage(TransportRunner.java:48)

Wanted to ask for an expert opinion on a better way to do a custom dispatch via TransportRunner. Or maybe there's interest to make recipient override feature (which has been quite handy with a different mail stack) a part of SJM?

Activity

  1. bbottema commented on Jan 13, 2022

    @bbottema
    Owner

    So I understand what you're trying to do, but that's not how the CustomMailer was designed. The point was to be able to replace the sending engine with your own, not tweak mails coming through and then still use the initial (internal) sending engine (using TransportRunner).

    So what's happening here is that you have both a CustomMailer, but then also the batch-module on the classpath. Currently, Simple Java Mail skips the cluster when a custom mailer is present. Your method would work find if you removed the batch-module. The reasoning here is that the batch module prevents the JVM from shutting down (because you need to stop the clusters/connection pool manually) and that doesn't make sense if you provide your own custom mailer implementation.

    One thing I am considering is that if both CustomMailer and batch-module are available, Simple Java Mail will throw a more descriptive error rather than some vague statement about a connection pool.

    I'm not sure otherwise how to elegantly define an API on the builder that flags the system to still use batch-module even though there is a custom mailer.

  2. andrus commented on Jan 14, 2022

    @andrus
    Author

    Thanks for the reply. It confirms what I inferred about CustomMailer's intent.

    The reasoning here is that the batch module prevents the JVM from shutting down (because you need to stop the clusters/connection pool manually) and that doesn't make sense if you provide your own custom mailer implementation.

    If a user has a CustomMailer that does not rely on the batch-module, they can simply not include batch-module on the classpath. So if batch-module is on classpath, we can assume this was a conscious decision, and hence it needs to be initialized whether there is a CustomMailer or not.

    Does this logic make sense?

  3. bbottema commented on Jan 14, 2022

    @bbottema
    Owner

    It does and I will give this some more thought...

  4. andrus commented on Jan 21, 2022

    @andrus
    Author

    @bbottema . So what do you think? I may even try and create a PR for this task if that's the direction you'd like to proceed.

  5. added a commit that references this issue on Jan 22, 2022
  6. changed the title [-]Recipient override for testing[/-] [+]Bug: batch-module gives error when there is a custom mailer[/+] on Jan 22, 2022
  7. added this to the 7.0.1 milestone on Jan 22, 2022
  8. bbottema commented on Jan 22, 2022

    @bbottema
    Owner

    I've released a fix for this in 7.0.1. Enjoy!

  9. andrus commented on Jan 22, 2022

    @andrus
    Author

    This was fast :) Thanks!

    What do you think about backporting it to 6.x? In Bootique I would like to support both javax and jakarta APIs for some time

  10. added a commit that references this issue on Jan 22, 2022
  11. bbottema commented on Jan 22, 2022

    @bbottema
    Owner

    Done. It's in 6.7.6

  12. andrus commented on Jan 22, 2022

    @andrus
    Author

    Much appreciated!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions