开发者

Can't get InputStream read to block

I would like the input stream read to block instead of reading end of stream (-1). Is there a way to configure the stream to do this? Here's my Servlet code:

    PrintWriter out = response.getWriter();
    BufferedReader in = request.getReader();
    try {
        String line;
        int loop = 0;
        while (loop < 20) {
            line = in.readLine();
            lgr.log(Level.INFO, line);
            out.println("<" + loop + "html>");

            Thread.sleep(1000);
            loop++;
            //
        }
    } catch (InterruptedException ex) {
        lgr.log(Level.SEVERE, null, ex);
    } finally {
        out.close();
    }

Here's my Midlet code:

    private HttpConnection conn;
    InputStream is;
    OutputStream os;
    private boolean exit = false;

    public void run() {

        String url = "http://localhost:8080/WebApp开发者_如何学Pythonlication2/NewServlet";
        try {
            conn =
                    (HttpConnection) Connector.open(url);
            is = conn.openInputStream();
            os = conn.openOutputStream();
            StringBuffer sb = new StringBuffer();
            int c;

            while (!exit) {
                os.write("<html>\n".getBytes());
                while ((c = is.read()) != -1) {
                    sb.append((char) c);
                }
                System.out.println(sb.toString());
                sb.delete(0, sb.length() - 1);


                try {
                    Thread.sleep(1000);
                } catch (InterruptedException ex) {
                    ex.printStackTrace();
                }
            }

            os.close();
            is.close();
            conn.close();

        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }

I've tried InputStream.read, but it doesn't block either, it returns -1 as well. I'm trying to keep the I/O streams on either side alive. I want the servlet to wait for input, process the input, then send back a response.

In the code above it should do this 20 times.

thanks for any help


According to the JavaDoc:

http://java.sun.com/j2se/1.4.2/docs/api/java/io/InputStream.html#read()

This method blocks until input data is available, end of file is detected, or an exception is thrown.

So as long as you feed the buffer on one end, it will consume it on the other.


The way a Servlet works is a simple request reply mechanism. In the Servlet you read the data from the Reader and then send a reply back in the Writer.

In plain old Servlets prior to 3.0 or Comet or Continuations you do not have the option of doing anything else. The only thing you can do is create a Session and have the client send back another request to get more info (You could use something in the Request to do the same thing as a Session).

What it seems you are looking for is for a way to keep your Connection open. For that you have several solutions that may or may not work depending on your firewall configuration.

1) Upgrade to a Servlet engine that supports the Servlet 3.x specification
2) Use Tomcat Comet.
3) Use Jetty Continuations.
4) Use Web Sockets.
5) Use a KEEP_ALIVE and have the client poll the open connection

As a note: You should never put a Sleep in your server code, that is an immediate flag that you are doing something wrong.


You're looking for asynchronous IO. This is builtin in Servlet 3.0 (sample app included). If you're still on Servlet 2.5 or older yet, then have a look at Comet technique, here's a Tomcat-targeted document.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