Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Regular Contributor
Posts: 69
Registered: ‎04-05-2010
My Device: Storm
Accepted Solution

HttpConnection issues in threads

I have a requirements where My blackberry app upload & download files from the server. To improve the upload and download performance, I am launching multiple threads and send the multiple chunks of the files through multiple connections. My server is capable of taking chunks from multiple connections and finally integrate them into a single file. Same I follow for downloading files. My blackberry app will launch multiple threads to download various different chunks for each file and write to the file with the appropriate position.

 

Application works fine when I upload and download using single thread. As soon as I start around 4 different threads to upload and download files, Connector.open(url), fails to open any more connections after few chunks of upload or download.

 

I even tried using the ConnectionFactory with retries, still same issie exists. code I using for the http connection is ..

            urlStr= "https://myhost.com/ServiceRest/Service.svc/;deviceside=true;interface=wifi";
            HttpConnection connection = null;
            String curURL = serverURL;
            try {
                  ConnectionFactory cFactory = new ConnectionFactory();
                  cFactory.setTimeoutSupported(true);
                  cFactory.setRetryFactor(1000);
                  cFactory.setAttemptsLimit(3);
                  cFactory.setConnectionMode ConnectionFactory.ACCESS_READ_WRITE);        
                  //cFactory.setTransportTypeOptions(TransportInfo.TRANSPORT_TCP_WIFI , options)
                  ConnectionDescriptor connDesc = cFactory.getConnection(urlStr);
                  connection = (HttpConnection) connDesc.getConnection();
            }
            catch (IOException e) 
            {
                  CSLogger.log("openHttpConnection Exception : " + e.getMessage()); 
                  e.printStackTrace();
            }         

 

Please suggest. Thanks in advance.

Developer
Posts: 204
Registered: ‎08-09-2010
My Device: 9930, PlayBook

Re: HttpConnection issues in threads

I believe that the limit for HTTPConnections on a device is 4 or 5.  This appears to be a system wide limit.

 

Check this post out:
http://supportforums.blackberry.com/t5/Java-Development/HTTP-connection-limit/td-p/79581

 

 

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection issues in threads

In addition, I seriously doubt you will see a significant throughput improvement using more than 2 simultaneous connections.  Even using WiFi.  Try 2 threads, and see how you go. 

Regular Contributor
Posts: 69
Registered: ‎04-05-2010
My Device: Storm

Re: HttpConnection issues in threads

Is there any document where they list this kind of limits in blackberry?

 

I am having lots of issues related to multiple threads, multiple datagram connections and multiple HTTPConnections.

 

There is some code written by me work seemless in Android, however start behaving in inconsistant ways in blackberry.

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection issues in threads

Regular Contributor
Posts: 69
Registered: ‎04-05-2010
My Device: Storm

Re: HttpConnection issues in threads

Based on this document, I can have 32 simultaneous connections over WiFi for above 4.5 OS. I am using 5.0 for my development. I am trying to use 4 connections at a time. I will not be opening new connection until and unless I am closing one of them.

 

As I had said I have four thread which will be opening four different connections for sending the chunks simultaneously. As soon as the sending chunk is completed by one thread, it closes that connection and reopen the new connection for the next chunk.

 

Even though I am making sure that there are only four different connnection active, I start getting Exception where I am trtying to get the connection.

 

Does it mean, blackebrry is not releasing the connection even after I close the connection? or it takes few seconds before releasing it back to the connection pool?

Developer
Posts: 19,612
Registered: ‎07-14-2008
My Device: Not Specified

Re: HttpConnection issues in threads

Is there an issue with the connection to the file?  There are limits on that side as well - assuming you are using File Connection API?

 

What Exception are yo seing?  Is this on the first connection, the 4th connection or after a period of time?

 

Just becuase I have to ask, are you making sure that all the connections (http and input and output streams), plus the file connction streams are all closed?

Regular Contributor
Posts: 69
Registered: ‎04-05-2010
My Device: Storm

Re: HttpConnection issues in threads

[ Edited ]

Sorry for the late reply as the results were being unpredictible. Used to get various different errors when I was trying to use multiple threads with multiple connections.

 

As I had said, current now I am using 4 threads with 4 different connections for uploading to or downloading from the server. Based on suggetions from various different discussions in this forum I implemented more stable code with retries.

 

Now it is working fine, but takes more time in some of the cases because the flush() method of the outputstream of HTTP connection either gets timesout ( Timeout Exception) or blocks for around two minutes before contineuing properly.

 

Any way I can set timeout for the write or flush method of the outputstream of HTTP connection?