KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > examples > tools > mail > MailExample


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8 package mx4j.examples.tools.mail;
9
10 import javax.management.Attribute JavaDoc;
11 import javax.management.JMException JavaDoc;
12 import javax.management.MBeanServer JavaDoc;
13 import javax.management.MBeanServerFactory JavaDoc;
14 import javax.management.ObjectName JavaDoc;
15
16 /**
17  * Example as how to use the SMTP MBean. There is a monitor which looks for the Str property
18  * on the TestMBean, when the value changes a notification is sent by the Monitor and a mail
19  * is produced.
20  * <p/>
21  * Modify the values of the SMTP server for your needs
22  *
23  * @version $Revision: 1.3 $
24  */

25 public class MailExample
26 {
27
28    private static interface TestClassMBean
29    {
30       public String JavaDoc getStr();
31
32       public void setStr(String JavaDoc str);
33    }
34
35    public static class TestClass implements TestClassMBean
36    {
37       private String JavaDoc str;
38
39       public TestClass(String JavaDoc str)
40       {
41          this.str = str;
42       }
43
44       public String JavaDoc getStr()
45       {
46          return str;
47       }
48
49       public void setStr(String JavaDoc str)
50       {
51          this.str = str;
52       }
53    }
54
55    public MailExample()
56    {
57    }
58
59    /**
60     * Starts the http server
61     */

62    public void start() throws JMException JavaDoc
63    {
64       // creates new server
65
MBeanServer JavaDoc server = MBeanServerFactory.createMBeanServer("Mail");
66       ObjectName JavaDoc beanName = new ObjectName JavaDoc("Test:name=test");
67       server.registerMBean(new TestClass("original"), beanName);
68
69       ObjectName JavaDoc monitorName = new ObjectName JavaDoc("Test:name=monitor");
70       server.createMBean("javax.management.monitor.StringMonitor", monitorName, null);
71
72       server.setAttribute(monitorName, new Attribute JavaDoc("ObservedObject", beanName));
73       server.setAttribute(monitorName, new Attribute JavaDoc("ObservedAttribute", "Str"));
74       server.setAttribute(monitorName, new Attribute JavaDoc("StringToCompare", "original"));
75       server.setAttribute(monitorName, new Attribute JavaDoc("GranularityPeriod", new Integer JavaDoc(100)));
76       server.setAttribute(monitorName, new Attribute JavaDoc("NotifyDiffer", Boolean.TRUE));
77
78       server.invoke(monitorName, "start", null, null);
79
80       ObjectName JavaDoc mailerName = new ObjectName JavaDoc("Test:name=mailer");
81       server.createMBean("mx4j.tools.mail.SMTP", mailerName, null);
82
83       // Sets attributes
84
server.setAttribute(mailerName, new Attribute JavaDoc("ObservedObject", monitorName));
85       server.setAttribute(mailerName, new Attribute JavaDoc("NotificationName", "jmx.monitor.string.differs"));
86       server.setAttribute(mailerName, new Attribute JavaDoc("FromAddress", "monitor@someserver"));
87       server.setAttribute(mailerName, new Attribute JavaDoc("FromName", "MX4J"));
88       server.setAttribute(mailerName, new Attribute JavaDoc("ServerHost", "smpt-server"));
89       server.setAttribute(mailerName, new Attribute JavaDoc("To", "nobody@nobody"));
90       server.setAttribute(mailerName, new Attribute JavaDoc("Subject", "Notification on $date$ at $time$"));
91       server.setAttribute(mailerName, new Attribute JavaDoc("Content", "Notification on $datetime$ sent by $objectname$ on $observed$ monitor and a notification $notification$\nNotice how $$$$ gets expanded to $$"));
92
93       // this will trigger the monitor and the mailer (Wait for 10 secs app)
94
server.setAttribute(beanName, new Attribute JavaDoc("Str", "something-else"));
95
96    }
97
98    public static void main(String JavaDoc[] str) throws JMException JavaDoc
99    {
100       MailExample example = new MailExample();
101       example.start();
102    }
103 }
104
105
Popular Tags