Search code examples
javaannotationsejb-3.0jboss-mdb

Configurable values to MDB annotations


I'm trying to use this method for receiving mail in our EJB3 app. In short, that means creating an MDB with the following annotations:

@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName = "mailServer", propertyValue = "imap.company.com"),
    @ActivationConfigProperty(propertyName = "mailFolder", propertyValue = "INBOX"),
    @ActivationConfigProperty(propertyName = "storeProtocol", propertyValue = "imap"),
    @ActivationConfigProperty(propertyName = "debug", propertyValue = "false"),
    @ActivationConfigProperty(propertyName = "userName", propertyValue = "username"),
    @ActivationConfigProperty(propertyName = "password", propertyValue = "pass") })
@ResourceAdapter("mail-ra.rar")
@Name("mailMessageBean")
public class MailMessageBean implements MailListener {
    public void onMessage(final Message msg) {
       ...snip...
    }
}

I have this working, but the situation is less than ideal: The hostname, username and password are hardcoded. Short of using ant and build.properties to replace those values before compilation, I don't know how to externalize them.

It would be ideal to use an MBean, but I have no idea how to get the values from the MBean to the MDB configuration.

How should I do this?


Solution

  • You can externalise the annotations into the ejb-jar.xml that you deploy in the META-INF of your jar file as follows:

    <?xml version="1.0" encoding="UTF-8"?>
    
    <ejb-jar version="3.0">
        <enterprise-beans>
            <message-driven>
                <ejb-name>YourMDB</ejb-name>
                <ejb-class>MailMessageBean</ejb-class>        
                <activation-config>
                    <activation-config-property>
                       <activation-config-property-name>username</activation-config-property-name>
                       <activation-config-property-value>${mdb.user.name}</activation-config-property-value>
                    </activation-config-property>
    ...
    ...
                </activation-config>
            </message-driven>
        </enterprise-beans>
    

    Then you can set the mdb.user.name value as a system property as part of the command line to your application server using -Dmdb.user.name=theUserName and it will magically get picked up by the mdb.

    Hope that helps.