Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to continuously stream text data to a servlet?
    primarykey
    data
    text
    <p>I have an Android app which connects to a server and continuously sends some signal data (using HttpURLConnection). On the server a script (shell or python, don't exactly know, it's not my part) currently processes this stream and for every incomming data package sets up a curl request to post this package to a servlet (running in a Jetty 8 web container). This is only a provisionary solution, since I don't know how to directly connect and stream the data to the servlet. As a requirement, the data transfer should use the HTTP protocol in order to be not blocked by any firewall.</p> <p>So, is it possible to initially connect to a servlet and then stream data over a long time, say minutes or hours? I was wondering that I did not find similar questions/solutions. I mean this is not a special scenario. And is this the same operation as uploading a large file to a servlet?</p> <p>The servlet actually looks like this:</p> <pre><code>protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { System.out.println("StreamingServlet#doPost(): IN"); BufferedReader reader = request.getReader(); String data = ""; while ((data = reader.readLine()) != null) { System.out.println("Data: " + data); } reader.close(); System.out.println("StreamingServlet#doPost(): OUT"); } </code></pre> <p>The android app has 3 methods using the HttpURLConnection:</p> <pre><code>private Boolean connect(URL URLdest) { try { connection = (HttpURLConnection) URLdest.openConnection(); connection.setDoOutput(true); connection.setChunkedStreamingMode(0); oSWout = new OutputStreamWriter(connection.getOutputStream()); } catch (IOException e) { return false; } return true; } public void send(String packet) { try { // TODO clOSWout.write and clOSWout.flush block may freeze if connection is lost. Somehow no exception is thrown and thread hangs instead. // It only occurs if the Server breaks the connection oSWout.write(packet); oSWout.flush(); } catch (IOException e) { ... } } public void stopConnection() { try { oSWout.close(); connection.disconnect(); } catch (IOException e) { ... } } </code></pre> <p>What I expect in the end is that when connecting to the servlet, it prints "StreamingServlet#doPost(): IN", then, for each incomming data package it should print the data string and when closing the connection it should print "StreamingServlet#doPost(): OUT" and finally return from the method.</p> <p>But I guess I miss something and this is done in another way in Java EE. I don't know.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload