Creating an Email Alert


When you created the monitor, you chose to send the alert to the console alert listener, and JBoss logged the alert as an INFO message waiting to be read. Alert listeners don't need to be so passive. In this lab, you'll configure alerts to be sent by email.


Note: If you need an email server for testing, you can try the JBoss Mail Server.

How do I do that?

You send email alerts using the email service, so first you'll need to make sure the email service is properly configured. Look in the mail-service.xml file. If you have a local mail server that relays without authentication, you can put the hostname of the server in the mail.smtp.host property and be done with the mail service.

If you do need to authenticate to the mail server, you will need to set the User and Password attributes with the required login, and set mail.smtp.auth to true:

     <mbean code="org.jboss.mail.MailService"            name="jboss:service=Mail">         <attribute name="JNDIName">java:/Mail</attribute>         <attribute name="User">username</attribute>         <attribute name="Password">my password</attribute>         <attribute name="Configuration">             <configuration>                 <!-- ... -->                 <property name="mail.smtp.host"                            value="my.mail.server"/>                 <property name="mail.smtp.auth"                           value="true" />             </configuration>         </attribute>     </mbean> 

Any standard JavaMail configuration options can be placed here. If your mail server is particularly demanding, you might need to consult the JavaMail documentation at http://java.sun.com/products/javamail/ to find the correct properties. Here's an example of a JavaMail configuration to connect to GMail:

     <property name="mail.smtp.host"                   value="smtp.gmail.com"/>     <property name="mail.smtp.port"                   value="465"/>     <property name="mail.smtp.auth"                   value="true" />     <property name="mail.smtp.starttls.enable"        value="true" />     <property name="mail.smtp.socketFactory.port"     value="465" />     <property name="mail.smtp.socketFactory.fallback" value="false" />     <property name="mail.smtp.socketFactory.class"               value="javax.net.ssl.SSLSocketFactory" /> 


Note: GMail is unusual in that it requires you to connect securely using SSL.

That gets the mail service running. You can configure the email alert listener by uncommenting the EmailAlertListener MBean in monitoring-service.xml. You'll need to set the To and From addresses for the email message as shown here:

     <mbean code="org.jboss.monitor.alerts.EmailAlertListener"            name="jboss.alerts:service=EmailAlertListener">         <depends>jboss:service=Mail</depends>         <attribute name="MessageTemplate"><![CDATA[             %(MONITOR_NAME) was triggered for attribute %(ATTRIBUTE).         ]]></attribute>         <attribute name="AlertName">Email Alert</attribute>         <attribute name="To">jbossnotebook@gmail.com</attribute>         <attribute name="From">jbossnotebook@gmail.com</attribute>         <attribute name="ReplyTo">jbossnotebook@gmail.com</attribute>         <attribute name="SubjectTemplate">             <![CDATA[[jboss-alert] %(MONITOR_NAME)]]>         </attribute>     </mbean> 


Note: If you have problems sending mail, set mail. smtp.debug to true to see the connection attempts.

Once you have made these changes, the free memory monitor will show an Email Alert option in the Alerts box. After you add the email listener to the monitor, you should access memory.jsp again to trigger the alert. If your email service is configured properly, you should soon have an email alert in your inbox. Figure 8-11 shows an email alert viewed from within GMail.

It's not the most informative of messages, but it works. You can change the message title and body by changing the MessageTemplate and SubjectTemplate attributes on the EmailAlertListener MBean.

Developer's Notebook 8-11. An email alert viewed in GMail


What just happened?

You configured a monitor to send an alert message by email. To do that you had to first enable the mail service to know what mail server to use to send mail. Then you activated the email alert listener service. The Web Console immediately saw the email alert listener and let you connect your monitor to it.


Note: If email isn't good enough, you can write your own listener to process alerts.


JBoss. A Developer's Notebook
JBoss: A Developers Notebook
ISBN: 0596100078
EAN: 2147483647
Year: 2003
Pages: 106

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net