27 July 2012

Motorbikes Can't Turn Corners - The Need For an Independent Viewpoint in Product Evaluations


The release of JBoss EAP 6 has spawned a couple of blog posts from Oracle and Red Hat attempting to sling mud at each other over features that may or may-not be included in each other's application servers. The blogs (https://blogs.oracle.com/middlewareplace/entry/why_should_you_choose_oracle and http://planet.jboss.org/post/true_false_oracle_fud) make interesting reading because they show just how entrenched in their own ideas, the two camps are. Both blog posts are full of inaccurate claims and counterclaims regarding the competitors offering, while over-hyping their own. This highlights just how important it is to get an independent view when making product selections or comaprisons. It is clear from the blog posts that neitehr really understand what their competitors offering is or how it works.

Oracle WebLogic and JBoss Application Server come from different backgrounds, have evolved and developed in different directions, and are fundamentally architected differently. While there is now competition and overlap between them, they were historically targeted at different market segments. Comparing them on a feature-by-feature basis with a marketing check-list is rarely productive, and as these blog posts show, can easily be manipulated - because you are just looking for yes or no answers.

It is like two motoring enthusiasts arguing over whether a car or motorbike is the superior method of transport. Both have engines, and one could have a more reliable, fuel efficient or powerful engine than the other, however the owners decide to argue over other matters - "Motorbikes cannot turn corners because they don't have steering wheels" says the car owner, "Cars can't carry luggage because they don't have panniers" retorts the motorbike rider. To an outside observer that knows the differences between cars and motorbikes, this argument is ridiculous, motorbikes and cars can both turn corners and both carry luggage. They are just designed in different ways and use different approaches to do so. There are ways in which motorbikes and cars differ, which may make one of them more suited to a particular use-case than another, but there is no "one correct way" of making a motor vehicle turn corners.

These same concepts apply to application servers (and other products) - a tickbox comparison is not going to be gair, especially when it is done by the vendor of one of the products. To make an informed decision about which product is best for you, you need to look at it with an independent eye, and focus on if and how it can do the things that matter to you.

The blog posts by Oracle and Red Hat do make some good points in their comparisons, but these are lost in the FUD and mudslinging. C2B2 intend to do a full analysis of the posts and the points that they raise, but we will take our time and make sure it is accurate. Rhetoric and innacuracies are not helpful to anyone.

Matt Brasier
@mbrasier

Using Custom Helpers with Byteman

Byteman comes with a set of built-in operations but what about when we want to extend those operations in order to perform our own custom operations?

In this article I'm going to take a look at creating custom helpers for use with Byteman.


What is Byteman?

Byteman is a bytecode manipulation tool which simplifies tracing and testing of Java programs. Byteman allows you to insert extra Java code into your application, either as it is loaded during JVM startup or even after it has already started running. The injected code is allowed to access any of your data and call any application methods, including where they are private. You can inject code almost anywhere you want and there is no need to prepare the original source code in advance nor do you have to recompile, repackage or redeploy your application. In fact you can remove injected code and reinstall different code while the application continues to execute.

For further detail on how Byteman works and to download it head over to the Byteman website

http://www.jboss.org/byteman/


What are custom helpers?

Byteman comes with a set of built-in operations available for use in your rules. However this set of operations is not fixed. By default Byteman uses its own helper class org.jboss.byteman.rule.helper.Helper.


The set of operations that are available for use in rules can be altered by providing an alternative helper class. If you want the helper to still contain the built-in operation simply extend org.jboss.byteman.rule.helper.Helper.


Any non-abstract class can be specified as the helper. Its public instance methods automatically become available as built-in operations in the rule event, condition and action.

So, that's a quick overview of Byteman and custom helpers. Now, let's see what we can do...

I'm going to look at 3 basic examples.

1) Display when a method is entered and when it exits.
2) Display the contents of an array variable within our code (using a custom helper).
3) Create a custom helper that will print the classloading hierarchy of an object (its classloader and all parents).

Pre-requisites:

  • Byteman is installed.
  • The environment variable BYTEMAN_HOME is set to point to the byteman install directory

Basic Example


First up a very simple example to show how Byteman works. This will use one of the in-built operations traceln to output some text at a given point in the code.

We need 2 things - a Java class that we can modify using Byteman and a rule that states how to modify it.

First up the Java class :

public class TestApp
{
    public static void main(String[] args)
    {
        TestObject testObject = new TestObject();

        String[] testArray = {"One", "Two", "Three"};

        System.out.println("Doing stuff in main method....");
    }
}

