Creating an XML-RPC Server

Now that you have a Java class that will handle XML-RPC requests, you're ready to develop a server that waits for these requests to arrive, dispatching them to the handler. The LottoServer class drives a simple web server that looks for these requests. Apache XML-RPC has two core packages: org.apache.xmlrpc for apps and org.apache.xmlrpc for apps. The WebServer class in org.apache.xmlrpc functions as a bare-bones XML-RPC server that listens on a specified TCP/IP Internet port:

int port = 4404;
server = new WebServer(port);

The choice of port number 4404 is arbitrary; any port from 1,024 through 49,151 could be chosen.

By the way

The Internet Assigned Numbers Authority, a group that decrees how ports below 1,024 are used, offers a directory of port numbers and the software using them on the Web:

The server's addHandler() method designates a class that will handle XML-RPC requests:

LottoCounter counter = new LottoCounter();
server.addHandler("lotto", counter);

The method's arguments are an identifier that gives the handler a name and an object. The LottoCounter object counter has been assigned the name "lotto", so the public methods of that object may be called over XML-RPC. There's one such method, sendRequest(), which will be called over XML-RPC as lotto.sendRequest. No parentheses should follow the method name—they are omitted in XML-RPC. An XML-RPC server can add as many handlers as desired, which don't need to belong to the same class. The server can be launched by calling its start() method:


Enter the text of Listing 25.3 and save the file under the name

Listing 25.3. The Full Text of
 1: import*;
 2: import nu.xom.*;
 3: import org.apache.xmlrpc.*;
 5: public class LottoServer {
 6: int port = 4404;
 7: WebServer server;
 9: public LottoServer() throws IOException, ParsingException {
10: // create the server
11: System.out.println("Creating server on port " + port);
12: server = new WebServer(port);
14: // register the request handler
15: LottoCounter counter = new LottoCounter();
16: server.addHandler("lotto", counter);
17: }
19: public void start() {
20: // start the server
21: System.out.println("Now accepting requests");
22: server.start();
23: }
25: public static void main(String[] arguments) {
26: try {
27: LottoServer lotto = new LottoServer();
28: lotto.start();
29: } catch (Exception exception) {
30: System.out.println("Error: " + exception.getMessage());
31: }
32: }
33: }

Compile and run the app, which requires no command-line arguments:

java LottoServer

The following output indicates the server's running and ready to receive requests:

Creating server on port 4404
Now accepting requests

On a Windows system, you must leave this running and open a second command-line window to create the XML-RPC client. On Linux, you can start the server in the background by putting an "&" character after the command:

java LottoServer &

When running the app in the background, to stop it later on, you must use the ps command to find its process ID, then the kill command to bring it to a halt. Here's an example for the Bash shell on Linux:

ps auxw | grep LottoServer rcade 10424 0.0 1.3 269764 14320 pts/0 S Jul13 0:00 /usr/java/jdk1.5.0/bin/java
Java ScreenShot LottoServer kill -9 10424