fixed monitoring.properties; fixed monitoring bundle to handle wrong bundle names more robustly

This commit is contained in:
Axel Uhl committed 2012-11-30 17:11:37 +01:00
1 parent d964fd8e76
commit c5a0ed6c59
5 files changed
+50 -50

No files matched your search

@@ -90,25 +90,21 @@ public abstract class AbstractPortMonitor extends Thread {
if (currentmillis >= (lastmillis + interval + (100*endpoints.length))) {
for (int i = 0; i < endpoints.length; i++) {
currentendpoint = endpoints[i];
if (!currentendpoint.isURL()) {
Socket sn = new Socket();
sn.connect(currentendpoint.getAddress(), timeout);
sn.close();
} else {
/* despite its name this does NOT open a real TCP connection */
HttpURLConnection conn = (HttpURLConnection)currentendpoint.getURL().openConnection();
conn.setConnectTimeout(timeout);
conn.setRequestMethod("GET");
conn.connect();
int code = conn.getResponseCode();
if (code != 200)
if (code != 200) {
throw new ConnectException("Could not successfully connect to endpoint " + currentendpoint.toString());
}
}
log.info("Connection succeeded to " + currentendpoint.toString());
handleConnection(currentendpoint);
@@ -22,7 +22,7 @@ public interface Endpoint {
* @return the name of this endpoint. This information can be used
* to reference other services.
*/
String getName();
String getBundleName();
/**
* @return true if this endpoint represents an url
@@ -46,7 +46,7 @@ public interface Endpoint {
void setFailure(long millis);
void setName(String name);
void setBundleName(String name);
String toString();
@@ -71,12 +71,12 @@ public class EndpointImpl implements Endpoint {
}
@Override
public String getName() {
public String getBundleName() {
return name;
}
@Override
public void setName(String name) {
public void setBundleName(String name) {
this.name = name;
}
@@ -27,54 +27,58 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
public OSGiRestartingPortMonitor(Properties properties) {
super(properties);
String[] prop_services = properties.getProperty("monitor.bundles").split(",");
String[] props_bundles = properties.getProperty("monitor.bundles").split(",");
for (int i=0; i<endpoints.length;i++) {
endpoints[i].setName(prop_services[i].trim());
endpoints[i].setBundleName(props_bundles[i].trim());
}
}
@Override
public void handleFailure(Endpoint endpoint) {
Bundle bundle = getBundleByName(endpoint.getName());
if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED) {
try {
bundle.stop();
} catch (BundleException e) {
log.severe("Could not stop " + endpoint.getName() + " trying to start anyway.");
Bundle bundle = getBundleByName(endpoint.getBundleName());
if (bundle == null) {
log.severe("Couldn't find bundle "+endpoint.getBundleName()+". Cannot restart. Please check monitoring.properties file");
} else {
if (bundle.getState() == BundleEvent.STARTED || bundle.getState() == BundleEvent.STOPPED) {
try {
bundle.stop();
} catch (BundleException e) {
log.severe("Could not stop " + endpoint.getBundleName() + " trying to start anyway.");
}
try {
bundle.start();
} catch (BundleException e) {
log.severe("Could not start " + endpoint.getBundleName() + "! Handler will try again next time");
}
}
try {
bundle.start();
} catch (BundleException e) {
log.severe("Could not start " + endpoint.getName() + "! Handler will try again next time");
}
}
log.info("Bundle " + endpoint.getName() + " restarted");
/* only send mail if service has not failed before */
if (!endpoint.hasFailed() /*before*/) {
log.info("Sending mail to "+this.properties.getProperty("mail.to")+" saying that bundle " + endpoint.getName() + " was restarted");
try {
Session session = Session.getDefaultInstance(this.properties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("root@sapsailing.com"));
msg.setSubject("Bundle " + endpoint.getName() + " restarted");
msg.setContent("The Bundle " + endpoint.getName() + " has been restarted - check on " + endpoint + " didn't respond!\n" +
"This Mail won't be sent again if service continues to fail.", "text/plain");
for (String mailAddress : this.properties.getProperty("mail.to").split(",")) {
msg.addRecipient(RecipientType.TO, new InternetAddress(mailAddress.trim()));
log.info("Bundle " + endpoint.getBundleName() + " restarted");
/* only send mail if service has not failed before */
if (!endpoint.hasFailed() /* before */) {
log.info("Sending mail to " + this.properties.getProperty("mail.to") + " saying that bundle "
+ endpoint.getBundleName() + " was restarted");
try {
Session session = Session.getDefaultInstance(this.properties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress("root@sapsailing.com"));
msg.setSubject("Bundle " + endpoint.getBundleName() + " restarted");
msg.setContent("The Bundle " + endpoint.getBundleName() + " has been restarted - check on "
+ endpoint + " didn't respond!\n"
+ "This Mail won't be sent again if service continues to fail.", "text/plain");
for (String mailAddress : this.properties.getProperty("mail.to").split(",")) {
msg.addRecipient(RecipientType.TO, new InternetAddress(mailAddress.trim()));
}
Transport ts = session.getTransport();
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
} catch (Throwable ex) {
ex.printStackTrace();
log.throwing(getClass().getName(), "handleFailure", ex);
}
Transport ts = session.getTransport();
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
} catch(Exception ex) {
ex.printStackTrace();
log.throwing(getClass().getName(), "handleFailure", ex);
}
}
}