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.

AOSD – Finally Thoughts

Posted in java, methodology, technology by pedro | Friday, March 6th, 2009 at 9:59 am

We started with Keynote by John A. Stankovic from Universtity of Virginia, it was really cool keynote. He talk about defense system  The dynamics aspects where use to implement mechanism “right defense at the right time”. He describe real life problems with messages encryption, power management and sensor localization. The localization problem comes from that GPS do works only on open space. Thanks to aspects they provide sensor communication protocol. Also power management was treated as crosscutting issue and John describe protocols they develop for this propose. The final John thought is : “Flexibility offered by Dynamic AOP has great potential”.
I went to demo sessions, there was two demos, the first one is out of my interests, the second was quite interesting and open my eyes. I thought that JDK classes and aspect are not woven because of security reasons (this is what AspectJ team claims). Now I know that this issue is due to bootstrap classloader. It makes impossible to wave aspect to for example String class. The demo was about the tool which can do it. There are tools for that purpose called MAJOR and CARAJillo, one limitation of this tools is that JVM classes can only be woven statically. Great stuff.

Industry Panel was about “Challenges and Roadmap for Using AOSD in Industry”. The panel was rather boring. It was very slow and some trash talking: “it not easy to adopt new things to industry”, “risk management”, “academic solution doesn’t scale”, “no trust for aspect”,  “company policies ” and so on. Finally one of the panelist said that “Why we should use aspects if OOP currently works well”.There was few examples of industry usage from Accenture and Siemens.

Another demo session, two quite nice demos. First Lavash quite interesting probably not so aspect oriented framework of tools to automatic requirements processing. The second is in early stage and was about suggestion pointcut modification as we write new code. This may be very interesting tool in the future to keep programmers from silly mistakes. I will keep eye on it.

Industry session was more likely as panel. One good tough was that academic researcher should provide tools to make aspects fully testable. There was another aspect language proposal called “e” which mainly differs from AspectJ that AspectJ is focus on classes and “e” language on units. The session was improved by Uwe Hohenstein from Siemes. Uwe shows real project usage of aspects. Despite obvious examples such as connection pool monitoring,  performance monitoring. Siemens use aspect to address challenges of integrating 3rd party software in a maintainable manner keeping 3rd party software untouched. Most of the examples comes from Siemens Soarian project.

The last day came so quickly. AOSD is high level rather academic conference, but we should learn from the best.

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.