tomcat-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Mark Pease" <>
Subject Re: Servlet Design Question
Date Fri, 11 Apr 2003 12:26:04 GMT
Hi Santos,

You are probably going to want to make a seperate class that will handle
your socket communications on a seperate thread for each connection.  I have
a similar application, which also listens on a socket port for XML data to
be sent sporadically.  I have a class defined to be run as a seperate
thread, defined by:

class SocketClient extends Thread implements Observer{

The Observer isn't necessary, but I use it to notify various Java Beans when
new data has come in.  I establish the socket in a seperate method, then, in
the run() method, I have this:

    public void run(){
        // Get XML data, send to ClientBean for parsing...
        // Slight problem with reading XML from sockets: the XML parser
        // does not know where the end of file is, so it hangs.
        // Workaround is to convert the XML data, which must have only one
        // CR LF at the end, into a string and send it.  See
        // community discussion, search by "XML socket" to find.
        String inData;
        BufferedReader br;
        StringReader sr;
        InputSource in;


            while ( keepRunning ) {

                    br = new BufferedReader(new
                    inData = br.readLine();

                    sr = new StringReader(inData);

                    in = new InputSource(sr);

                    // Do what you want with the string (in)

                // This will be thrown when the socket connection is lost.
                // Will stop excecution even though the .readLine() method
                // a blocking call.
                catch(NullPointerException npe){
                    keepRunning = false;

                    client = null;

                        socket = null;
                    catch(IOException ioe){
                        socket = null;

        catch(IOException ioe){
            Output.systemOut(99,"IOException:" + ioe.getMessage());

Essentially, it keeps running the while loop until the socket connection is
lost, which will raise an exception.  You may want to start checking the
Java forums if you need further information.

Good luck!

----- Original Message -----
From: "Santos Jha" <>
To: "Tomcat Users List" <>
Sent: Thursday, April 10, 2003 9:36 PM
Subject: Servlet Design Question

> Hello Everybody,
> I am developing a system which finds out the application specific status
> of different servers( written in C++)  and and this data is transmitted
> to users for display.System should continuously
> ( every 15 seconds) poll these servers to get the data. Once user
> requests it fetches the most recent data.
> In order to get the data from these servers I am planning to use socket.
> However I have few questions regarding socket and servlet.
> 1. Should I write a servlet with a init method  which will have
>           a. for ever loop and inside the for ever loop I will be
> continuously getting the data using socket.
> 2. Should I write a client outside the tomcat servlet container which
> will get all the data and will be sent them to the container.
> What will be the better approach? or there is something better than
> Santos
> >
> >
> >
> ---------------------------------------------------------------------
> To unsubscribe, e-mail:
> For additional commands, e-mail:

To unsubscribe, e-mail:
For additional commands, e-mail:

View raw message