Updates from July, 2010 Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 11:59 am on July 30, 2010 Permalink |
    Tags: , code snippet, commons, cookie, , httpclient, , networking, proxy,   

    HttpClient (Apache commons) code sample 

    I was playing around with Apache commons Http utilities the last day. I used to use the java.net.* APIs to satisfy my HTTP(s) needs.

    Here is a sample code which I wrote which takes a URL as input, sets the basic request parameters (e.g. cookie string) and set the proxy settings along with the user credentials.

    import org.apache.commons.httpclient.Credentials;
    import org.apache.commons.httpclient.DefaultHttpMethodRetryHandler;
    import org.apache.commons.httpclient.Header;
    import org.apache.commons.httpclient.HttpClient;
    import org.apache.commons.httpclient.UsernamePasswordCredentials;
    import org.apache.commons.httpclient.auth.AuthScope;
    import org.apache.commons.httpclient.methods.GetMethod;
    import org.apache.commons.httpclient.params.HttpMethodParams;
    
    public class HttpClientTest
    {
        public static void main(String[] args)
        {
            HttpClient client = null;
            GetMethod getMethod = null;
            int responseCode = -1;
            byte[] responseStream = null;
    
            String urlString = "http://www.facebook.com";
            String cookieString = null;
    
            try
            {
                // Creating the GetMethod instance
                getMethod = new GetMethod(urlString);
    
                // Retries to establish a successful connection the specified number
                // of times if the initial attempts are not successful.
                getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
                        new DefaultHttpMethodRetryHandler(1, false));
                getMethod.getParams().setParameter("http.socket.timeout", new Integer(5000));
                getMethod.setRequestHeader(new Header("Cookie", "<COOKIE_STRING>"));
    
                // Creating an HttpClient instance
                client = new HttpClient();
    
                // Proxy settings: Configures the proxy host, port & user
                // credentials and the scope of the credentials.
                client.getHostConfiguration().setProxy("<HOST>", <PORT>);
                Credentials credentials = new UsernamePasswordCredentials
                    ("<USERNAME>", "<PASSWORD>");
                AuthScope scope = new AuthScope(AuthScope.ANY_HOST, AuthScope.ANY_PORT);
                client.getState().setProxyCredentials(scope, credentials);
    
                // Sets the user-agent for the client instance
                client.getParams().setParameter("http.useragent", "<USER_AGENT>");
    
                // Sends the GET request and gets the response
                responseCode = client.executeMethod(getMethod);
                responseStream = getMethod.getResponseBody();
    
                System.out.println("Response Code: " + responseCode);
                System.out.println("Response Body: \n" + new String(responseStream));
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
            finally
            {
                getMethod.releaseConnection();
                client = null;
            }
        }
    }
    

    P.S. Did I mention that this is test code? 🙂

    Note: I was looking for an API set which does make use of its own Socket level implementation. I don’t think HttpClient got it’s own implementation as it claims to be 100% Java. If you know any API set which performs better than java.net.* APIs please feel free to share it.

     
  • Subinkrishna Gopi 10:10 am on February 5, 2009 Permalink |
    Tags: , farming, , , , , ,   

    Farming service & JBoss 5 

    jbosscorp_logo

    First of all, I would like to thank whitelassiblog for bringing this to my notice. You can see his comment here on my post.

    As I never did any JBoss clustering in my life, I searched (in fact “Googled“) for it. And I found a small & nice article on it.

    What is the farm service ?

    That is hot-deploying the application archive file (e.g., the EAR, WAR or SAR file) in the all/farm/ directory of any of the cluster members, will cause the application to be automatically duplicated across all nodes in the same cluster.

    If node joins the cluster later, it will pull in all farm deployed applications in the cluster and deploy them locally at start-up time. If you delete the application from one of the running cluster server node’s farm/ folder, the application will be undeployed locally and then removed from all other cluster server nodes farm folder (triggers undeployment.) You should manually delete the application from the farm folder of any server node not currently connected to the cluster.

    Read this article here: http://www.mastertheboss.com/en/jboss-application-server/146-jboss-farming-service.html

    But in JBoss 5 this feature is not available.

    Unfortunately this replacement for farming is not ready available for AS 5.0.0.GA. The Farm Service’s function of replicating deployment archives between file systems will not be maintained.

    At the moment the easiest solution to deploy a JBoss application on JBoss AS 5 is creating a script which copies the file on all the “deploy” folder of your Nodes.

    Hope this helps.

     
    • whitelassiblog 4:06 pm on February 5, 2009 Permalink

      Thanks for the credit. Very kind of you 🙂
      Farm service is a very cool feature that jboss has. It makes me sad why they removed it. Moreover, with no replacement, it is likely to break production grade applications who want to attain cluster wide fault tolerance and migrate to Jboss 5.

    • Subinkrishna G 4:16 pm on February 5, 2009 Permalink

      Hmm. After reading the mastertheboss.com article and your comment even I think so. Anyways thanks again.

    • whitelassiblog 8:08 pm on May 21, 2009 Permalink

      A quick update. The farming service has been restored in JBOSS 5.1.0 CR1.

    • Subinkrishna G 8:24 am on May 31, 2009 Permalink

      Thank you for this update.

  • Subinkrishna Gopi 5:02 pm on January 22, 2009 Permalink |
    Tags: , , , , , , ,   

    A Jboss 5 issue 

    jbosscorp_logo

    Me and my colleague Venu were working on some Jboss 5 stuff  (infact migrating from Jboss 4 to 5) when we got the following exception.

    Exception:

    ERROR [my.package.name] doSomething() Ex: javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

    javax.naming.CommunicationException [Root exception is java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646]

    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:725)
    at org.jnp.interfaces.NamingContext.lookup(NamingContext.java:590)
    at javax.naming.InitialContext.lookup(InitialContext.java:351)

    .. .. ..

    Caused by: java.io.InvalidClassException: org.jboss.ejb3.remoting.IsLocalInterceptor; local class incompatible: stream classdesc serialVersionUID = -3758782076801249473, local class serialVersionUID = 337700910587744646

    at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:546)
    at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1552)
    at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1466)
    at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1699)
    at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1305)

    We are trying to connect to another server which is running on Jboss 4 and trying to access a remote EJB (session bean). Till now I’ve no clear idea about this whole issue. If any of you can put some light to this issue, please.

    I will update this place once we tackle the issue.

    Update 1:
    I found some useful information here: https://jira.jboss.org/jira/browse/EJBTHREE-749

    The problem seems to be mismatching SerialVersionUID in org.jboss.ejb3.remoting.IsLocalInterceptor of  jboss-ejb3-client.jar.

    Update 2: 28-Jan-2009
    This issue has been fixed from Jboss-4.2.2 GA onwards. So all those who are using versions lesser than 4.2.2 are more likely to get this issue.

    Find the JIRA post by Venu here: https://jira.jboss.org/jira/browse/EJBTHREE-1696

     
  • Mathew Varghese 5:30 pm on January 19, 2009 Permalink |  

    Setting connection time-out delay in Java 

    When I was working with HttpUnit, I always faced issues with connection timeout. Hey, thats not an HttpUnit issue :). To set the time-out delay, set a system property “sun.net.client.defaultReadTimeout“.

    System.setProperty("sun.net.client.defaultReadTimeout", "<time in millies>");
    

    Note:
    You can read more about it here: http://java.sun.com/j2se/1.4.2/docs/guide/net/properties.html

     
    • Subinkrishna G 11:41 am on March 3, 2009 Permalink

      There is a slight change in the property name specified. The sun.net.client.defaultReadTimeout will throw a timeout exception if the application is not able to read from the input stream “after establishing a connection” with in the mentioned time out delay.

      The sun.net.client.defaultConnectTimeout will throw a time out if the application is not able to establish a connection with in the mentioned time in milliseconds. The default value of this system property is -1.

      Subin

c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel