Maven 2.2.0 Released

Posted in java by pedro | Wednesday, July 1st, 2009 at 12:59 pm

New version of maven was released. One of the benefit is that there are many bug fixes and some regression from 2.1.0 was fixed.

The main issue when upgrading is that new maven requires Java 1.5 or later! If you still have system which must be build for Java 1.4 you’ve got trouble to overcome. There is guide how you can configure your pom to use ensure that you do not use API from later version t han suggested.

This is achieved by using animal-sniffer. Animal-sniffer is project which goal is to provide tools for developers which must use many different Java versions. For example I use java 1.4, java 5 and java 6 in my daily work.

Currently there are maven plugin and command line tool. Command line help us to find which class is compiled by which java version, it may help to spot libraries or modules compiled by higher version than our project.

Most helpful is maven plugin which can be configure in such way that it rise build error if we use API from higher java version. Assume the code:

package info.pietrowski;

public class Main {
    public static void main(String[] args) {
        BigDecimal d = new BigDecimal(10); // this constructor is since 1.5
        System.out.println(d);
    }
}

Now we configure maven to target for 1.4 java platform, but JAVA_HOME points to java 5 or higher

...

  maven-compiler-plugin
  
    1.4
    1.4
  

...

Running this program with different java version drive to the output

$ jdk1.6.0_13/bin/java -classpath test.jar info.pietrowski.Main
10
$ j2sdk1.4.2_16/bin/java -classpath test.jar info.pietrowski.Main
Exception in thread "main" java.lang.NoSuchMethodError: java.math.BigDecimal.(I)V
        at info.pietrowski.Main.main(Main.java:10)

The problem is that compiler choose BigDecimal constructor based on rt.jar, and despite produce bytecode 1.4 compatible, can run it because rt.jar in java 1.4 do not have this constructor.

The rescue for this problem is animal-sniffer maven plugin which we can configure like this:

...

    org.jvnet
    animal-sniffer
      
        
          animal-sniffer
compile
            
              check
            
            
              
                org.jvnet.animal-sniffer
                java${jdk.level}
                1.0
              
            
          
        
      
        
          org.jvnet.animal-sniffer
          java${jdk.level}
          1.0
          sig
        
     
   
...

Where jdk.level is property for target java version. Also this two resource has some configuration guidance:
Signature Checker guideline and Guide to building 1.4 project.

Currently they have signatures for

  • java1.3
  • java1.4
  • java1.5
  • java1.6

And hopefully you do not have to use older java than 1.3.

After this change pom like suggested and try to build by java higher than 1.5 we get this error message:

[INFO] [animal-sniffer:check {execution: animal-sniffer}]
[INFO] Checking unresolved references to org.jvnet.animal-sniffer:java1.4:1.0
[ERROR] Undefined reference: java/math/BigDecimal.(I)V in ...\target\classes\info\pietrowski\Main.class
[INFO] ------------------------------------------------------------------------
[ERROR] BUILD ERROR
[INFO] ------------------------------------------------------------------------
[INFO] Signature errors found. Verify them and put @IgnoreJRERequirement on them.

Thanks to Marcin who test with me this configuration and finally put this into our organization parent pom for all our developers.

Galileo Galileo

Posted in java by pedro | Friday, June 26th, 2009 at 6:40 pm

Eclipse Galileo just have arrived. It is common that every product in newest version go to the top. Currently there is Eclipse Galileo, and nearly finished NetbeansIDE 6.7RC3 and InteliJ 9.0M1. As always this products comes with great new features for us developers. So I decided to go through new features and see what’s going on in Eclipse Galileo.

First at all Netbeans introduce integration with Project Kenai which is quite nice project hosting it really nice stuff. Both InteliJ and Netbeans has quite nice language support such as PHP, Ruby, Grovy. Also Maven support is better with each release. In fact Eclipse is based rather on plugins not released with standard edition as this plugins are more independent as core platform. InteliJ has quite nice Flex technology and what’s new come at last with OSGi support.

The most important feature in Galileo is Cocoa support both 32 and 64 bit with OpenGL it look really cool. We can use it for OSX application development. The second great in my opinion feature is that compare editor works now more like Java editor especially content assist is very useful. I’m code quality fun, so every new quality assurance feature is welcome. Currently compiler can detect when we do not have hashCode() method implemented when we override  equals() method, also dead code detection is quite nice but unfortunately it still do not find such a code.

if (DEBUG) {
  deadCode(); // dead code due to DEBUG is set to false.
}

The minor improvement {@inheritDoc} tag which allows as to see javadoc from our and parent documentation comment. The most used rename refactoring now is in Quick Fix menu. Editor choosing from Open Resource dialog is also useful. JUnit was upgraded to 4.5 and Equinox implements a lot of feature of new OSGi R4.2 specification.

Oh and one more now we can compare word documents, great stuff for my boss :) .

Finally try it, it worth to do this, and as always in few month another IDE became the better, competition is very good for us.

VisualVm performance tuning tool

Posted in java, technology by pedro | Sunday, June 14th, 2009 at 1:35 am

Few weeks ago I’ve got Performance Tuning Training made by Kirk. It was great time for me and great training. If you can manage such training go there. You won’t regret it. After this training I’ve got some deeper knowledge about performance tuning and performance tuning tools. One of such tool is VisualVm.  If you download latest Java 6 update 14 (May 28), than you have one more option to perform application tuning. Beside of jps, jstat, jmap you get jvisualvm. The tool has long story, from NetBeans plugin to standard java tool.  If you have earlier java version you can also download it separately from project page visualvm.dev.java.net .

The VisualVm developers made very good job providing plugin API. If you write any plugin for NetBeans Platform you know how to write plugin for VisualVm tool. In this post I will describe available plugins. How to install them and how to use them. The plugin menu is under Tools->Plugin. We can setup there plugins repositories. We can also manage our plugins (install, uninstall, activate, deactivate).

As I write this post currently available plugins are:.

VisualVM-Glassfish: Advanced monitoring of Glassfish server. I use Glassfish as development server in daily work and the plugin is very useful.  I will not describe this plugin. It has very nice and detailed description made by Masoud Kalali.

Java ME Priofiler Snapshot Viewer
: Java ME SDK 3.0 offline profiling session viewer.  I’ve got very little with Java ME, so I stay just on this short  description.

VisualVm-Extension : Provide support for extending VisualVM for new JDK, JVM etc. It is for extension proposes in the future.

Memory Sampler : Experimental sampling memory profiler. It’s similar to jmap -histo, an additional feature over the jmap  is possibility to display delta results. If you try to find heap memory problem or PermGen space problems this tool can help you to figure out what going there.

memory sampler

From the top on the left you can set refresh rate, on the right you can set Heap, PermGen samples and "Perform GC" and "Heap Dump" on demand. Both histograms has short information about classes, instances and overall size, and has three operation, "Pause" stops refreshing and it waits to push "Update" button, and "Deltas" switch to delta result mode. On the bottom we have filtering option to the histogram. Quite nice functionality for free.

VisualVM-JConsole: There is another command line tool called jconsole, and you can provide additional plugins for them, this plugin allows you to put the same jconsole plugin into VisualVm tool. More information.

VisualVM-MBean: MBean viewer from jconsole,  You have access to all registered MBean. I assume that you are familiar with JMX and MBeans, so I just put screen shot for you.

MBean Browser

On the left is MBean tree group by MBean’s category, and on the right are MBean’s properties, grouped by Attributes, Operations (methods), Notification (if MBean defines notifications) and Metadata (all information provided with MBean).

Visual GC: Integration of the Visual GC tool into VisualVM. It is quite nice and quick view of java heap utilization, maybe it is not so detailed but you can quickly figure out what’s going on.

VisualGC

On the left we have spaces with information how full space are, on the right we have space trends and on the bottom we’ve got heap histogram.

SysTray: Allows to minimize VisualVM into system tray. Unfortunately it is not supported on Mac OS X.

KillApplication: Very simple functionality, from menu we can kill any java attached application. Just press right mouse button on the application and choose "Kill Application".

VisualVM-JvmCapabilities: Sample plugin displaying JVM capabilities in Overview tab. It is for learning proposes.

Demo plugin

In additional tab we see more information about currently attached JVM.

VisualVm-Logfile Module: Logfile Plugin needed by TDA Plugin. Support JVM log file parsing, we can use it into our plugins.

VisualVm TDA ModuleThread Dump Analyzer Plugin. Now you can use TDA directly from VisualVm. Currently there are some problems with this plugin, but from my point of view it’s worth to manage this problems. Problems mainly are relevant for displaying properly TDA workspace. This is sample screen after manage displaying problems.

Thread Dump Analyzer

On the top you have to action button, they are responsible for Thread Dump tree operation. In the upper left corner you have standard TDA thread groups with monitors. On the right TDA displays current chose group and on the bottom TDA display current chose thread. The great thing is that in thread panel you can pressed "Thread Dump" button and after that you simply select Thread Dump and TDA is opening automatically. Great tool.

Is this all ?!?

In fact no. Of course you can look around jconsole plugin or write it by ourselves. Another option is to write by ourselves VisualVm plugin which can be very easy task due to VisualVM Sample Collection. This is a collection of NetBeans example projects which are VisualVM plugins. There are also some others sample projects, you can find them here.  Also VisualVM team provides great information how to start with VisualVM API.

Currently there is under development a plugin to Garbage Collection Histogram Tool. Another Plugin in development is BTrace plugin. We can install it by adding additional VisualVm plugin repository. All the information is provided for you here.

Btrace plugin

You can write BTrace script to find out what’s going on. User Guide is here.

With GHisto is more work to do. The team is working hard, actually you can download GCHisto. You download and run it by java -jar command.

, I’m still waiting for the VisualVM plugin.

Conclusion

I install all of this plugins, and I disabled SysTray and JavaME Profiler. I put it on the USB stick and have always with me. Unfortunately it works the best with Java 6, so if you have legacy systems it will be hard to use all the features. Also if you go into problems such as this:

Profiler Agent: JNI On Load Initializing...
Profiler Agent: JNI OnLoad Initialized succesfully
Profiler Agent: Waiting for connection on port 5140 (Protocol version: 8)
Profiler Agent: Established local connection with the tool
#
# An unexpected error has been detected by Java Runtime Environment:
#
#  EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x6ddc1475, pid=2768, tid=4136
#
# Java VM: Java HotSpot(TM) Server VM (11.3-b02 mixed mode windows-x86)
# Problematic frame:
# V  [jvm.dll+0x291475]
#
# An error report file with more information is saved as:
# c:\development\svn\unite-trunk\hs_err_pid2768.log
#
# If you would like to submit a bug report, please visit:
#   http://java.sun.com/webapps/bugreport/crash.jsp
#

Check if both Application and VisualVM work on the same JVM.  I get this error due to run application on Java 6u13 and VisualVM running on Java 6u14.

ServiceLoader – modularization for free

Posted in java by pedro | Wednesday, April 1st, 2009 at 11:53 pm

Another outcome of NetBeans training I was mention in previous post was information about java.util.ServiceLoader class. Current application are very large, and to provide maintenance and easy of development we can build it from modules. There are so much information about Dependency Injection, that probably everyone knows about it, frameworks such as Spring Framework, Google Guice, Pico Container etc.  OSGi with  Equinox, Apache Felix and Knopflerfish implementation and NetBeans Module API and even maven and ivy tool. This frameworks and tools help us to keep our application modular. But … Did you know that by using Java version 6 we have DI for free. ServiceLoader class allows that, and now we can write modular application without using framework with many megabytes of classes. So how it works. Assume that we have business interface :

