001: /*
002: * Copyright (C) The MX4J Contributors.
003: * All rights reserved.
004: *
005: * This software is distributed under the terms of the MX4J License version 1.0.
006: * See the terms of the MX4J License in the documentation provided with this software.
007: */
008: package mx4j.examples.tools.mail;
009:
010: import javax.management.Attribute;
011: import javax.management.JMException;
012: import javax.management.MBeanServer;
013: import javax.management.MBeanServerFactory;
014: import javax.management.ObjectName;
015:
016: /**
017: * Example as how to use the SMTP MBean. There is a monitor which looks for the Str property
018: * on the TestMBean, when the value changes a notification is sent by the Monitor and a mail
019: * is produced.
020: * <p/>
021: * Modify the values of the SMTP server for your needs
022: *
023: * @version $Revision: 1.1 $
024: */
025: public class MailExample {
026:
027: private static interface TestClassMBean {
028: public String getStr();
029:
030: public void setStr(String str);
031: }
032:
033: public static class TestClass implements TestClassMBean {
034: private String str;
035:
036: public TestClass(String str) {
037: this .str = str;
038: }
039:
040: public String getStr() {
041: return str;
042: }
043:
044: public void setStr(String str) {
045: this .str = str;
046: }
047: }
048:
049: public MailExample() {
050: }
051:
052: /**
053: * Starts the http server
054: */
055: public void start() throws JMException {
056: // creates new server
057: MBeanServer server = MBeanServerFactory
058: .createMBeanServer("Mail");
059: ObjectName beanName = new ObjectName("Test:name=test");
060: server.registerMBean(new TestClass("original"), beanName);
061:
062: ObjectName monitorName = new ObjectName("Test:name=monitor");
063: server.createMBean("javax.management.monitor.StringMonitor",
064: monitorName, null);
065:
066: server.setAttribute(monitorName, new Attribute(
067: "ObservedObject", beanName));
068: server.setAttribute(monitorName, new Attribute(
069: "ObservedAttribute", "Str"));
070: server.setAttribute(monitorName, new Attribute(
071: "StringToCompare", "original"));
072: server.setAttribute(monitorName, new Attribute(
073: "GranularityPeriod", new Integer(100)));
074: server.setAttribute(monitorName, new Attribute("NotifyDiffer",
075: Boolean.TRUE));
076:
077: server.invoke(monitorName, "start", null, null);
078:
079: ObjectName mailerName = new ObjectName("Test:name=mailer");
080: server.createMBean("mx4j.tools.mail.SMTP", mailerName, null);
081:
082: // Sets attributes
083: server.setAttribute(mailerName, new Attribute("ObservedObject",
084: monitorName));
085: server.setAttribute(mailerName, new Attribute(
086: "NotificationName", "jmx.monitor.string.differs"));
087: server.setAttribute(mailerName, new Attribute("FromAddress",
088: "monitor@someserver"));
089: server.setAttribute(mailerName, new Attribute("FromName",
090: "MX4J"));
091: server.setAttribute(mailerName, new Attribute("ServerHost",
092: "smpt-server"));
093: server.setAttribute(mailerName, new Attribute("To",
094: "nobody@nobody"));
095: server.setAttribute(mailerName, new Attribute("Subject",
096: "Notification on $date$ at $time$"));
097: server
098: .setAttribute(
099: mailerName,
100: new Attribute(
101: "Content",
102: "Notification on $datetime$ sent by $objectname$ on $observed$ monitor and a notification $notification$\nNotice how $$$$ gets expanded to $$"));
103:
104: // this will trigger the monitor and the mailer (Wait for 10 secs app)
105: server.setAttribute(beanName, new Attribute("Str",
106: "something-else"));
107:
108: }
109:
110: public static void main(String[] str) throws JMException {
111: MailExample example = new MailExample();
112: example.start();
113: }
114: }
|