为Jboss Seam邮件配置故障切换

为Jboss Seam邮件配置故障切换,jboss,jakarta-mail,failover,seam3,Jboss,Jakarta Mail,Failover,Seam3,我需要使用jboss seam实现发送邮件的故障切换功能。我试图在mail-service.xml中配置两个JNDiname。但我不知道如何在代码中设置第二个JNDI名称。 另外,我不明白如何在代码中设置smtp主机。 我的代码: @Name("emailService") @AutoCreate public class EmailService { private static final Log logger = LogFactory.getLog(EmailService.c

我需要使用jboss seam实现发送邮件的故障切换功能。我试图在mail-service.xml中配置两个JNDiname。但我不知道如何在代码中设置第二个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());  
        }  
    }  
}  
}

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

java:/Mail
没有人
密码
jboss:service=Naming
java:/Mail1
没有人
密码
jboss:service=Naming
您可以帮助我如何配置和使用两个JNDI或动态设置SMTP主机服务器来发送邮件吗? 先谢谢你

问候,,
Saurabh

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

非常感谢您的回复。正如我在问题中提到的,我并没有使用Javamail特性,而是使用JBossSeam。因此,对我来说,让交通工具飞起来是行不通的。我需要一个。将不同的JNDIName注入现有上下文2。为现有上下文注入不同的主机。如果可以将Transport对象与seam一起使用,请给出一些示例?谢谢。我对Seam一无所知,所以我不知道它是如何限制您使用JavaMail API的。