public interface BusinessInterface {
    void businessMethod(String data);
}

And we have two implementation in different modules.

public class BusinessFirstImpl implements BusinessInterface {
   public void businessMethod(String data) {
     System.out.println("First processing " + data);
   }
}
public class BusinessAnotherImpl implements BusinessInterface {
   public void businessMethod(String data) {
     System.out.println("A pnother processing " + data);
   }
}

Two make this working, every jar must have META-INF/services directory which contains file which names is exactly the same as FQN of the interface.

metainf1

In this file we should have single line per contract implementation

info.pietrowski.first.BusinessFirstImpl
info.pietrowski.another.BusinessAnotherImpl
...

We can discover our service using ServiceLoader class. This is the main class.

public class DependencyMain {
  public static void main(String[] args) {
    ServiceLoader implementation = ServiceLoader.load(BusinessInterface.class);
    for (BusinessInterface impl : implementation) {
       impl.businessMethod(impl.getClass().getName());
    }
  }
}

Depending on which jars you have in classpath we can have one of  outcome.

>java -cp main.jar info.pietrowski.DependencyMain
>java -cp main.jar;first.jar info.pietrowski.DependencyMain
First processinginfo.pietrowski.first.BusinessFirstImpl
>java -cp main.jar;another.jar info.pietrowski.DependencyMain
Another processinginfo.pietrowski.another.BusinessAnotherImpl
>java -cp main.jar;another.jar;first.jar info.pietrowski.DependencyMain
Another processinginfo.pietrowski.another.BusinessAnotherImpl
First processinginfo.pietrowski.first.BusinessFirstImp

Download full code source and jar files has also sources. Next time you can use this mechanism in simple and light application.

WeakReference common mistake

Posted in java by pedro | Wednesday, April 1st, 2009 at 4:55 am

Last weekend I was on NetBeans Platform Training conducted by Geertjan and Karol Harezlak from Sun Microsystem. This was a great training. One slot was about Netbeans Issuezilla and how to contribute, provide patches and so on. Karol showed us his daily work, and he showed one of the bugs he had reported and how looks all the process from issue report to make issue resolved.

All attendees try to help Karol to do this, I’ve got some fillings that the solution is still wrong but I wasn’t sure. So after training I’m came home and try this.

Code Snapshot:

codesnapshot

Wrong method : The problem is that after checking that  we have a reference and it is not null, and using it we may have gc executed and another call to refString.get() is null !!!

wrong-method

Good method: We crate strong reference during all method execution so both refString.get() is null or not during all method body.

good-method

To run this you should adjust -Xmx parameter. -Xmx256m works for me.

So the problem is that even we check reference is not null, garbage collector can start and after check we’ve got  NullPointerException. The solution is easy, we must make strong reference (just for the method execution).

I hope this will help you in daily life.  Here is source file, so you can check it on yout own.

about me

My name is Sebastian Pietrowski. I've finished Warsaw University as Master degree. I started my journey with Java 1.1 with Thread and JDBC programing in 1998 as I worked for merlin.pl. In 1999 I've passed Java Programer Certificate for Java 1.2, and was solution architect of merlin.pl infrastructure when we was moving from pl/sql to J2EE. It was great performance optimization with 10 times more req/sec than in requirements and 85 times faster as original solution.

Currently I work as Expert Software Development Java at F.Hoffmann-La Roche. The company was founded in 1896 and today, Roche employs over 80.000 people. After work I'm involved in activities related to Scala/Lift, Ruby/Rails/Merb, Python/Django. This is because I try to be pragmatic also I'm focused on application performance and tuning with success in my daily work.

My Yoda's motto: Do, or do not. There is no try.