nekop's blog

OpenShift / JBoss / WildFly / Infinispanの中の人 http://twitter.com/nekop

JBoss ASで受信メール処理

MDBでゴー。

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.mail.Message;
import javax.mail.MessagingException;

import org.jboss.annotation.ejb.ResourceAdapter;
import org.jboss.resource.adapter.mail.inflow.MailListener;

@MessageDriven(activationConfig={
   @ActivationConfigProperty(propertyName="mailServer", propertyValue="server"),
   @ActivationConfigProperty(propertyName="mailFolder", propertyValue="INBOX"),
   @ActivationConfigProperty(propertyName="storeProtocol", propertyValue="imap"),
   @ActivationConfigProperty(propertyName="userName", propertyValue="foo"),
   @ActivationConfigProperty(propertyName="password", propertyValue="foo")
})
@ResourceAdapter("mail-ra.rar")
public class MailMDB implements MailListener {
    public void onMessage(Message mail) {
        try {
            System.out.println("メール来たよー!: " + mail.getSubject());
        } catch (MessagingException ex) {
            // どうにかする
        }
    }
}

より詳しい説明はJBoss Wikiにある。

http://community.jboss.org/wiki/InboundJavaMail

MDBというのはWeb系、Servlet中心のJava EEを使っている人たちにはあまり馴染みがないかもしれない。この前書いたJBoss ASで定期処理というのも同じくMDBを利用したもの。簡単でいいよね、MDB