minor adjustments regarding defaults of properties for port monitor

This commit is contained in:
Axel Uhl committed 2012-11-27 11:19:28 +01:00
1 parent 82ed5425f2
commit bcec5b4a81
3 files changed
+40 -63

No files matched your search

@@ -20,9 +20,9 @@ public abstract class AbstractPortMonitor extends Thread {
Logger log = Logger.getLogger(AbstractPortMonitor.class.getName());
private int TIMEOUT = 1000;
private int GRACEFUL = 5000;
private int PAUSE = 1000;
private int timeout = 1000;
private int graceful = 5000;
private int pause = 1000;
protected InetSocketAddress[] endpoints = null;
protected int interval;
@@ -37,21 +37,17 @@ public abstract class AbstractPortMonitor extends Thread {
public AbstractPortMonitor(InetSocketAddress[] endpoints, int interval) {
this.endpoints = endpoints;
if (interval < 10000) {
throw new RuntimeException("Interval can not be lower than 10 seconds!");
}
this.interval = interval;
this.started = true;
log.info("Initialized monitoring!");
}
public AbstractPortMonitor(Properties properties) {
String[] prop_endpoints = properties.getProperty("monitor.endpoints").split(",");
this.endpoints = new InetSocketAddress[prop_endpoints.length];
try {
for (int i=0;i<prop_endpoints.length;i++) {
String[] data = prop_endpoints[i].split(":");
@@ -60,16 +56,12 @@ public abstract class AbstractPortMonitor extends Thread {
} catch (Exception ex) {
ex.printStackTrace();
}
this.interval = Integer.parseInt(properties.getProperty("monitor.interval").trim());
this.TIMEOUT = Integer.parseInt(properties.getProperty("monitor.timeout").trim());
this.GRACEFUL = Integer.parseInt(properties.getProperty("monitor.gracetime").trim());
this.PAUSE = Integer.parseInt(properties.getProperty("monitor.pause").trim());
this.interval = Integer.parseInt(properties.getProperty("monitor.interval", ""+interval).trim());
this.timeout = Integer.parseInt(properties.getProperty("monitor.timeout", ""+timeout).trim());
this.graceful = Integer.parseInt(properties.getProperty("monitor.gracetime", ""+graceful).trim());
this.pause = Integer.parseInt(properties.getProperty("monitor.pause", ""+pause).trim());
this.started = true;
this.properties = properties;
log.info("Initialized monitoring!");
}
@@ -79,11 +71,9 @@ public abstract class AbstractPortMonitor extends Thread {
@Override
public void run() {
try {
/* graceful start - give services some time */
Thread.sleep(GRACEFUL);
Thread.sleep(graceful);
while (started) {
if (!paused) {
long currentmillis = System.currentTimeMillis();
@@ -93,33 +83,28 @@ public abstract class AbstractPortMonitor extends Thread {
currentendpoint = endpoints[i];
Socket sn = new Socket();
sn.connect(currentendpoint, TIMEOUT);
sn.connect(currentendpoint, timeout);
log.info("Connection succeeded to " + currentendpoint.toString());
handleConnection(currentendpoint);
lastmillis = System.currentTimeMillis();
}
}
} catch (SocketTimeoutException|ConnectException ex) {
log.info("Connection FAILED to " + currentendpoint.toString());
handleFailure(currentendpoint);
lastmillis = System.currentTimeMillis();
} catch (IOException ex) {
ex.printStackTrace();
}
} else {
log.info("Pausing...");
}
Thread.sleep(PAUSE);
Thread.sleep(pause);
}
} catch (InterruptedException ex) {
System.err.println("Interrupted");
}
}
public void stopMonitoring() {
@@ -128,12 +113,10 @@ public abstract class AbstractPortMonitor extends Thread {
protected void removeEndpoint(InetSocketAddress endpoint) {
InetSocketAddress[] new_endpoints = new InetSocketAddress[endpoints.length-1];
for (int i=0;i<endpoints.length;i++) {
if (endpoints[i] != endpoint)
new_endpoints[i] = endpoint;
}
this.endpoints = new_endpoints;
}
@@ -14,38 +14,40 @@ import org.osgi.framework.BundleContext;
* @since Nov 25, 2012
*/
public class Activator implements BundleActivator {
private static BundleContext context;
private AbstractPortMonitor monitor;
private static BundleContext context;
private AbstractPortMonitor monitor;
static BundleContext getContext() {
return context;
}
static BundleContext getContext() {
return context;
}
/*
* (non-Javadoc)
*
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#start(org.osgi.framework.BundleContext)
*/
public void start(BundleContext bundleContext) throws Exception {
Activator.context = bundleContext;
/* Load properties */
File propertiesfile = new File(new File(System.getProperty("jetty.home")).getParent()
+ "/monitoring.properties");
Properties props = new Properties();
props.load(new FileReader(propertiesfile));
/* Load properties */
File propertiesfile = new File(new File(System.getProperty("jetty.home")).getParent() + "/monitoring.properties");
Properties props = new Properties();
props.load(new FileReader(propertiesfile));
/* Starts a new port monitoring app */
monitor = new OSGiRestartingPortMonitor(props);
monitor.startMonitoring();
}
/* Starts a new port monitoring app */
monitor = new OSGiRestartingPortMonitor(props);
monitor.startMonitoring();
}
/*
* (non-Javadoc)
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
monitor.stopMonitoring();
Activator.context = null;
}
/*
* (non-Javadoc)
*
* @see org.osgi.framework.BundleActivator#stop(org.osgi.framework.BundleContext)
*/
public void stop(BundleContext bundleContext) throws Exception {
monitor.stopMonitoring();
Activator.context = null;
}
}
@@ -24,7 +24,6 @@ import org.osgi.framework.BundleException;
* @since Nov 26, 2012
*/
public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
Logger log = Logger.getLogger(OSGiRestartingPortMonitor.class.getName());
HashMap<InetSocketAddress, String> endpointservices = new HashMap<InetSocketAddress, String>();
@@ -32,12 +31,10 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
public OSGiRestartingPortMonitor(InetSocketAddress[] endpoints, String[] services, int interval) {
super(endpoints, interval);
/* store bundles so that we can reference them later */
for (Bundle bundle : Activator.getContext().getBundles()) {
bundles.put(bundle.getSymbolicName(), bundle);
}
for (int i=0; i<endpoints.length;i++) {
endpointservices.put(endpoints[i], services[i]);
}
@@ -45,12 +42,10 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
public OSGiRestartingPortMonitor(Properties properties) {
super(properties);
/* store bundles so that we can reference them later */
for (Bundle bundle : Activator.getContext().getBundles()) {
bundles.put(bundle.getSymbolicName(), bundle);
}
String[] prop_services = properties.getProperty("monitor.services").split(",");
for (int i=0; i<endpoints.length;i++) {
endpointservices.put(endpoints[i], prop_services[i].trim());
@@ -75,9 +70,7 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
log.severe("Could not start " + servicename + "! Handler will try again next time");
}
}
log.info("Bundle " + servicename + " restarted");
try {
Session session = Session.getDefaultInstance(this.properties, new SMTPAuthenticator());
MimeMessage msg = new MimeMessage(session);
@@ -90,7 +83,6 @@ public class OSGiRestartingPortMonitor extends AbstractPortMonitor {
ts.connect();
ts.sendMessage(msg, msg.getRecipients(RecipientType.TO));
ts.close();
} catch(Exception ex) {
ex.printStackTrace();
}