MDB and mails: yes it works!


Message Driven Beans (MDBs) are often linked to JMS but actually there is no link at all!

Using ironjacamar you can listen on your mail box :).

To deploy it in tomee the setup is pretty easy: get ironjacamar rar(http://www.ironjacamar.org/download.html), add the api (jar with MailListener) and jboss logging in tomee libs (yes this is not that clean on this part, you can desire to deploy jboss logging + ironjacamar mail.jar in tomee and just a rar with the rar.xml as resource adapter) and then simply define the new MDB container:

<tomee>
  <Resource id="mail-ra" class-name="org.jboss.jca.adapters.mail.MailResourceAdapter" />
  <Container id="mail-mdb-container" type="MESSAGE">
    ResourceAdapter = mail-ra
  </Container>
</tomee>

or simply define the rar which will define the container automatically:

<tomee>
  <Deployments file="/path/to/ironjacamar.rar" />
</tomee>

Then in your project just implement MailListener:

@MessageDriven
public class MyListener implements MailListener {
    @Override
    public void onMessage(final Message message) {
        try {
            System.out.println("Mail >>> " + message.getSubject());
        } catch (final MessagingException e) {
            e.printStackTrace();
        }
    }
}

Then you need to configure it, here is a sample with gmail:

@MessageDriven(activationConfig = {
    @ActivationConfigProperty(propertyName = "mailServer", propertyValue = "pop.gmail.com"),
    @ActivationConfigProperty(propertyName = "port", propertyValue = "995"),
    @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"),
    @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "pop3s"),
    @ActivationConfigProperty(propertyName = "userName", propertyValue="I don't want it here"),
    @ActivationConfigProperty(propertyName = "password", propertyValue="I don't want it here at all"),
    @ActivationConfigProperty(propertyName = "starttls", propertyValue="true"),
    @ActivationConfigProperty(propertyName = "pollingInterval", propertyValue="5000"),
    @ActivationConfigProperty(propertyName = "debug", propertyValue = "false"),
    @ActivationConfigProperty(propertyName = "flush", propertyValue = "false")
})
public class MyListener implements MailListener {
    // ....
}

Main issue is how to set address and password? We don’t want it in annotations for sure but in ejb-jar.xml neither since it will not be easily overridable.

For that TomEE/OpenEJB provides a nice feature: setting activation config properties as system properties.

Here you can set:

-DMyListener.activation.password=changeit -DMyListener.activation.userName=me@gmail.com

And it will be set as if it was in @ActivationConfigProperty :).

Side note: to use it with gmail (pop3s) javax.mail was not working well and geronimo-mail needed a little workaround (fixed on trunk). Here is the workaround: create a META-INF/javamail.providers with the line “protocol=pop3s; type=store; class=org.foo.MyPOP3SSLStore; vendor=Me”, MyPOP3SSLStore extends org.apache.geronimo.javamail.store.pop3.POP3Store and overrides Folder getFolder(String) simply by delegating to super result excepted exist() which is ‘return “INBOX”.equals(name) || superFolder.exists();’.

This is really nice since it opens the door to a bunch of systems: all external systems like a FTP server, a messaging server like XMPP, a SSH server and so on can listen to new inputs through MDBs.

JCA is really the most unknown part of EE but it does worth looking into it since that’s where you can do what you want and then wiring it to CDI is just built-in since you can fire a CDI event from you MDB for instance!

Advertisement

4 thoughts on “MDB and mails: yes it works!

  1. Bart van Leeuwen

    Hi,

    Looks like a great idea, unfortunatly I couldn’t find any of the rar’s jar’s you refering to in your article.
    I’m curious if this would work for IMAP as well.

    Bart

    Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s