The following examples show how custom advisors can be implemented.
A sample custom advisor for WebSphere Application Server is included in
the
install_root/servers/samples/CustomAdvisors/
directory. The full code is not duplicated in this document. Ensure that the
following will be implemented:
- ADV_was.java is the advisor source code file that is compiled and run
on the Load Balancer machine.
- LBAdvisor.java.servlet is the servlet source code that must be renamed
to LBAdvisor.java, compiled, and run on the WebSphere Application Server machine.
The complete advisor is only slightly more complex than the sample. It
adds a specialized parsing routine that is more compact than the StringTokenizer
example shown in the topic Example: Using data returned from advisors.
The more complex part of the sample code is in the Java servlet. Among
other methods, the servlet contains two methods required by the servlet specification:
init() and service(), and one method, run(), that is required by the Java.lang.thread
class.
- init() is called once by the servlet engine at initialization time. This
method creates a thread named _checker that runs independently of calls from
the advisor and sleeps for a period of time before resuming its processing
loop.
- service() is called by the servlet engine each time the servlet is invoked.
In this case, the method is called by the advisor. The service() method sends
a stream of ASCII characters to an output stream.
- run() contains the core of the code execution. It is called by the start()
method that is called from within the init() method.
The relevant fragments of the servlet code appear below:
...
public void init(ServletConfig config) throws ServletException {
super.init(config);
...
_checker = new Thread(this);
_checker.start();
}
public void run() {
setStatus(GOOD);
while (true) {
if (!getKeepRunning())
return;
setStatus(figureLoad());
setLastUpdate(new java.util.Date());
try {
_checker.sleep(_interval * 1000);
} catch (Exception ignore) { ; }
}
}
public void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
ServletOutputStream out = null;
try {
out = res.getOutputStream();
} catch (Exception e) { ... }
...
res.setContentType("text/x-application-LBAdvisor");
out.println(getStatusString());
out.println(getLastUpdate().toString());
out.flush(); return;
}
...