/[projects]/miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/notification/MailNotifier.java
ViewVC logotype

Contents of /miscJava/CircuitBreaker/src/main/java/dk/thoerup/circuitbreaker/notification/MailNotifier.java

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1161 - (show annotations) (download)
Mon Oct 4 17:36:43 2010 UTC (13 years, 8 months ago) by torben
Original Path: CircuitBreaker/src/dk/thoerup/circuitbreaker/notification/MailNotifier.java
File size: 1490 byte(s)
If the Notifier gets a reference to the CircuitBreaker itself instead of the circuitbreaker's name then the same notifier instance can easily 
be shared across several circuitbreakers
1 package dk.thoerup.circuitbreaker.notification;
2
3 import java.util.Properties;
4
5 import javax.mail.Message;
6 import javax.mail.Session;
7 import javax.mail.Transport;
8 import javax.mail.internet.InternetAddress;
9 import javax.mail.internet.MimeMessage;
10
11 import java.util.logging.*;
12
13 import dk.thoerup.circuitbreaker.CircuitBreaker;
14
15 public class MailNotifier extends AsyncNotifier {
16
17
18 Logger logger = Logger.getLogger(MailNotifier.class.getName());
19
20 private String recipient;
21 private String from;
22
23 Properties props;
24
25 public MailNotifier(String from, String recipient, String mailhost) {
26
27 this.from = from;
28 this.recipient = recipient;
29
30 props = new Properties();
31 props.put("mail.smtp.host", mailhost);
32 }
33
34
35 @Override
36 public void sendAsync(CircuitBreaker breaker, Event evnt) {
37 try {
38 if (evnt == Event.BreakerTripped || evnt == Event.BreakerReset) {
39 // create some properties and get the default Session
40 Session session = Session.getDefaultInstance(props, null);
41 session.setDebug(false);
42
43
44 // create a message
45
46 MimeMessage msg = new MimeMessage(session);
47 msg.setFrom( new InternetAddress(from) );
48
49 msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient) );
50 msg.setSubject("Circuitbreaker " + breaker.getName() + " : " + evnt.toString());
51 msg.setText("--");
52
53 Transport.send(msg);
54 }
55 } catch (Exception e) {
56 logger.warning("Unable to send CircuitBreaker notification mail " + e );
57 }
58 }
59
60 }

  ViewVC Help
Powered by ViewVC 1.1.20