As you can see we have a very simple class that creates a TestObject (code for this below), creates an array of strings and then outputs some text so we can see that the code is running when we test it.

public class TestObject
{
    public TestObject()
    {
        System.out.println("Constructed a new TestObject");
    }
}

Now, we need a rule that will be triggered when we enter the main method.

RULE Trace - Main Entry
CLASS TestApp
METHOD main
AT ENTRY
IF true
DO traceln("======ENTERING MAIN======")
ENDRULE

So, as you can see the rule itself is very simple.

We give it a name : Trace - Main Entry
We state the class, in this case the one we just created - TestApp. 
We state the method we are injecting into - main 
We state where to do the insert - AT ENTRY (to the method).

Following this is the rule itself, in this case a very simple one just for illustrative purposes. If true (so, always), call the traceln method (in the Helper class) and output some text indicating that we've entered the main method.

The exit rule is identical except it outputs on exit of the the method.

RULE Trace - Main Exit
CLASS TestApp
METHOD main
AT EXIT
IF true
DO traceln("======EXITING MAIN======")
ENDRULE

Save these two rules into a file called traceRules.btm in the same place as the code.

Now, in order to run this firstly we need to build our code :

javac TestApp.java

Then we need to run it with our rules :

java -Dorg.jboss.byteman.verbose -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:traceRules.btm TestApp

Couple of things to note here :

-Dorg.jboss.byteman.verbose - this is useful for debugging purposes and to see what Byteman is doing under the covers.

-javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:traceRules.btm - this tells the JVM to use the Byteman agent code and that the script we want to use is the one we just wrote containing our two rules - traceRules.btm

On running this you should see a lot of trace from running in verbose mode stating what is happening followed by the running of our code with the new code inserted. If we run without verbose mode (simply take out the -Dorg.jboss.byteman.verbose argument) you should see :

======ENTERING MAIN======
Constructed a new test object
Doing stuff in main method....
======EXITING MAIN======

So far, so simple. With no changes to the original code we have managed to insert our own code, albeit just outputting some debug info at the start and end of our method.


Custom Helper - displaying internal data


Next up we will look at building a custom helper that will allow us to access a variable within the TestAppcode, the testArray.

No changes are needed to TestApp.

Firstly we need to create a Helper class that will output the contents of the array. This is where I first ran into difficulties.

Initially I intended to use a generic function to display the contents of any array, as follows:

public <E> void displayArray(E[] inputArray)
{         
    for (E element : inputArray)
    {       
        System.out.printf("%s ", element);
    }

    System.out.println();
}

However Byteman's type checker does not understand generics. Therefore if we want to handle different array types we would need a different method for each type. In our case we are only dealing with a String[] therefore to keep things simple we will only add this method to our helper class.

The full class is below :


public DisplayArrayHelper
{
    protected DisplayArrayHelper()
    {
    }

    public void displayArray(String[] strings)
    {
        System.out.print("Array contents : ");

        for (String s : strings)
        {
            System.out.print(s + " ");}
 }
}


Next we need a rule :

RULE Display Array Rule
CLASS TestApp
METHOD main
HELPER DisplayArrayHelper
AT EXIT
IF true
DO displayArray($testArray)
ENDRULE

Save this as displayArrayRule.btm

Note - here we are stating that rather than use the default Helper we are using our own custom helper - DisplayArrayHelper. We are also accessing a variable within our code - $testArray.

In order to run our example we need to build our Helper class (adding the Byteman jar to the classpath) :

javac -cp ".;%BYTEMAN_HOME%\lib\byteman.jar" DisplayArrayHelper.java

We also need to recompile the TestApp class using the -g flag to generate debugging info. It is necessary to compile with -g if you want to refer to a local variable in the location (AT) clause. it's exactly the same if you want to refer to it in the BIND, IF or DO clauses.

And to run it :

java -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:displayArrayRule.btm TestApp

Here we get the following output :

Constructed a new test object
Doing stuff in main method....
Array contents : One Two Three

Again, a very simple example but shows how easy it is to create a custom helper and to retrieve data from within the code.

Custom Helper - displaying classloader information


Lastly we will look at another helper class, this time one that displays the classloading hierarchy of an object.  Again, no changes need to be made to the TestApp class, aside from building it with the -g flag. Our helper class is as follows:

public class ClassLoaderHelper
{
    protected ClassLoaderHelper()
    {
    }

