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
Highlighted
Developer
Posts: 69
Registered: ‎08-25-2008
My Device: Not Specified

Re: java.io.IOException: Max connections opened in BB device?

Thanks for your replies,

 

I have used same code of BrowserFieldDemo in JDE samples. I am getting "java.io.IOException: Max connections opened" in Utilities makeConnection() method here i think i should not close connection because it is return the connection which is used by the "processConnection()" this process connection is called from PrimaryResourceFetchThread class run method. Please find code of three classes.

PrimaryResourceFetchThread:

public void run() { HttpConnection connection = ZBBUtilities.makeConnection(_url, _requestHeaders, _postData); _browser.processConnection(connection, _event, _zbbMain); if (connection != null) { try { connection.close(); } catch (Exception exe) { System.err.println("Exception in primaryResourceThread run() ::"+exe.toString()); } }

ZBBUtilities:

public static HttpConnection makeConnection(String url, HttpHeaders requestHeaders, byte[] postData) { HttpConnection conn = null; OutputStream out = null; try { conn = (HttpConnection) Connector.open(url); if (requestHeaders != null) { String referer = requestHeaders.getPropertyValue("referer"); boolean sendReferrer = true; if (referer != null && StringUtilities.startsWithIgnoreCase(referer, "https:") && !StringUtilities.startsWithIgnoreCase(url, "https:")) { sendReferrer = false; } int size = requestHeaders.size(); for (int i = 0; i < size;) { String header = requestHeaders.getPropertyKey(i); // Remove referer header if needed. if (!sendReferrer && header.equals("referer")) { requestHeaders.removeProperty(i); --size; continue; } String value = requestHeaders.getPropertyValue(i++); if (value != null) { conn.setRequestProperty(header, value); } } } if (postData == null) { conn.setRequestMethod(HttpConnection.GET); } else { conn.setRequestMethod(HttpConnection.POST); conn.setRequestProperty( HttpProtocolConstants.HEADER_CONTENT_LENGTH, String .valueOf(postData.length)); out = conn.openOutputStream(); out.write(postData); } } catch (Exception exe) { System.err.println("Exception in ZBBUtilities makeConnection():: exe "+exe.toString()); } finally { if (out != null) { try { out.close(); } catch (IOException e) { System.err.println("Exception in ZBBUtilities makeConnection():: e " +e.toString()); } } } return conn; }

ZBBBrowser (like as BrowserFieldDemo)

public class ZBBBrowser implements RenderingApplication { private static final String REFERER = ZBBUtilities.REFERER; public ZBBBrowser() { _renderingSession = RenderingSession.getNewInstance(); // Enable javascript. _renderingSession.getRenderingOptions().setProperty(RenderingOptions.CORE_OPTIONS_GUID, RenderingOptions.JAVASCRIPT_ENABLED, true); } private RenderingSession _renderingSession; private ZBBMain _zbbMain; private HttpConnection _currentConnection; public void processConnection(HttpConnection connection, Event e, final ZBBMain zbbMain) { _zbbMain = zbbMain; // Cancel previous request. if (_currentConnection != null) { try { _currentConnection.close(); } catch (Exception exe) { System.out.println("Exception ZBBBrowser processConnection()::" +exe.toString()); } } // Assign new connection to the currentConnection _currentConnection = connection; BrowserContent browserContent = null; try { browserContent = _renderingSession.getBrowserContent(connection, this, e); if (browserContent != null) { Field field = browserContent.getDisplayableContent(); if (field != null) { synchronized (Application.getEventLock()) { zbbMain.addBrowserField(field); } } try{ browserContent.finishLoading(); }catch (Exception e1){ System.out.println("IO Exception in ZBBBRowser ::"+e1);} } } catch (RenderingException re) { System.out.println("Exception ZBBBrowser processConnection()... re::" +re); } catch (Exception exe) { System.out.println("Exception ZBBBrowser processConnection()... exe::" +exe); } finally { ZBBSecondaryThread.doneAddingImages(); } } /** * Called when event have been occured. */ public Object eventOccurred(Event event) { int eventId = event.getUID(); switch (eventId) { case Event.EVENT_URL_REQUESTED: { UrlRequestedEvent urlRequestedEvent = (UrlRequestedEvent) event; PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread( urlRequestedEvent.getURL(), urlRequestedEvent.getHeaders(), urlRequestedEvent.getPostData(), event, this, _zbbMain); thread.start(); break; } case Event.EVENT_REDIRECT: { RedirectEvent e = (RedirectEvent) event; String referrer = e.getSourceURL(); switch (e.getType()) { case RedirectEvent.TYPE_SINGLE_FRAME_REDIRECT: // Show redirect message. Application.getApplication().invokeAndWait(new Runnable() { public void run() { Status.show(ZBBUtilities.RE_DIRECT); } }); break; case RedirectEvent.TYPE_JAVASCRIPT&colon break; case RedirectEvent.TYPE_META: // MSIE and Mozilla don't send a Referer for META Refresh. referrer = null; break; case RedirectEvent.TYPE_300_REDIRECT: // MSIE, Mozilla, and Opera all send the original request's // Referer as // the Referer for the new request. Object eventSource = e.getSource(); if (eventSource instanceof HttpConnection) { referrer = ((HttpConnection) eventSource) .getRequestProperty(REFERER); } break; } HttpHeaders requestHeaders = new HttpHeaders(); requestHeaders.setProperty(REFERER, referrer); PrimaryResourceFetchThread thread = new PrimaryResourceFetchThread( e.getLocation(), requestHeaders, null, event, this, _zbbMain); thread.start(); break; } case Event.EVENT_CLOSE: break; // close the appication case Event.EVENT_SET_HEADER: // No cache support. case Event.EVENT_SET_HTTP_COOKIE: // No cookie support. case Event.EVENT_HISTORY: // No history support. case Event.EVENT_EXECUTING_SCRIPT: // No progress bar is supported. case Event.EVENT_FULL_WINDOW: // No full window support. case Event.EVENT_STOP: // No stop loading support. default: } return null; } /** * For getting Blackberry display height */ public int getAvailableHeight(BrowserContent browserField) { // Field has full screen. return Display.getHeight(); } /** * For getting BlackBerry display width. */ public int getAvailableWidth(BrowserContent browserField) { // Field has full screen. return Display.getWidth(); } /** * @see net.rim.device.api.browser.RenderingApplication * #getHistoryPosition(net.rim.device.api.browser.BrowserContent) */ public int getHistoryPosition(BrowserContent browserField) { // No history support. return 0; } public String getHTTPCookie(String url) { // No cookie support. return null; } /** * For getting requested resource. */ public HttpConnection getResource(RequestedResource resource, BrowserContent referrer) { if (resource == null) { return null; } // Check if this is cache-only request. if (resource.isCacheOnly()) { // No cache support. return null; } String url = resource.getUrl(); if (url == null) { return null; } // If referrer is null we must return the connection. if (referrer == null) { System.out.println("!!!!!!!!!!!!!!!!!!ZBBBrowser !!!! referrer is null!!!!!!!!!!!!!!!!!!"); HttpConnection connection = ZBBUtilities.makeConnection(resource .getUrl(), resource.getRequestHeaders(), null); return connection; } else { // If referrer is provided we can set up the connection on a // separate thread. ZBBSecondaryThread.enqueue(resource, referrer); } return null; } public void invokeRunnable(Runnable runnable) { (new Thread(runnable)).start(); } }

 

 

Sorry for such a big message.  Please help me to fix this issue.

Developer
Posts: 1,415
Registered: ‎07-30-2008
My Device: Not Specified

Re: java.io.IOException: Max connections opened in BB device?

I guess there are two issues, maxing out connections because old ones don't get closed and maxxing

them out because you try to open more while others are still active. IIRC, renderingoptions has an

option for maximum requests to make at once or something similar. But, you can count your requests

and closes and see if they match. As a fail safe, if you are sure connections close reliably,

you can keep retrying if you get a max connection throw. This will work even if you don't

close but it will be uselessly slow as the dangling connections take minutes to time out.

 

You will probably eventually find you want to sort requests in this mode too and possibly

redirect/fool the requestor with your own httpconnection subclasses. For example, most files will

be manageably small and you can buffer a result before returning anything etc.

I think stepping through some of their code, they do image caching etc. You may want to try

this before at some point.