1

I have a javasript running on a JAVA FX Web View. I am making an ajax call to an end point.

var formData3 = new FormData();

var content = veryLargeJSONString;

formData3.append("userfile", content);
xhttp3.open("POST", "http://testurl", true);
xhttp3.setRequestHeader("cache-control", "no-cache");
xhttp3.send(formData);

When xhttp3.open("POST", "http://testurl", false); everything works as expected, the response always returns even though it takes a long time due to the large size of the form data.

When the asynch flag is set to true, the response never even gets sent because JFX gives the following exception.

PM com.sun.webkit.network.URLLoader doRun
WARNING: Unexpected error
java.io.IOException: Error writing request body to server
    at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.checkError(HttpURLConnection.java:3479)
    at sun.net.www.protocol.http.HttpURLConnection$StreamingOutputStream.write(HttpURLConnection.java:3462)
    at com.sun.webkit.network.URLLoader.sendRequest(URLLoader.java:373)
    at com.sun.webkit.network.URLLoader.doRun(URLLoader.java:162)
    at com.sun.webkit.network.URLLoader.lambda$run$91(URLLoader.java:128)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.webkit.network.URLLoader.run(URLLoader.java:127)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

This of course only occurs when the formData content is very large, the exact same call with different smaller content has no issue.

I thought maybe this had something to do with the way I was starting the JVM, and thus I tried increasing the starting heap size and also max heap size.

java -jar test.jar -Xms4096m -Xmx8192m

So first question is, what could be causing this java exception and why does it only occur when Asynch is set to true for the ajax call.

Second What happens if in my java -jar start up params I try to allocate more start up heap than my system has available? So if I have 16 gigs of RAM and i'm using 13 gigs and then I start my java app and try to allocate for the starting heap 4 gigs, will it just ignore my start up parameter?

Pasha Skender
  • 397
  • 4
  • 21
  • Apparently between jre-8u111 and some previous version of java 8, this issue was fixed. I will investigate further to see exactly when it was patched and answer once I find out. – Pasha Skender Dec 19 '16 at 16:15

1 Answers1

2

what could be causing this java exception and why does it only occur when Asynch is set to true for the ajax call.

This seems like a WebView bug. You may wish to report it at http://bugreport.java.com.

I do not know what causes it. But, as it occurs for asynchronous processing only, my guess is that it is a concurrency implementation issue in the WebView code.

Proposed Workaround

  1. Use a 3rd party library to make the ajax call using Java concurrency utilities or a third party library:

  2. Ensure you use Platform.runLater as appropriate to process the async response.

  3. Use communication between JavaScript running in WebView and your Java code to transfer any data required between the two:

Answers to additional questions

I was starting the JVM, and thus I tried increasing the starting heap size and also max heap size.

java -jar test.jar -Xms4096m -Xmx8192m

4 gigs of memory is a lotta memory. 8 gigs is even more. I don't think you have a memory issue here and it is highly unlikely you need to allocate so much memory to your application on startup.

So if I have 16 gigs of RAM and i'm using 13 gigs and then I start my java app and try to allocate for the starting heap 4 gigs, will it just ignore my start up parameter?

No, it will respect your starting parameters. The JVM will allocate the minimum you requested (4 gigs). It is unlikely your app is actually using more than four gigs, so it won't probably won't use any more memory than four gigs. If your app does use more than your available physical ram size (16 gigs less space for OS and other programs), then everything will still work, just slowly as the OS pages your app memory.

Community
  • 1
  • 1
jewelsea
  • 150,031
  • 14
  • 366
  • 406
  • Any guess as to why this would only be occurring 50% of the time and only with the large json string however? Smaller strings (I can find you a specific size threshold if you'd like) don't cause this issue at all. – Pasha Skender Dec 14 '16 at 19:39
  • 1
    As I stated in my answer, I don't know what causes your issue. It may be an issue with your network connectivity or server configuration rather than a WebView issue. The underlying networking for WebView uses the standard Java [HttpUrlConnection](https://docs.oracle.com/javase/8/docs/api/java/net/HttpURLConnection.html), which is of course very stable since it has been around since Java 1.1, though it is just part of the overall processing for an ajax call. Tools such as Wireshark can sometimes help debug network connectivity issues, though that takes some skill. – jewelsea Dec 14 '16 at 20:29