1. Summary
  2. Files
  3. Support
  4. Report Spam
  5. Create account
  6. Log in

Hello World

From patterntesting

Jump to: navigation, search

Contents

Hello World

Although PatternTesting is an AspectJ library we will start with a simple Java application - the famous hello-world program:

package hello;

public class World {
    public static void main(String[] args) {
        System.out.println("hello world!");
    }
}

When you start this program you will see "hello world!" on the console. But that's not a big surprise. Now create a lib directory and put the following libraries inside:

Java Build Path (Eclipse)

The aspectjrt.jar is normally provided by the AspectJ plugin AJDT. But because we have still a pure Java project we must provide it as additional library (we will change that later when we use AJDT). From the commons projects we need only the jar files of the downloaded archives. Add the jar files to your build path (Eclipse: Project > Properties > Java Build Path) and change the code to:

package hello;

import patterntesting.runtime.monitor.ClasspathMonitor;

public class World {
    public static void main(String[] args) throws InterruptedException {
        System.out.println("hello world!");
        ClasspathMonitor.registerAsMBean();
        Thread.sleep(300000);
    }
}
jconsole

This will register the ClasspathMonitor at the MBeanServer and you can use the JConsole (or another JMX-console) to inspect the classpath. The sleep() statement let the program live for 5 minutes so you have a chance to use the JConsole. Use at least Java 6 to start the JConsole. If you can't connect to your application try (see http://oli.blogger.de/20100127/)

jconsole -Djava.rmi.server.hostname=localhost

As you can see in the image you can ask the ClasspathMonitorMBean for the bootclasspath or normal classpath, which classes or packages are loaded and other things. Here in this simple hello-world application you should detect no surprising values but in a web environment (e.g. inside tomcat) some of the values like the classpath could be very interesting. At the moment of this writing (May 2009) the ClasspathMonitor is only tested for the default classloader and for the Tomcat classloader (and Weblogic support is under work).

Under "Operations" you can ask the MBean some things like which is the classpath for "java.lang.String", which is the resource for "log4j.xml" and other things.

Hello World Profiled

Till now we have used the PatternTesting library as a normal Java library. Now we will change this and use it as AspectJ library. For this reason we must convert the project into a "AspectJ project". Do you have installed the AJDT plugin from http://www.eclipse.org/ajdt/? If not do it now and restart Eclipse.

If the AJDT is installed you should see the entry "AspectJ Tools > Convert to AspectJ Project" in the context menu of the selected project (see also http://oli.blogger.de/stories/1242343/). Convert the hello-world project into an Aspectj project and remove the aspectjrt.jar from the Java build path - this library is now provided by AJDT. If you start the World application again you should see nearly the same values in the JConsole.

Aspect Path

Now open the project properties and select the entry "AspectJ Build". Add the patterntesting-rt.jar in the tab "Aspect Path" (see image). Now put the annotation @ProfileMe before the hello.World class:

@ProfileMe
public class World {
    ...
}
jconsole

Start the hello-world application as before. Start the JConsole and open the ProfileStatistic MBean. When you select "Operations" you will see a button "dumpStatistic". When you press this button you will see in the console something like INFO: profiling data dumped to /tmp/profile4375161835311945187.csv: PatternTesting will create a [CSV file] in the temp directory, where the values are separated by a semicolon (;). You can open this table e.g. with OpenOffice or Excel and will see the measured times of all you methods in your World class (ok, that's two: the main method and default constructor):


Label Unit Total Avg Hits Max Min
patterntesting.runtime.monitor.ClasspathMonitor.getClasspathClassSet() ms 201,2 201,2 1 201,2 201,2
patterntesting.runtime.monitor.ClasspathMonitor.addClassesFromDir(Set, File) ms 102,57 102,57 1 102,57 102,57
patterntesting.runtime.monitor.ClasspathMonitor.addClassesFromArchive(Set, File) ms 88,45 17,69 5 60,25 1,3
patterntesting.runtime.monitor.ClasspathMonitor.createClasspathClasses() ms 0,21 0,21 1 0,21 0,21
hello.World.main(String[]) ms 0 NaN 0 0 1,80E+308
new hello.World() ms 0 NaN 0 0 1,80E+308

As you can see the ClasspathMonitor is also profiled because it has some expensive method calls. But more interesting are the values for our World class: the main method has a hit of "0", because it has not yet finished (the column "Hits" represents the number of method calls). And the default constructur World() is never called (that's true).

You can put the @ProfileMe annotation not before the class but before a method or constructor only - then only this method or constructor will appear in the table. But if you put it in front of the class (as done here in this example) you will also see the methods and constructors which are never called.


Hello World Checked

PatternTesting provides to check libraries:

  1. Check.CT for compile time checks (patterntesting-check-ct-1.x.x.jar)
  2. Check.RT for runtime checks (patterntesting-check-rt-1.x.x.jar)

TODO: Fortsetzung folgt...

Lessons Learned

How to start with PatternTesting and use it as pure Java library

  • add the following libraries to your classpath:
    • patterntesting-rt-1.0.3.jar (or a newer version)
    • aspectjrt.jar
    • commons-lang.jar
    • commons-io.jar
    • commons-logging.jar
    • junit.jar
  • call ClasspathMonitor.registerAsMBean()
  • start java -Dcom.sun.management.jmxremote.local.only=false -Dcom.sun.management.jmxremote ... hello.world
  • start jconsole

How to use PatternTesting as AspectJ library

  • convert the project into a AspectJ project
  • add patterntesting-rt-1.0.3.jar to the Aspect Path
  • use some of the provided annotations (e.g. @Profile)
  • start the application and jconsole as done before
Personal tools