Posts Tagged ‘java’

Proper timeout handling with Apache HttpClient

August 24th, 2009 by Brian | 5 Comments | Filed in work

I’ve seen some really bad things happen when developers don’t code in proper timeout handling. Occasionally I’ve been asked what the best way to handle timeouts is – so I thought I’d share my take on it:

  1. MultiThreadedHttpConnectionManager connectionManager =  new MultiThreadedHttpConnectionManager();
  2. HttpConnectionManagerParams params = connectionManager.getParams();
  3.  
  4. params.setConnectionTimeout(connectiontimeout); //set connection timeout (how long it takes to connect to remote host)
  5. params.setSoTimeout(sotimeout); //set socket timeout (how long it takes to retrieve data from remote host)
  6.  
  7. HttpMethodBase baseMethod = null;
  8.  
  9. try {
  10.   HttpClient httpClient = new HttpClient(connectionManager);
  11.   httpClient.getParams().setParameter("http.connection-manager.timeout", poolTimeout); //set timeout on how long we’ll wait for a connection from the pool
  12.  
  13.   baseMethod = new GetMethod();
  14.   int statusCode = httpClient.executeMethod();
  15.  
  16.   …
  17. }
  18. catch (ConnectTimeoutException cte ){
  19.   //Took too long to connect to remote host
  20. }
  21. catch (SocketTimeoutException ste){
  22.   //Remote host didn’t respond in time
  23. }
  24. catch (Exception se){
  25.   //Some other error occurred
  26. }
  27. finally {
  28.   if (baseMethod != null)
  29.     baseMethod.releaseConnection();
  30. }

Tags: , , ,

Aptana on Ubuntu 8.10

February 2nd, 2009 by Brian | No Comments | Filed in work

I was having a problem getting Aptana to run on my fresh install of Ubuntu 8.10

I lost the original error message, but it was something like

[ERROR] Invalid key
java.security.InvalidKeyException: Invalid AES key length: 26 bytes


Even though I’m running 32-bit, the solution to include an older version of xulrunner and reference it in a startup script worked like a charm.

In case this page ever dies, the trick is to install xulrunner-1.8.1.3 and use a script similar to this to start Aptana

#!/bin/sh
MOZILLA_FIVE_HOME=/usr/lib/xulrunner-1.8.1.3
if [ $LD_LIBRARY_PATH ]; then
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
fi
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH
~/aptana/AptanaStudio -vm /usr/lib/jvm/ia32-java-6-sun/jre/bin/java

For reference, I am running IBM Java 1.6

#!/bin/sh
MOZILLA_FIVE_HOME=/usr/lib/xulrunner-1.8.1.3
if [ $LD_LIBRARY_PATH ]; then
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME:$LD_LIBRARY_PATH
else
LD_LIBRARY_PATH=$MOZILLA_FIVE_HOME
fi
export MOZILLA_FIVE_HOME LD_LIBRARY_PATH
~/aptana/AptanaStudio -vm /usr/lib/jvm/ia32-java-6-sun/jre/bin/java

Tags: , , , ,