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.
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?
The servlet actually looks like this:
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");
}
The android app has 3 methods using the HttpURLConnection:
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)
{
...
}
}
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.
But I guess I miss something and this is done in another way in Java EE. I don't know.
Finally, I solved the problem by myself:
Using the BufferedReader, all the data packages are collected (buffered) and are read and printed as one packege when the connection is closed. So I had to use the ServletInputStream [obtained by HttpServletRequest#getInputStream()] and read the data packages into a byte array using the read() method. Now everything works as I expected.
But I still wonder, if it is a more or less typical scenario to use a blocking operation and thus remain a long time (say minutes or hours) in the doPost() method or if the server is somehow expected to respond in much shorter time.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With