Wednesday, April 28, 2010

Hudson Address Bind Issue (java.net.BindException: Address already in use)

Hudson is a great software tool, but unfortunately yesterday as soon as I updated my hudson WAR, it stopped working and keeps giving me error below.

java.net.BindException: Address already in use
at java.net.PlainDatagramSocketImpl.bind0(Native Method)
at java.net.PlainDatagramSocketImpl.bind(PlainDatagramSocketImpl.java:82)
at java.net.DatagramSocket.bind(DatagramSocket.java:368)
at java.net.MulticastSocket.(MulticastSocket.java:147)
at java.net.MulticastSocket.(MulticastSocket.java:112)
at hudson.UDPBroadcastThread.(UDPBroadcastThread.java:58)
at hudson.model.Hudson.(Hudson.java:606)
at hudson.WebAppMain$2.run(WebAppMain.java:224)

It happens due to previous hudson application failed to close the port 33848.

It took my 2 hours but finally i was able to find the solution.

This is the line that saves my life :)
-Dhudson.udp=33849

Solution is the simple below line that need to be added in your tomcat's catalina.sh.

JAVA_OPTS="$JAVA_OPTS -Dhudson.udp=33849"


Reference: http://issues.hudson-ci.org/browse/HUDSON-5177

HTTP Clent 4 HTTP PUT Multipart Client Code Sample

Few days back got stuck in usage of HTTP Client 4.0 Library usage for Multi Part Put Request.
Finally found a workaround and thought to share with everyone.

Below is the Sample code.


import java.io.File;

import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;


public class PutRequestTest {

public static void main(String[] args) {
try {
HttpClient httpclient = new DefaultHttpClient();
httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

HttpPost method = new HttpPost("http://localhost:8080/AdamService/service/sharedshelf/json/insertImage");

File file = new File("/Users/imranlakhani/a.JPG");
MultipartEntity mp = new MultipartEntity();
mp.addPart("projectId", new StringBody("1234", Charset.forName("UTF-8")));
mp.addPart("Name", new StringBody("image.jpg", Charset.forName("UTF-8")));
mp.addPart("username", new StringBody("imran", Charset.forName("UTF-8")));
mp.addPart("instituteId", new StringBody("2", Charset.forName("UTF-8")));
mp.addPart("password", new StringBody("", Charset.forName("UTF-8")));
FileBody fileBody = new FileBody(file);
mp.addPart("File", fileBody);
method.setEntity(mp);

System.out.println("executing request " + method.getRequestLine());
HttpResponse response = httpclient.execute(method);
HttpEntity resEntity = response.getEntity();

System.out.println(response.getStatusLine());
if (resEntity != null) {
System.out.println(EntityUtils.toString(resEntity));
}
if (resEntity != null) {
resEntity.consumeContent();
}

httpclient.getConnectionManager().shutdown();

} catch (Exception e) {
e.printStackTrace();
}

}
}