    public void getClassLoaderHierarchy(Object obj)
    {
        System.out.println(ClassLoaderUtils.showClassLoaderHierarchy(obj, "Testing"));
    }
}

Again, we override the Helper class then we create one method which takes in an Object and displays its class load hierarchy. To do so we use a utility class called ClassLoaderUtils. This is as follows :


/*
 * Copyright 2002-2005 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * Utility class for diagnostic purposes, to analyze the
 * ClassLoader hierarchy for any given object or class loader.
 *
 * @author Rod Johnson
 * @author Juergen Hoeller
 * @since 02 April 2001
 * @see java.lang.ClassLoader
 */

public abstract class ClassLoaderUtils {

  /**
   * Show the class loader hierarchy for this class.
   * Uses default line break and tab text characters.
   * @param obj object to analyze loader hierarchy for
   * @param role a description of the role of this class in the application
   * (e.g., "servlet" or "EJB reference")
   * @return a String showing the class loader hierarchy for this class
   */
  public static String showClassLoaderHierarchy(Object obj, String role) {
    return showClassLoaderHierarchy(obj, role, "\n", "\t");
  }



  /**
   * Show the class loader hierarchy for this class.
   * @param obj object to analyze loader hierarchy for
   * @param role a description of the role of this class in the application
   * (e.g., "servlet" or "EJB reference")
   * @param lineBreak line break
   * @param tabText text to use to set tabs
   * @return a String showing the class loader hierarchy for this class
   */

  public static String showClassLoaderHierarchy(Object obj, String role, String lineBreak, String tabText) {
    String s = "object of " + obj.getClass() + ": role is " + role + lineBreak;

    return s + showClassLoaderHierarchy(obj.getClass().getClassLoader(), lineBreak, tabText, 0);

  }

  /**
   * Show the class loader hierarchy for the given class loader.
   * Uses default line break and tab text characters.
   * @param cl class loader to analyze hierarchy for
   * @return a String showing the class loader hierarchy for this class
   */

  public static String showClassLoaderHierarchy(ClassLoader cl) {

    return showClassLoaderHierarchy(cl, "\n", "\t");

  }



  /**
   * Show the class loader hierarchy for the given class loader.
   * @param cl class loader to analyze hierarchy for
   * @param lineBreak line break
   * @param tabText text to use to set tabs
   * @return a String showing the class loader hierarchy for this class
   */

  public static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText) {

    return showClassLoaderHierarchy(cl, lineBreak, tabText, 0);

  }



  /**
   * Show the class loader hierarchy for the given class loader.
   * @param cl class loader to analyze hierarchy for
   * @param lineBreak line break
   * @param tabText text to use to set tabs
   * @param indent nesting level (from 0) of this loader; used in pretty printing
   * @return a String showing the class loader hierarchy for this class
   */

  private static String showClassLoaderHierarchy(ClassLoader cl, String lineBreak, String tabText, int indent) {

    if (cl == null) {

      ClassLoader ccl = Thread.currentThread().getContextClassLoader();

      return "context class loader=[" + ccl + "] hashCode=" + ccl.hashCode();

    }

    StringBuffer buf = new StringBuffer();

    for (int i = 0; i < indent; i++) {
      buf.append(tabText);
    }

    buf.append("[").append(cl).append("] hashCode=").append(cl.hashCode()).append(lineBreak);

    ClassLoader parent = cl.getParent();

    return buf.toString() + showClassLoaderHierarchy(parent, lineBreak, tabText, indent + 1);

  }



}



We then create the rule that utilises our helper class : 

RULE Class Loader Rule 
CLASS TestApp 
METHOD main 
HELPER ClassLoaderHelper 
AT EXIT 
IF true 
DO getClassLoaderHierarchy($testObject); 
ENDRULE 

Save this as classLoaderRule.btm. 

Now we need to build the 2 new classes : 

javac ClassLoaderUtils.java 
javac -cp ".;%BYTEMAN_HOME%\lib\byteman.jar" ClassLoaderHelper.java 

and then run the rule against our TestApp code : 

java -javaagent:%BYTEMAN_HOME%\lib\byteman.jar=script:classLoaderRule.btm TestApp 

Here we see something similar to this : 

Constructed a new test object Doing stuff in main method.... object of class TestObject: role is Testing [sun.misc.Launcher$AppClassLoader@553f5d07] hashCode=1430215943         [sun.misc.Launcher$ExtClassLoader@32f4a24a] hashCode=854893130 context class loader=[sun.misc.Launcher$AppClassLoader@553f5d07] hashCode=1430215943 


