Welcome to

Confessions of a build breaker

A blog on Java, JEE and Spring by Jelmer Kuperus

Too many open files

January 4th, 2008 by site admin

Today I was asked to review a piece of code that a coworker of mine suspected was causing “Too many open files“ exceptions in a production environment The code looked innocuous enough. He was using ProcessBuilder to create and run an external application. Regardless I booted into Linux and ran some tests. Now on Linux its fairly easy to find out if a program is leaking file handles. Once a program is started you can look up the process in the /proc file system. The /proc filesystem is a virtual filesystem that resides in the kernels memory it provides you with an easy way to change the behavior of the linux kernel and allows you to view information about currently running processes.

To find out what filehandles a java process is holding onto you simply navigate to /proc/[PROCESSID]/fd There’s a file there for every open file handle.

To count all open file handles you can execute ls /proc/[PROCESSID]/fd | wc -w

Using these simple tools I quickly found out that every time the suspect code was run. 3 filehandles where created that were not being released
As it turns out you need to explicitly close the file handles allocated by Process (eg. Close inputstream, outputstream and errorstream) Process does not release them after the invoked program terminates!

This seems to be a little known fact about Process. Its not mentioned in the javadoc for the process class and just about all the authorative resources get this wrong. Eg.


http://www.javaworld.com/javaworld/jw-12-2000/jw-1229-traps.html


http://www.ibm.com/developerworks/java/library/j-tiger09304.html

Etc

At least sun accepted a bug report for improving the documentation. However its been open since 2006..
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6462165

Now I must admit I still am kind of curious about the application my colleague is developing because i believe the max number of filehandles is 65536 on most modern linux systems :)

Update: you can also use lsof -p PID for this. The added benefit is that for sockets it will also cross reference the from - > to for socket connections

Posted in java | 1 Comment »

Spring 2.5 and Maven problems

December 1st, 2007 by site admin

It looks like the spring 2.5 jars that where uploaded to the official maven repository are broken. The full spring jar is 321 kb large and only contains the spring aop classes.

Anyway I added springs own repository to my pom and then i got the correct jar and sources

<repository>
  <id>spring</id>
  <name>Spring Portfolio Repository</name>   <url>https://springframework.svn.sourceforge.net/svnroot/springframework/repos/repo</url>
</repository>

Posted in java, spring | 3 Comments »

Extensibility jBPM style

October 8th, 2007 by site admin

Currently I am involved in a project that is moving away from a proprietary workflow solution to a JBPM based workflow solution. One of the first things I attempted to do was try and make jBPM integrate with the identity management solution used in the project. I knew that jBPM comes packaged with some sort of identity component so I started reading up on it in the userguide.

“Management of users, groups and permissions is commonly known as identity management. jBPM includes an optional identity component that can be easily replaced by a company’s own identity data store.”

Sounds good. But then I kept on reading

When you want to use your own datasource for organisational information such as your company’s user database or ldap system, you can just rip out the jBPM identity component. The only thing you need to do is make sure that you delete the line …
<mapping resource="org/jbpm/identity/User.hbm.xml"/>
<mapping resource="org/jbpm/identity/Group.hbm.xml"/>
<mapping resource="org/jbpm/identity/Membership.hbm.xml"/>

from the hibernate.cfg.xml
The ExpressionAssignmentHandler is dependent on the identity component so you will not be able to use it as is. In case you want to reuse the ExpressionAssignmentHandler and bind it to your user data store, you can extend from the ExpressionAssignmentHandler and override the method getExpressionSession.
protected ExpressionSession getExpressionSession(AssignmentContext assignmentContext);

Yikes! What more, the reference to ExpressionAssignmentHandler class is actually hardcoded. So you cannot use a class that simply extends from ExpressionAssignmentHandler. It looks like you have to physically break open the jbpm-identity jar and replace the ExpressionAssignmentHandler contained within by a modified version

This must be some new kind of easy

update: Someone already submitted a Jira issue for this

Posted in java | 5 Comments »

What to do with an Interruptedexception

October 5th, 2007 by site admin

Many java methods, such as Thread.sleep and Process.waitFor can throw an Interruptedexception. InterruptedException is a checked exception and as such requires you to explicitly catch it. Up until recently I’ve always simply swallowed this exception. I was fairly confident that this was the proper way to deal with this exception because this is how I’d seen others handle it many times over. As it turns out I was wrong and this is the worst possible way to handle an interrupted exception

Brian Goetz of java concurrency in practice fame has written an article on the subject that is definitely worth a read

If there is anything you should take away from reading this article it is this :

“At the very least, whenever you catch InterruptedException and don’t rethrow it, reinterrupt the current thread before returning.”

