Tuesday, May 20, 2008

Controlling Module Dependencies in Eclipse

J2EE Module Dependencies is a critical issue in an enterprise application. You need your one web module to access some resources while prohibiting others. Similar is the case with EJB modules.

Let’s figure it out with Eclipse Web Tools Platform. I consider that you all have ability to create projects and modules in Eclipse; so skipping those steps. I have Eclipse 3.3 with WTP 2.0 M6.

I have created project with following details.

MyProject – Enterprise project

MyEJB_Module – Contains bean class

MyEJBClient_Module – Contains interfaces of EJB, VO, and BeanUtil.

MyWeb_Modules – Holds JSPs, servlets and all presentation tier stuffs.

MyJava_Module – Contains common classeslike constants, utilities.

Starting with MyProject, select properties from right click menu. In property dialog, select “J2EE Module Dependencies”. You will find all the modules. Select all as project is dependent on all modules.

Now you can configure dependency of individual module, let’s take an example of web module. In “J2EE Module Dependencies” window, you will find a radio button group which would has by default “Use EJB Client Jars” option selected. That radio button groups defines visibility of EJB jar.

As clients of any EJB should be isolated from bean classes, it is preferable not to select other options where you can define dependencies with EJB jar file. After all, EJB clients should not have any kind of dependencies with EJB jar.

Now here if I select MyEJB_ModuleClient.jar only, MyWeb_Module won’t able to access classes defined in MyJava_Module until you redefine module dependencies.

Similarity you can define dependencies with Utility project, specifically saying third party libraries. You want one module to access one jar and isolate others all you need to do is add library in main project (MyProject in my case) and then configure dependencies of other modules.

This feature takes care of dependencies on behalf of you and let you concentrate on other tasks.

Thursday, May 15, 2008

Servicemix PollingComponentSupport - stuck if lower period and higher threadPoolSize

Again i got one issue with servicemix Poller components.

We have two FilePoller components here and we kept the period to 10 minutes. Which means the instance of FilePoller will run at every 10 minutes. Now say if I have two instances, each run at 10 minutes, and 5 minutes gap between them. So it means after every five minutes, files will be picked by one of them. And the WorkManager, which is given to FilePoller as property, has threadPoolSize of 30.

All was working fine with this configuration.30 threads in pool, every five minutes any of two pullers will pick files. But now we have to change it. When we changed the period from 10 minutes to 1 minute, we got strange errors. Yes even you won't believe it. While testing concurrency we got, FileNotFound exception even though the file exists there, and many other concurrency issues. We looked into the logs and found that sometimes the load goes only on one server and the other is always idle doing nothing. Because when it comes, the first one is doing task.

Then we decided to decrease the thredPoolSize. Becasue if puller get 30 files for 30 threads from the pool, and when it comes to 31st file from the folder and if any of previous 30 threads is in the pool after it has completed its job, then it will take the file. It means, though we have pool size of 30, it may be possible that it can process more than 30 files per instance (say 32 to 38 files). So we decided to make pool size to 10.

Now after making 10 it seems to be working fine. Each one is picking almost equal numbers of files and sharing load as it should. Let’s see if more issues are coming or not.

So, key thing to be remembered is
"Keep you threadPoolSize and period on which instance will run in proportion to each other. If period is long then threads can be more but if you want to reduce the period, you must take care of pool size."

Tuesday, May 13, 2008

Java optimization and performance

No new Exception if not needed

Many of us do this when we want to throw an error. But if you are throwing exception from another catch, then don't make new exception object. Keep throwing the same exception which you caught if possible. This will be better for performance. And also in new exception usually we change the description only to reflect the perfect error cause, but other ways are there to track it.

Avoid excessive synchronization

Yah threading can be a bit complex situation sometimes in java. And we all use synchronization to avoid dead locks. But it needs a more attention then we give it. Try to make a block of code synchronized rather then making the whole method synched. The whole method will stop all other threads, waiting to go inside it. It will generate more overhead. So keep the design like we need less sync code.

Release DB and File IO

This is the most case in which we all do something wrong.
DB connections must be closed after completing your DB transactions because its a limited resource.

Also if you are using File IO, then must close file streams after completing the task.

Best way for this is, to write the releasing code in 'finally' block of the method.

Use StringBuffer/StringBuilder rather than String

