Here is a sample code on how to ping the server from a Vaadin 7 app
First we need some javascript to ping the server. The following is the code in ping.js
var host=location.host;
var path=location.pathname.split('/')[1];
var redPic=new Image(100,25);
redPic.src='http://' + host + '/' + path + '/red.jpg';
var greenPic=new Image(100,25);
greenPic.src='http://' + host + '/' + path + '/green.jpg';
var intervalTimer;
function pingHost(){
var http = new XMLHttpRequest();
http.open('GET', 'http://' + host + '/' + path + '/ping', false);
try {
http.send('');
} catch (e) { /*can't send over http....means server down*/
var els = document.getElementsByTagName('img');
for(i=0; i < els.length; i++) {
if(els[i].src.indexOf('green.jpg') > -1) {
els[i].src=redPic.src;
alert('serverDownMessage');
break;
}
}
return;
}
var els = document.getElementsByTagName('img');
for(i=0; i < els.length; i++) {
if(els[i].src.indexOf('red.jpg') > -1) {
els[i].src=greenPic.src;
alert('serverUpMessage');
location.reload();
break;
}
}
var timer=setTimeout(function(){alert('serverDownMessage')}, pingInterval);
if (http.status == 200){ /*the server is up and running*/
clearTimeout(timer); /*no need for a timer*/
}
};
intervalTimer=setInterval(function() {pingHost();}, pingInterval);
Then we need the Vaadin servlet that reads the javascript
@SuppressWarnings("serial")
@Title("My Monitor")
@Push
public class MyMonitorUI extends UI implements ManagerEventListener {
@VaadinServletConfiguration(productionMode = false, ui = MyMonitorUI.class)
public static class Servlet extends VaadinServlet { }
private static Logger logger = Logger.getLogger(MyMonitorUI.class);
private int pingInterval = 10000; // 10 seconds
private String serverDownMessage="Server Down!";
private String serverUpMessage="Server Up!";
@Override
protected void init(VaadinRequest request) {
...
// Read the JS File and Process it
JavaScript.getCurrent().execute(readPingFile());
logger.info("Started UI: "+hashCode());
}
...
private String readPingFile() {
String basepath = VaadinService.getCurrent().getBaseDirectory().getAbsolutePath();
BufferedReader br=null;
StringBuffer pingJavaScript= new StringBuffer();
try {
br= new BufferedReader(new FileReader(new File(basepath + "/ping.js")));
String line="";
while((line=br.readLine()) != null) {
pingJavaScript.append(line);
}
} catch (Exception e) {
logger.error("can't read js file", e);
} finally {
try {
br.close();
} catch (Exception e) {}
}
return pingJavaScript.toString().
replaceAll("pingInterval", String.valueOf(pingInterval)).
replaceAll("serverDownMessage", serverDownMessage).
replaceAll("serverUpMessage", serverUpMessage);
}
then we need to configure the servlet in web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>NtsTpsAsteriskMonitor</display-name>
<servlet>
<servlet-name>Vaadin Application</servlet-name>
<servlet-class>com.vaadin.server.VaadinServlet</servlet-class>
<init-param>
<description>Vaadin application class to start</description>
<param-name>UI</param-name>
<param-value>net.mydomain.MyMonitorUI</param-value>
</init-param>
<init-param>
<param-name>pushmode</param-name>
<param-value>automatic</param-value>
</init-param>
<async-supported>true</async-supported>
</servlet>
<servlet-mapping>
<servlet-name>Vaadin Application</servlet-name>
<url-pattern>/ui/*</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>Vaadin Application</servlet-name>
<url-pattern>/VAADIN/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
No comments:
Post a Comment