Conclusion

Byteman is a powerful tool allowing the user to inject code into previously built code. It's particularly useful in  multi-threaded environments where normal debugging is really difficult. Whilst the Helper class contains many useful methods through the use of custom helpers it is possible to extend this to do whatever is required.

There are a couple of points that were noted whilst working with Byteman : 

  • Although the Byteman site claims "Since it only needs access to bytecode this means it can modify library code whose source is either unavailable or unable to be recompiled." this is not strictly true.Whilst it can modify it you will not be able to access local variables without recompiling with the -g flag to generate debugging info.
  • You cannot use generics in your helper class method signatures as the type checker does not understand generics. 

Andy Overton

13 July 2012

Free Hands-on Technical Workshops in July


Data Grids Explained - Coherence, GemFire & Infinispan

24th of July 2012, 9:30am-4pm, Institute of Directors, London


  • Do you want to learn what data grids are and how they can help you improve reliability, scalability and performance of your platform?
  • Do you want to learn about the differences between various current grid products out there and receive an independent expert's view?
  • Do you want to play with data grid technology and get some hands-on experience with data grids?
Come to our workshop and learn what data grids are and why they are cool! The workshop will give you a chance to compare and find out more about the three current grid products in the market: GemFire, Infinispan and Coherence.
As the leading and independent experts in data grids for scalability and performance and expert group members of JSR 347: Data Grids for the Java Platform & JSR 107 Java Caching, C2B2 offers a unique independent view of the grid products.

To secure your place and see detailed agenda please visit www.c2b2.co.uk/datagrids

Why should you attend?
  • You will discover the key requirements for compute grids
  • You will learn about the differences between three current grid products
  • You will receive an independent view of data grids from a leading data grid expert
  • You will have a chance to play with data grid technology
What are the key benefits of data grid?
  • Data Reliability - ensures no down time or data loss.
  • Linear Scalability - ensures you can grow as your customer base grows.
  • Extreme Performance - your service is fast and responsive to customers. 
  • Elastic scale up and scale down - ready for modern cloud architectures so you can save money by flexing your hardware to match customer demand.
For more information contact us on 08450 539457 or email marketing@c2b2.co.uk


_____________________________________________________________________________________________________


Unleash the Power of Oracle WebLogic 12c: Architect, Deploy, Monitor and Tune JEE6

25th of July 2012, 9:30am-4pm, Oracle Office, London

  • Can you maintain performance in response to rapid peaks in customer traffic?
  • Do you need to quickly diagnose and resolve performance and scalability issues?
  • Does your application need to cope with high volumes of concurrent transactions?
  • Do you currently run a single application server instance and wish to explore ways that you can scale out your architecture? 
Come to our Workshop and get bootstrapped in the use of Oracle WebLogic 12c for high performance systems.The workshop, run by an Oracle Application Grid Certified Specialist will start with a simple WebLogic 12c system which will scale up to a distributed, reliable system designed to give zero downtime and support extreme throughput.

To secure your place and see detailed agenda please visit www.c2b2.co.uk/weblogic

Why should you attend?
  • You will learn how to leverage WebLogic and Application Grid features to build high performance, resilient clusters and gain deep insight into your application footprint
  • You will learn to architect WebLogic Domains that will survive outages and provide great performance benchmarks
  • You will find out what's new in WebLogic 12c
  • The workshop is a great networking opportunity; you can talk with your peers and industry experts to swap knowledge and experience
For more information contact us on 08450 539457 or email marketing@c2b2.co.uk

12 July 2012

JBoss EAP 6 Webinars

On the 20th of June Red Hat announced JBoss Enterprise Application Platform 6. To help you understand how it works, Matt Brasier - Head of Consulting at C2B2 - run a series of live webinars giving you an opportunity to take a look at the newest offering from Red Hat and understand how it works. The webinar recordings are now available to watch on C2B2 YouTube channel.

Part 1 - JBoss EAP 6 First Look


In Part 1 Matt installed JBoss EAP 6 and walked the attendees through the new structure and components of the application server:



Part 2 - JBoss EAP 6 Domain Mode Features

Following on from our first look at EAP 6 in the second part we took a deeper look into the features of domain mode in JBoss EAP 6, including configuring domains, deploying applications and managing the servers:

11 July 2012

