为Jboss Seam邮件配置故障转移

时间:2013-03-12 06:08:48

标签: jboss javamail failover seam3

我需要实现故障转移功能,以便使用jboss seam发送邮件。我试图在mail-service.xml中配置两个JNDINames。但我没有得到如何在代码中设置第二个JNDI名称。 另外,我没有得到如何在代码中设置smtp主机。 我的代码:

@Name("emailService")  
@AutoCreate  
public class EmailService {  
private static final Log logger = LogFactory.getLog(EmailService.class);  

@In(create = true)  
private Renderer renderer;  

@Asynchronous  
public void sendMessage(@Duration long delay, String template,  
        Object infoNeededForTemplate) {  
    MailSession mailSession = new MailSession();  
    try {  
        Contexts.getEventContext().set("currentMail", infoNeededForTemplate);  
        renderer.render(template);  
        logger.info("Email send to " + ((Mail) infoNeededForTemplate).getToEmail());  
    } catch (Exception e) {  
        logger.error("Error while sending mail: Message = " + e.getMessage());  
        try {  
            renderer.render(template);  
            logger.info("Email send to " + ((Mail) infoNeededForTemplate).getToEmail());  
        } catch (Exception e1) {  
            logger.error("Error while sending mail: Message = " + e1.getMessage());  
        }  
    }  
}  

}

我的mail-service.xml:

<?xml version="1.0" encoding="UTF-8"?>  
<server>  
<mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">  
    <attribute name="JNDIName">java:/Mail</attribute>  
    <attribute name="User">nobody</attribute>  
    <attribute name="Password">password</attribute>  
    <attribute name="Configuration">  
    <!-- A test configuration -->  
    <configuration>  
    <!-- Change to your mail server prototocol -->  
    <property name="mail.store.protocol" value="pop3"/>  
    <property name="mail.transport.protocol" value="smtp"/>  

    <!-- Change to the user who will receive mail  -->  
    <property name="mail.user" value="nobody"/>  

    <!-- Change to the mail server  -->  
    <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>  

    <!-- Change to the SMTP gateway server -->  
    <property name="mail.smtp.host" value="HOST_1"/>  

    <!-- The mail server port -->  
    <property name="mail.smtp.port" value="25"/>  

    <!-- Change to the address mail will be from  -->  
    <property name="mail.from" value="nobody@abc.com"/>  

    <!-- Enable debugging output from the javamail classes -->  
    <property name="mail.debug" value="true"/>  
    <property name="mail.smtp.auth" value="false"/>  
    <property name="mail.smtp.starttls.enable" value="false"/>  
  </configuration>  
</attribute>  
<depends>jboss:service=Naming</depends>  
</mbean>  

<mbean code="org.jboss.mail.MailService" name="jboss:service=Mail">  
<attribute name="JNDIName">java:/Mail1</attribute>  
<attribute name="User">nobody</attribute>  
<attribute name="Password">password</attribute>  
<attribute name="Configuration">  
  <!-- A test configuration -->  
  <configuration>  
    <!-- Change to your mail server prototocol -->  
    <property name="mail.store.protocol" value="pop3"/>  
    <property name="mail.transport.protocol" value="smtp"/>  

    <!-- Change to the user who will receive mail  -->  
    <property name="mail.user" value="nobody"/>  

    <!-- Change to the mail server  -->  
    <property name="mail.pop3.host" value="pop3.nosuchhost.nosuchdomain.com"/>  

    <!-- Change to the SMTP gateway server -->  
    <property name="mail.smtp.host" value="HOST_2"/>  

    <!-- The mail server port -->  
    <property name="mail.smtp.port" value="25"/>  

    <!-- Change to the address mail will be from  -->  
    <property name="mail.from" value="nobody@abc.com"/>  

    <!-- Enable debugging output from the javamail classes -->  
    <property name="mail.debug" value="true"/>  
    <property name="mail.smtp.auth" value="false"/>  
    <property name="mail.smtp.starttls.enable" value="false"/>  


  </configuration>  
</attribute>  
<depends>jboss:service=Naming</depends>  
</mbean>  
</server>

请问我如何配置&amp;使用两个JNDI或动态设置SMTP主机服务器发送邮件? 非常感谢提前。

此致 SAURABH

1 个答案:

答案 0 :(得分:0)

您可以在Transport.connect方法中明确指定主机名;有关详细信息,请参阅javadoc。