Skip to content

Different corePoolSize for different Mailer instances (with using batch module) #565

Closed

Description

@elalbertinoprimo

I am trying to configure different corePoolSize for 2 Mailer instances, but so far without success. As i understand from documentation, all i need is to define different clusterKey for each Mailer. But it doesn`t work as I expect. I also use batch-module.

  1. new SmtpConnectionPoolClustered beeing created here just for 1st Mailer. For 2nd I am getting warning "Global SMTP Connection pool is already configured with pool defaults ...".
    private void ensureClusterInitialized(@NotNull OperationalConfig operationalConfig) {
    if (smtpConnectionPool == null) {
    LOGGER.warn("Starting SMTP connection pool cluster: JVM won't shutdown until the pool is manually closed with mailer.shutdownConnectionPool() (for each mailer in the cluster)");
    smtpConnectionPool = new SmtpConnectionPoolClustered(configureSmtpClusterConfig(operationalConfig));
    } else if (compareClusterConfig(operationalConfig, smtpConnectionPool.getClusterConfig())) {
    LOGGER.warn("Global SMTP Connection pool is already configured with pool defaults from the first Mailer instance, ignoring relevant properties from {}", operationalConfig);
    }
    }
  2. So there is just one instance of ResourceClusters created with clusterConfig (including corePoolSize) from 1st MailerBuilder cofig. Then it`s beeing reused for 2nd cluster as well.
    https://github.com/bbottema/clustered-object-pool/blob/5857ced611c5be3489cd3cb763bfce8421aab9d9/src/main/java/org/bbottema/clusteredobjectpool/core/ResourceClusters.java#L55

My config:

    @Bean(name = FIRST_SERVER_HOST)
    public Mailer mailerFirst() {
        return MailerBuilder
                .withSMTPServer(FIRST_SERVER_HOST, FIRST_SERVER_PORT)
                .async()
                .withThreadPoolSize(20)
                .withConnectionPoolCoreSize(1)
                .withClusterKey(UUID.randomUUID())
                .buildMailer();
    }

    @Bean(name = SECOND_SERVER_HOST)
    public Mailer mailerSecond() {
        return MailerBuilder
                .withSMTPServer(SECOND_SERVER_HOST, SECOND_SERVER_PORT)
                .async()
                .withThreadPoolSize(20)
                .withConnectionPoolCoreSize(0)
                .withClusterKey(UUID.randomUUID())
                .buildMailer();
    }

Activity

  1. bbottema commented on Dec 12, 2024

    @bbottema
    Owner

    Hello!

    There are some properties that are considered globally applicable (as defaults) for all clusters and cannot be changed after the initial registration. These are:

    • connection pool core size
    • connection pool max size
    • load balancing strategy
    • expiration policy

    The properties that can be provided differently per cluster are:

    • connection pool claim timeout

    If this doesn't work for you, please let me know your use case. Also, it would help me to know which part of the documentation wasn't clear enough so I can improve it. Thanks!

  2. elalbertinoprimo commented on Dec 13, 2024

    @elalbertinoprimo
    Author

    Hello! Thank you for reply.

    In my case we work with 2 SMTP-servers, one of which has a problem with handling a large number of open connections. So I would like to set a different value "connection pool core size" for it (0).

    Regarding documentation, "(core size, max size etc.) are set and fixed by the first Mailer instance in the cluster " made me think that i can configure it different way for different clusters.
    https://www.simplejavamail.org/configuration.html#section-batch-and-clustering:~:text=Note%202%3A-,The%20Connection%20Pool%20defaults%20(core%20size%2C%20max%20size%20etc.)%20are%20set%20and%20fixed%20by%20the%20first%20Mailer%20instance%20in%20the%20cluster.,-Subsequent%20Mailer%20instances

  3. added this to the 9.0.0 milestone on Jul 6, 2026
  4. added
    dependencies
    on Jul 6, 2026
  5. bbottema commented on Jul 6, 2026

    @bbottema
    Owner

    Fixed by making batch-module pool defaults scoped to the cluster key. Different cluster keys can now keep different Java API connection-pool defaults; mailers sharing the same cluster key still keep the first Mailer config for that cluster.

    UUID normalCluster = UUID.randomUUID();
    UUID constrainedCluster = UUID.randomUUID();
    
    Mailer normal = MailerBuilder
        .withSMTPServer("smtp-a.example.com", 587, username, password)
        .withClusterKey(normalCluster)
        .withConnectionPoolCoreSize(2)
        .withConnectionPoolMaxSize(8)
        .buildMailer();
    
    Mailer constrained = MailerBuilder
        .withSMTPServer("smtp-b.example.com", 587, username, password)
        .withClusterKey(constrainedCluster)
        .withConnectionPoolCoreSize(0)
        .withConnectionPoolMaxSize(1)
        .buildMailer();

    The upstream pieces are tracked in bbottema/clustered-object-pool#6 and simple-java-mail/smtp-connection-pool#8. Property-only multi-cluster configuration remains tracked separately in #618.

  6. bbottema commented on Jul 8, 2026

    @bbottema
    Owner

    Released in Simple Java Mail v9.0.0, which is now available from Maven Central:

    https://repo1.maven.org/maven2/org/simplejavamail/simple-java-mail/9.0.0/

    Release notes:
    https://github.com/bbottema/simple-java-mail/releases/tag/9.0.0

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Priority-Lowbugdependencies

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions