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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1161 - (hide 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 torben 1148 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 torben 1161 public MailNotifier(String from, String recipient, String mailhost) {
26 torben 1148
27     this.from = from;
28     this.recipient = recipient;
29    
30     props = new Properties();
31     props.put("mail.smtp.host", mailhost);
32     }
33    
34 torben 1155
35 torben 1148 @Override
36 torben 1161 public void sendAsync(CircuitBreaker breaker, Event evnt) {
37 torben 1148 try {
38 torben 1155 if (evnt == Event.BreakerTripped || evnt == Event.BreakerReset) {
39     // create some properties and get the default Session
40     Session session = Session.getDefaultInstance(props, null);
41 torben 1157 session.setDebug(false);
42 torben 1155
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 torben 1161 msg.setSubject("Circuitbreaker " + breaker.getName() + " : " + evnt.toString());
51 torben 1155 msg.setText("--");
52    
53     Transport.send(msg);
54     }
55 torben 1148 } 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