1 |
package dk.thoerup.circuitbreaker.notification; |
2 |
|
3 |
import java.util.Properties; |
4 |
import java.util.logging.Logger; |
5 |
|
6 |
import javax.mail.Message; |
7 |
import javax.mail.Session; |
8 |
import javax.mail.Transport; |
9 |
import javax.mail.internet.InternetAddress; |
10 |
import javax.mail.internet.MimeMessage; |
11 |
|
12 |
import dk.thoerup.circuitbreaker.CircuitBreaker; |
13 |
import dk.thoerup.circuitbreaker.Event; |
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 |
} |