Question

I'm trying to receive emails with a message-driven bean. I'm using jboss wildfly. Unfortunatley, I can't find a real documentation on it. This is the most I've found so far:

@MessageDriven(activationConfig={
        @ActivationConfigProperty(propertyName="mailServer", propertyValue="imap.gmail.com"),
        @ActivationConfigProperty(propertyName="mailFolder", propertyValue="INBOX"),
        @ActivationConfigProperty(propertyName="storeProtocol", propertyValue="imap"),
        @ActivationConfigProperty(propertyName="userName", propertyValue="me@gmail.com"),
        @ActivationConfigProperty(propertyName="password", propertyValue="xxx")
    })
@ResourceAdapter("mail-ra.rar")
public class EMailReceiver implements MailListener {


    @Override
    public void onMessage(Message msg) {

        System.out.println("MSG: " + msg.toString());
    }

}

I have several questions on how to make this work. Sorry for putting them all into one question, but I believe they belong together.

  1. what is this ResourceAdapter? I can find a ResourceAdapter class, but not an annotation.
  2. where do I get the correct mail-ra.rar and where do I put it ? I asssume this might come in different versions and I can't find one inside wildfly.
  3. is the annotation config sufficient or do I need to configure anything in xml?
Was it helpful?

Solution

I'm no expert in IronJacamar, which is what you'd need to get configured for an inbound adapter. My impression from reading the docs is that there are several layers of abstraction hiding a simple polling of the gmail IMAP service.

Good news is: you can do this easily yourself.

You have to configure a mail Session. I've ripped the following extract for standalone.xml from a Wildfly example:

<subsystem xmlns="urn:jboss:domain:mail:1.0">
<mail-session jndi-name="java:/jboss/mail/MyGmail">
    <imap-server address="example.com" port="432">
        <login name="nobody" password="pass"/>
    </imap-server>
</mail-session>
</subsystem>

If you've set it up like this, you can refer to your session as a Resource:

@Stateless
public class PollingImapAccess {
    @Resource(name = "java:/jboss/mail/MyGmail")
    private Session session;

    @Schedule(hour = "*")
    public void init() {
        try {
            IMAPStore store = (IMAPStore) session.getStore();
            if (!store.isConnected()) store.connect();
            // grab your folders, scan for new messages     
        } catch (MessagingException ex) { /* handle exception */ }
    }
}

This would cause the EJB container to wake up your bean every hour and connect to your gmail account. You can read more about timers in the tutorial.

I don't really appreciate that you have to burden your generic server config with your personal account information, you can fall back to the Session#getInstance() method:

Properties gmail = new Properties();
gmail.setProperty("mail.imaps.host", "imap.gmail.com");
gmail.setProperty("mail.imaps.port", "993");

Session session = Session.getInstance(gmail);
IMAPSSLStore mystore = (IMAPSSLstore) session.connect("me@gmail.com", "xxx");

The full list of settable properties is available in the javadoc.

In this case you don't need the @Resource at all and can just wire up a new connection every time the method wakes up. Note that if plan to have any significant load on there, you probably want to cache that store object and only connect if the connection timed out.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top