Increase connection timeout on Spring MVC server for HTML5 audio - java

I have a Spring MVC Java web app which plays audio files using the HTML5 audio tag. My MVC controller streams the audio file by writing to response.getOutputStream().
When the audio file is small (< 6mb), the browser will download the entire audio file immediately and play it without issue. When the audio file is larger, the browser will download about 6mb and then wait until playback catches up before requesting more data. It won't close the connection, but just wait before requesting more data from the stream.
The server will then wait for however long connectionTimeout is set on the HTTP connector in Tomcat's server.xml (default 20 seconds). Tomcat then throws a SocketTimeoutException, ending the connection.
The browser will then play until it reaches the end of what it has downloaded and print the error ERR_CONTENT_LENGTH_MISMATCH to the console. This is because the browser requested the entire file but the server terminated the connection before all the data was sent. It makes an HTTP range request for the rest of the data, opening a new connection. This happens seamlessly and the song continues to play. However, it shouldn't have to do this. It should be able to keep the original connection open for the entire duration of the song.
One fix is to increase Tomcat's connectionTimeout in server.xml. However, I would prefer to do this just for the application rather than globally for the Tomcat server. Even better would be just for that one MVC controller method that streams the audio. Is this possible?

Related

How does seek work in peerflix?

i was checking peerflix and it was really awesome that it support seek feature in videos.
i tried to understand how it works , i think it create a stream pipe on HTTP file server and write video data on it when a required piece is downloaded and the video player reads data from other end of pipe.
i tried to do this in java using Pipedinputstream and pipedoutputstream using nanohttpd server but cannot make it work.
is there something i am missing?
peerflix: it is a lib that stream torrent videos , you can watch videos with seek feature. you do not have to wait for the download to finish.
Peerflix selects the biggest file in the torrent or you can select one on your own. Then it begins downloading the file sequentially from the first piece. This file is served on a standard HTTP static file server.
When you seek in a video player, it checks the video container to find which bytes in the video file that corresponds to the given time stamp. The video player then sends a HTTP range request for the bytes corresponding to the video time plus some buffer. Then, Peerflix's HTTP server reads this range request and checks the .torrent file to find which piece corresponds to the requested bytes. It starts downloading sequentially from the requested piece onwards, and responds to the HTTP request when the requested bytes are available.

GlassFish web application suddenly dies without an explicable reason

I'm developing a web application via Netbeans 8.0,Glassfish server 4 and JavaEE 7.
Everything was going great but now I'm facing a strange issue.
If the user interrupts uploads or downloads before their completion (by closing browser's windows or simply by pressing esc or X to interrupt), I get an "IOException: Connection closed" into the server console, but the application still works. We are talking about files and photos whose size can vary from 1 MB to 30 MBs, so it's possible that the user simply gets bored and decides to stop the download before its completion.
If those interruptions (and related exceptions) happen for 3-4 times, the whole application simply stops and can't be used, until I decide to reset the Glassfish server. This is really a crap.
I can't figure what's the problem. Into jvm.log or server.log, there is nothing significative.
And yes, I flush and close the streams that I used for uploads/downloads.
I'll post exception stacks and source code, if needed.
I'm using servlets for downloads (the OutputStream from response)

How can Tomcat and Media Servers work together?

I am planning on building a Java web app that will have lots of integrated video (original content) being streamed from our backend. I'm having a difficult time seeing the forest through the trees: I want all non-media, client-side content (HTML, CSS, JS, etc.) to be served from my Tomcat server, but I want the media content (videos) to be served from an actual media server like Red5 or Wowza.
So my problem is: on the backend, how do you make 1 request for http://myapp.example.org/videos/vid2449 return content from 2 servers (Tomcat and the media server)?
You can also serve the two things on two different ports
Web page generated from http://yourserver.com/videos returns
<html>
Link to stream
<!-- or you can embed a player and feed it
</html>
Req #1 returns stuff for views etc from TC. This response includes ffollowing
Link on where to get media meta data bound to that session/request. A collection of playables
Link on where to get a session Mgr player
JS on client does follow with response
Bootstrap player
Marshall media info is links to streams
Bootstrap wrapper for player state ( Idle Paused PLaying Buffering )
Start player on a stream from list of links which will require a second network connection ( http stream )
Manage player state
Manage socket stream
Manage player ui controls events
Player callbacks
--EDIT--
if your media is self hosted as opposed to coming from the cloud, you can use apache 'ajp' connectors or whatever and have single server running Apache in front of the connectors and infront of TC. Apache will stream the static media files while TC handles other stuff and it all runs from the some domain host.
You didn't specify the type of media being served except in one comment you mention m3u8 which indicates HLS. Your tags indicate you're going to stream Flash media. Red5 includes an embedded tomcat server, so everything can be served from one server.
Your given url could be handled with JavaScript and a swf player; simply read the video id and pass it to the player.

Slow file upload: Can I remove file attachment from multipart POST data on the fly and just submit form text fields?

I have a regular JSP/Servlet/Java web application that is used for uploading pictures from a mobile device. I am using Apache Commons library for the upload. Application is hosted on WebSphere Application Server 7.0.
Everything is working fine and the user can upload several images totaling 8MB or more if he has a really good/strong signal/connection or on a good WiFi.
The problem arises when the user is at a location with poor 3G/4G signal/connection. He gets errors like "Illegal state exception" or some time-out error, and in some cases the mobile browser just stays on the submit page with the progress bar no longer moving.
Any suggestions on how to "gracefully" handle this? Like is there a way to intervene after a set amount of time and give the the user an option to submit the form without the file attachment (i.e. just submit the form text fields)? Any other suggestions are welcome too.
UPDATE: The setTimeout solution below worked for me. The other missing piece was that I have to issue a "browser stop" command to stop the original submission that's in progress before I can issue a re-submit. Otherwise, my re-submit command will just be ignored by the browser.
The usecase here is simple - if the upload didn't finish in N minutes, remove/clear the field using javascript and resend the form.
You don't need to control the upload in the basic implementation, just safely asume that if you set a timeout to resend, it won't happen if the first attempt was successful and the page reloaded.
jQuery pseudocode:
setTimeout(function(){
$imageFieldNode.remove();
$form.trigger('submit');
},30000);//after 30 seconds
The more advanced way is to use a ready solution for controlled upload. They work like that:
upload starts
js prompts the server in intervals with a GET query to get the size of content that was already received.
everytime it gets the info - it reports progress.
You can do a lot with these libs.
You can think about the approach used in popular webmail clients (when attaching files to a message):
The files are uploaded independently (i.e. before) of the form submit, using javascript. Each of the files are stored in a temporary directory, and after the upload succeeds the user can proceed with the action.
The upload status is displayed to the user, and if it fails the main action (form fill/submit) does not get interrupted.

Read a remote XML file or copy it to locale (using Java)?

My question is about reading a remote XML file by using Java.
My files are stored in one device that runs Windows CE. I should access to few of these devices several times per day.
Which solution is more efficient considering network constraints, stablishment of a TCP session and data loss: to open and read the file remotelly or get a copy locally to the server and process it then?
Thank you very much.
It seems u want the files to be read from client by server, whereas it most cases its the other way round. In this case you should have some push functionality from the client to server and this can be over HTTP.
Or you can have a Http connection listener running in the client which accepts request from server and sends back the XML file to the server. Essentially its like a server thread running in the client.
Not sure if u running JAVA on Windows CE. Look for solutions in Windows CE HTTP listener.
See if it helps

Categories