Domanda

Sto cercando di utilizzare questo metodo per ricevere posta nel nostro EJB3 app. In breve, ciò significa creare un MDB con le seguenti annotazioni:

@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...
    }
}

Ho funzionato, ma la situazione è tutt'altro che ideale: nome host, nome utente e password sono codificati. A corto di usare ant e build.properties per sostituire quei valori prima della compilazione, non so come esternarli.

Sarebbe l'ideale usare un MBean, ma non ho idea di come ottenere i valori dal MBean alla configurazione MDB.

Come dovrei farlo?

È stato utile?

Soluzione

Puoi esternalizzare le annotazioni in ejb-jar.xml che distribuisci nel META-INF del tuo file jar come segue:

<?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>

Quindi puoi impostare il valore mdb.user.name come proprietà di sistema come parte della riga di comando sul tuo server delle applicazioni usando -Dmdb.user.name = theUserName e verrà magicamente raccolto da mdb.

Spero che sia d'aiuto.

Altri suggerimenti

Almeno a partire da JBoss AS 5.1, è possibile utilizzare AOP per configurare @ActivationConfigProperties. Ho scoperto questo guardando gli esempi che jboss fornisce qui . Ciò è utile se non si desidera che il nome utente e le password siano disponibili per l'intero contenitore in una proprietà di sistema o se si è come me e mai, ripeto MAI, si desidera distribuire un artefatto con un nome utente / password al suo interno. In ogni caso, ecco il jist ...

Annota l'mdb in questo modo ...

...
@MessageDriven
@AspectDomain("TestMDBean")
public class TestMDBean implements MessageListener {
...

Quindi aggiungi un $ {whatever} -aop.xml alla directory deploy con interni come sotto. Ho lasciato lì i commenti originali nel caso in cui Jaikiran apporti le modifiche menzionate ...

  

Nota: l'annotazione deve essere su una   solo linea.

<?xml version="1.0" encoding="UTF-8"?>
<aop xmlns="urn:jboss:aop-beans:1.0">
   <!-- TODO: Jaikiran - These interceptor declarations need not be here since they 
   are already declared through the ejb3-interceptors-aop.xml. Duplicating them leads to
   deployment errors. However, if this custom-ejb3-interceptors-aop.xml needs to be 
   independent, then we must find a better way of declaring these. Right now, commenting these
   out, can be looked at later. -->
   <!--    
   <interceptor class="org.jboss.ejb3.AllowedOperationsInterceptor" scope="PER_VM"/>
   <interceptor class="org.jboss.ejb3.entity.TransactionScopedEntityManagerInterceptor" scope="PER_VM"/>
   <interceptor factory="org.jboss.ejb3.security.RunAsSecurityInterceptorFactory" scope="PER_CLASS"/>
   <interceptor class="org.jboss.ejb3.stateless.StatelessInstanceInterceptor" scope="PER_VM"/>

   <interceptor factory="org.jboss.ejb3.interceptor.EJB3InterceptorsFactory" scope="PER_CLASS_JOINPOINT"/>
   <interceptor factory="org.jboss.aspects.tx.TxInterceptorFactory" scope="PER_CLASS_JOINPOINT"/>
   -->
   <domain name="TestMDBean" extends="Message Driven Bean" inheritBindings="true">
      <annotation expr="!class(@org.jboss.ejb3.annotation.DefaultActivationSpecs)">
         @org.jboss.ejb3.annotation.DefaultActivationSpecs (value={@javax.ejb.ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @javax.ejb.ActivationConfigProperty(propertyName="destination", propertyValue="queue/MyQueue"), @javax.ejb.ActivationConfigProperty(propertyName="user", propertyValue="testusr"), @javax.ejb.ActivationConfigProperty(propertyName="password", propertyValue="testpwd")})
      </annotation>
   </domain>
</aop>
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top