Its interesting to see that even the spring guys initially got this wrong It was fixed in spring 1.2.8

Posted in java | 1 Comment »

JSF Security video

June 12th, 2006 by site admin

Today while looking around on google video I found a recording of a presentation by Duncan Mills on JSF Security. Am I the only person who thinks it’s crazy that such a simple and more importantly, very common requirement is so hard to implement in jsf ?

Posted in java | 1 Comment »

Integrating spring and GWT

June 4th, 2006 by site admin

I just completed my first shot at integrating Spring with GWT. You can check it out here

Here’s what you do to expose a simple service.

1) Write the remote service, note that the service no longer extends RemoteServiceServlet however HelloService should still implement RemoteService


public class HelloServiceImpl implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}

2) Define this service in your application context

<bean id=”helloService” class=”nl.jteam.hello.server.HelloServiceImpl”/>

3) expose the bean as a gwt service

<bean id=”defaultHandlerMapping” class=”org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping”/>

<bean name=”/helloService” class=”nl.jteam.gwt.exporter.GwtInvokerServiceExporter”>
<property name=”service” ref=”helloService”/>
<property name=”serviceInterface” value=”nl.jteam.hello.client.HelloService”/>
</bean>

4) Change the clientcode to point to your Spring service. be sure to use an absolute url while running in hosted mode or it will complain


helloService = (HelloServiceAsync) GWT.create(HelloService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) helloService;
endpoint.setServiceEntryPoint("http://localhost:8080/helloservice");

Feedback is appreciated

Posted in java, spring | 14 Comments »

Google WebToolkit remoting

June 4th, 2006 by site admin

Last week I started playing around with the google webtoolkit an Ajax framework that allows software engineers to develop complex ajax web interfaces in java rather than javascript.

I found the google webtoolkit not without it’s limitations, most noteably it is very hard to style / layout pages compared to jsp templates etc. But I do think it has potential for certain types of applications. While I struggled with other component based frameworks such as jsf, tapestry and wicket GWT was really easy to get into even though the documentation is currently still lacking and without compare to that offered by the aformentioned frameworks.

GWT comes with its own rpc framework. I’ll give you an example of how this works

You start out by defining your service interface


public interface HelloService extends RemoteService {
String sayHello(String name);
}

Service interfaces should allways extend from the RemoteService marker interface. Then you create an asychronous companion interface


public interface HelloServiceAsync {
void sayHello(String name, AsyncCallback callback);
}

The relationship between a service interface and its asynchronous counterpart is straightforward:
If a service interface is called com.example.HelloService , then the async interface must be called com.example.HelloServiceAsync. The async interface must be in the same package and have the same name, but with the suffix Async.

I actually don’t like having to repeat myself like this but i suppose it can’t be helped, eventually the code will get translated to javascript and as I understand it all javascript code runs within the context of a single thread. You can’t have a method blocking for 5 seconds while waiting for a response from the server because it would effectively make your application nonresponsive. If you ever played around with DWR you should be familiar with this pattern.

Implementations of the remote interface extend from RemoteServiceServlet which is a thin layer on top of HttpServlet. and takes care of serializing / deserializing arguments and response objects.


public class HelloServiceImpl extends RemoteServiceServlet implements HelloService {
public String sayHello(String name) {
return "hello " + name;
}
}

Once you have this it is very simple to write a simple client.


public class Hello implements EntryPoint {
private HelloServiceAsync helloService;

public void onModuleLoad() {
helloService = (HelloServiceAsync) GWT.create(HelloService.class);
ServiceDefTarget endpoint = (ServiceDefTarget) helloService;
endpoint.setServiceEntryPoint(”/helloservice”);

HorizontalPanel buttonPanel = new HorizontalPanel();
buttonPanel.add(new Button(”Say hello to jelmer”, new ClickListener() {
public void onClick(Widget sender) {
helloService.sayHello(”jelmer”, new AsyncCallback() {
public void onSuccess(Object result) {
String message = (String) result;

Window.alert(message);
}
public void onFailure(Throwable caught) {
}
});
}
}));
RootPanel.get().add(buttonPanel);
}
}
This will add a button to your page. when pressed it will call sayHello on the remote interface and display the resulting message in a javascript alert box.

Overall i turned out to be pretty easy and painless to create some (relatively) sophisticated functionality. The resulting code is very clean, strongly typed and you can use you favorite java ide to write it, so you have autocompletion and all that goodness available to you

Posted in java | No Comments »

copyright © 2oo6 by Confessions of a build breaker | Powered by Wordpress

Ported by ThemePorter - template by Design4 | Sponsored by 47channel