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 |
|
|
public MailNotifier(CircuitBreaker cb, String from, String recipient, String mailhost) { |
26 |
|
|
super(cb); |
27 |
|
|
|
28 |
|
|
this.from = from; |
29 |
|
|
this.recipient = recipient; |
30 |
|
|
|
31 |
|
|
props = new Properties(); |
32 |
|
|
props.put("mail.smtp.host", mailhost); |
33 |
|
|
} |
34 |
|
|
|
35 |
torben |
1149 |
//TODO: Only send mail when event == trigger || event == reset |
36 |
torben |
1148 |
@Override |
37 |
|
|
public void sendAsync(String breakerName, Event evnt) { |
38 |
|
|
try { |
39 |
|
|
|
40 |
|
|
// create some properties and get the default Session |
41 |
|
|
Session session = Session.getDefaultInstance(props, null); |
42 |
|
|
session.setDebug(true); |
43 |
|
|
|
44 |
|
|
|
45 |
|
|
// create a message |
46 |
|
|
|
47 |
|
|
MimeMessage msg = new MimeMessage(session); |
48 |
|
|
msg.setFrom( new InternetAddress(from) ); |
49 |
|
|
|
50 |
|
|
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(recipient) ); |
51 |
|
|
msg.setSubject("Circuitbreaker " + breakerName + " : " + evnt.toString()); |
52 |
|
|
msg.setText("--"); |
53 |
|
|
|
54 |
|
|
Transport.send(msg); |
55 |
|
|
} catch (Exception e) { |
56 |
|
|
logger.warning("Unable to send CircuitBreaker notification mail " + e ); |
57 |
|
|
} |
58 |
|
|
|
59 |
|
|
|
60 |
|
|
} |
61 |
|
|
|
62 |
|
|
} |