C2B2 is Speaking at JavaOne 2012


Steve Millidge, C2B2 Director, and Nick Wright, Senior Middleware Consultant, will be speaking at JavaOne 2012 in San Francisco 
(Sep 30 - Oct 4 2012)

Steve's talks:
BOF6336 - Data Grids and WebSocket: Delivering Real-Time Push at Scale  (Birds-of-a-Feather Session)
The real-time Web is coming with WebSocket in HTML5. The big question is how to deliver event-driven architectures for WebSocket at scale. This session, delivered by a member of the JSR 347 data grids expert group, provides insight into how combining data grids with WebSocket can deliver enterprise-scale push to Web devices. The session first provides an introduction to WebSocket and then delves into typical data grid architectures and how they deliver linear scalability and high availability. It then looks at the event capabilities inherent in data grids that, when hooked up to a WebSocket server, can deliver data grid updates in real time to HTML5 devices.
Find out more

TUT6361 - Building WebSocket Applications with GlassFish and Grizzly (Tutorial Session)
WebSocket is coming with HTML5, and building rich real-time event-driven Web applications will be the future. GlassFish, as of Release 3.1.2, now supports WebSocket, so now is an ideal time to learn this new technology. This tutorial builds a WebSocket Web application hosted on GlassFish that can deliver stock updates from a data grid directly to the Web browser in real time. It starts by introducing WebSocket and the APIs for building a server-side WebSocket application in GlassFish. It then moves on to the client side and builds an updating stock ticker, using HTML5’s Canvas and JavaScript. For a final flourish, the presenter hooks up GlassFish to a data grid that will deliver updates at scale in real time.
Find out more


Nick's talk (presented with Nigel Deakin):

BOF6562 - Meet the Experts: JMS 2.0 Expert Group  (Birds-of-a-Feather Session)
This session gives you an opportunity to meet the members of the JMS 2.0 (JSR 343) expert group. You’ll be able to ask questions about existing JMS features as well as the new features proposed for JMS 2.0. You’ll also be able to give your comments on JMS 2.0 directly to the expert group, and if you’d like to suggest a JMS feature that would be useful for your project, come and promote it!
To find out more about Steve's, Nick's and others' talks and presentations visit Oracle Open World and JavaOne Content Catalogue


4 July 2012

Red Hat acquires FuseSource

Red Hat have announced their acquisition of FuseSource. You may have not heard of FuseSource but you have probably heard of Active MQ, Camel, ServiceMix and Apache CXF. Well FuseSource provided paid support services for these projects and also employs a number of key commiters as well. Originally created out of IONA then acquired by Progress FuseSource have been around for quite a while. The business models and open source cultures of FuseSource and Red Hat match well, probably much better than the business models of FuseSource and Progress. From a business perspective the merger makes sense. Acquiring Fusesource gives Red Hat an opportunity to use their marketing clout and Linux penetration to try and convert many of the large customers that use the Apache variants into paying Red Hat support customers. Something that Fusesource without the Linux installed base has struggled to do. Red Hat already do this with their Tomcat support packages (who knows how successfully). If you are using ActiveMQ, Tomcat, Camel or ServiceMix on top of RHEL expect a call from your friendly Red Hat sales rep ;-)
However what about the technologies? Well there is a lot of overlap;

ActiveMQ <-> HornetQ
Camel <-> SwitchYard/JBoss ESB
Apache CXF <-> JBoss Web Services
FuseHQ <-> RHQ (JBoss Operations Network)
Apache Service Mix <-> SwitchYard/JBoss ESB

In our experience the Apache (FuseSource) technologies are much more widely deployed than the equivalent Red Hat technologies however usually in the Apache variant rather than the FuseSource variant. My technical prediction for a year or so from now (I have no inside knowledge) on the winners and losers in that list are;
ActiveMQ (much more widely deployed than HornetQ and has a good reputation)
SwitchYard (merging in Camel and ServiceMix, SwitchYard already supports Camel)
Apache CXF (already an option in JBoss)
RHQ (FuseHQ is a licensed version of Hyperic which is a VMWare product)

More interesting is what will Red Hat do with development as these are all currently Apache projects will they transition future development onto JBoss.org or leave them in Apache?
From a C2B2 perspective we already provided consultancy services on ActiveMQ, Camel, CXF and ServiceMix so no change for us. In fact we have had discussions with FuseSource in the past about becoming partners so one less partner fee to pay.

On Balance, good synergy, good news.

Steve Millidge