We use string manipulations in our daily coding many times. And all we do is always making new String object. As most of us know, String is immutable object (which means once assigned you can't change its value), so when you assign existing String object another value, it will make new String object. And most string operation generates lot of small String objects.

If it’s possible to use StringBuffer or StringBuilder instead of String then its more more way better. Because it will be one object having all your data in it and you can get String form it when you want.

Now StringBuilder is nonSynchronized and StringBuffer is synchronized. So if you don't have threading issues and don't need Sync, then you must use StringBuilder which is included since jdk 1.5.

Later on we will talk about more points also when i remember them :)

Friday, May 9, 2008

servicemix problems: NormalizedMessage getContent,copyPropertiesAndAttachments()

Though servicemix is widely used and very nice ESB framework, it has some basic problems.The one i am talking about here is very little thing but it can be annoying if you forget this or don't know this.

While creating and sending NormalizedMessage object from any data, after doing that if you read data using NormalizedMessage.getContent() method and send the message object with exhchange to deliveryChannel, using DeliveryChannelImpl.send(MessageExchange) method, then you will get null exception in DeliveryChannelImpl class.

Yes this is true.I don't know why, but it removes content from the message object.Only the content is removed and the properties are still there.

You can read properties from it in same component as many times as you want.If you are forwarding same message to next component, properties will automatically goes.But if you read content once, you must set it again :(.

Also remember that if you need to make new MessageExchange, then you must copy all properties and attachments using TransformComponentSupport.copyPropertiesAndAttachments(outExchange,inMsg,outMsg) method.Then all your data from one message will be copied to another message.

So keep checking this if you getting null Exceptions while sending message through DeliveryChannel.

Spring Framework problem : SqlReturnResultSet() not returned

One more problem with servicemix and spring is in DAO classes, when you make any private static class of StoreProcedure, then in its constructor you must declare SqlReturnResultSet before you declare SqlParameter.Otherwise you will not be able to find return data from StoredProcedure execution.Check the code below,
public MySp(JdbcTemplate jdbcTemplate){
super(jdbcTemplate,"sp_mysp");
declareParameter(new SqlReturnResultSet("strRs",rsMyResultSet));
declareParameter(new SqlReturnResultSet("strRs1",rsMyResultSet1));
declareParameter(new SqlParameter("myId", Types.INTEGER));
compile();
}
so keep in mind while using Spring DAO support.Must declare resultsets first.


Thursday, May 1, 2008

Using Struts tags - <logic:iterate>,<logic:equal>,<bean:write>

I was wondering what should i write about now.And suddenly i got this to do.
I have a small change in the jsp which is made using struts framework.In struts, there are many tags which are easy to use and generate output fast.easy to write.

In struts, we have ActionServlet,Action classes and ActionForms.

ActionServlet will transfer the request to ActionServlet with ActionForms containing all the HTML form data.so you can access any data of the of ActionForm from Servlet request.

See the below code.then we will talk regarding it.

<select name="sel1" id="selTestStruts" style="width:90px;">
<option value="0" selected="selected">Please Select
<logic:iterate id="myBean" name="someForm" property="someFormProp1">
<logic:equal value="test" property="someStrPropOfMyBean" name="myBean">
<option value='<bean:write name="myBean" property="someStrPropOfMyBean"/>'><bean:write name="myBean" property="someNamePropOfMyBean"/></option>
</logic:equal>
</logic:iterate>
</select>


here we are trying to fill HTML select box using struts' <logic:iterate>, <logic:equal><bean:write> tags.

<logic:iterate> will iterate on 'someFormProp1' of 'someForm' which is in the http request.it will make 'myBean' from property 'someFormProp1' which is a List of some data.

You can simply write any property from that 'myBean' we made above.But i also shown here how you can put logic to seperate things on some property value so you don't need to put logic in the SP or generate another property for it.

<logic:equal> checks the value of property named 'someStrPropOfMyBean' with the string given in 'value="test"' attribute (means with 'test' here). If the value matches then only it will make new <option> for <select> box.

same thing you can do with not including some specific value match using <logic:notEqual>

<bean:write> tag will simply write property 'someStrPropOfMyBean' of 'myBean' which is a object of some Value Object class from the List property 'someFormProp1' of 'someForm'.

Though this is very simple thing but i write because may be this can be helpful to some newbies.