Monday, February 7, 2011

thank you axis2 for this handful of close_wait

In one of our projects we are using axis2 1.4 for generating the client side of the web service. Our web service part is working for few hours but gets stuck for no apparent reason. After I investigate a little I saw that it generates lots of sockets with CLOSE_WAIT state which results this freeze. I google a bit about similar problems with Axis2 and saw that this a known bug. Here's the source I used for fixing it. As explicitly calling the garbage collector didn't seem like a good solution, and as I can't upgrade axis2, I opt for changing the HTTP version to 1.0. For this I set a single field in the ServiceStub by calling


serviceStub._getServiceClient().getOptions()
.setProperty(
org.apache.axis2.transport.http.HTTPConstants.HTTP_PROTOCOL_VERSION,
org.apache.axis2.transport.http.HTTPConstants.HEADER_PROTOCOL_10)


from ServiceStub where this object is created.
After that, this CLOSE_WAIT bug is fixed.

Thursday, February 3, 2011

no such method? you must be joking java.lang.NoSuchMethodError

In our project we use Spring Security's new release and we didn't care about the versions of different Spring components like Spring Beans, Core etc and probably left few old versions next to new versions. The result is a nice exception and I had to figure out why.


java.lang.NoSuchMethodError: org.springframework.beans.MutablePropertyValues.add(Ljava/lang/String;Ljava/lang/Object;)Lorg/springframework/beans/MutablePropertyValues;


After a little bit of googling I saw that the root cause is the fact that we use old versions in some Spring components next to new ones. I removed the old ones from pom.xml and everything's fixed.

Friday, January 14, 2011

et tu log4j? java.lang.classnotfoundexception: org.apache.log4j.enhancedpatternlayout

While I was setting log4j for our new project I got the following exception:


log4j:ERROR Could not instantiate class [org.apache.log4j.EnhancedPatternLayout].
java.lang.ClassNotFoundException: org.apache.log4j.EnhancedPatternLayout


I have log4j in my pom and its jar in my local repository so why EnhancedPatternLayout is missing?
This class is not in our log4j but in log4j-extras. You have to add the necessary extras jar to your pom.



log4j
apache-log4j-extras
1.0



I added the version 1.0 because version 1.1 requires log4j 1.2.16 while I'm using log4j 1.2.15.

classcastexception while working with quartz + spring

I was trying to integrate Quartz to Spring and while everything seemed OK I got the following exception:


java.lang.ClassCastException: org.quartz.impl.StdScheduler


I had everything in hand for the simple job I implemented to work.

A simple Job class that implements QuartzJobBean:


public class Job extends QuartzJobBean{

protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
System.out.println("working!");
}
}


The necessary spring context:
































All I had to do is instantiate the bean with getBean() but I was getting "java.lang.ClassCastException: org.quartz.impl.StdScheduler" everytime. My code for instantiating the SchedulerFactoryBean was below:


SchedulerFactoryBean d = (SchedulerFactoryBean) appCon.getBean("scheduler");


I was trying to downcast to a wrong class. It seemed quite straightforward to use SchedulerFactoryBean but it was wrong. As the exception said I changed it to StdScheduler (of Quartz) and I fixed it.
Here's the correct form:


StdScheduler d = (StdScheduler) appCon.getBean("scheduler");


I hope that easy solution will be helpful for somebody out there.

Tuesday, January 11, 2011

datetime fun with mysql: when do i lost my time?

In our project, we chose the data type of a column as datetime in MySQL. We built models that are in line with our tables. For the datetime attribute in the database I chose to use the Date object in Java model and the show begun.

When I tried to use my model's Date attribute for setting the corresponding field (with setDate()) on the prepared statement I saw that the object types dont match. What I have in the model is java.util.Date while the prepared statement asks for java.sql.Date. Ok then I'll convert it. I googled a bit and found a solution, or rather thought that I found a solution.


ps.setDate(1, new java.sql.Date(model.getDateField().getTime()) )


First I was converting to a millisecond unix timestamp with getTime() and then converting it to second (by dividing with 1000) and then wrapping the result into a java.sql.Date object.
The result was far from satisfying. The time part of the date (hour, minute and second) were missing in the result. The date in the model was '2010-10-26 14:13:33' and the result in the database was '2010-10-26 00:00:00'.

After that I googled a bit more and inspected few javadocs then came up with not one but two solutions.


SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
ps.setString(1, dateFormat.format( model.getDateField() ));

The first solution creates a SimpleDateFormat object which formats the Date attribute of the model as a String and we use setString() of the prepared statement for setting it.

The second solution is below.


ps.setTimestamp(1, new java.sql.Timestamp(model.getDateField().getTime()));


We first get the Date attribute and then convert it to unix timestamp in milliseconds for wrapping it into a Timestamp object and call the related setter.

I didn't notice major performance difference in one of the solutions I offer which means that you have to test it in your cases.