Friday, April 8, 2016

Firebug for Chrome

As a Firefox user I have normally used Firebug to debug HTTP requests and trace the execution of a request.
In Chrome most people see to be happy with the Control-Shift-I (= “more tools” / “Developer tools”) , then in the Network tab, enable “preserve logs”

Thursday, April 7, 2016

EJB : Stateless vs Singleton and in particular this answer
The EXCELLENT book "Beginning EJB 3: Java EE 7 Edition" (that I highly recommend to buy) explains it very well:
"Introduced in EJB 3.1, a singleton session bean is a session bean component that is instantiated only once per application. For an application, only one instance of a singleton session bean can ever exist. Once instantiated, a singleton session bean lives for the full duration of the application. The singleton session bean maintains its state between client invocations, but it cannot save that state after a container shutdown or crash. Similar to stateless and stateful session beans, the singleton session bean is comprised of a bean class and, optionally, one or more business interfaces."

Monday, April 4, 2016

WebLogic: No known valid port for Default[t3]: 7001, No available router to destination.

if you get a funny error like:
The Message-Driven EJB failed while creating a JMS Connection ... No known valid port for: 'Default[t3]:t3(t3): ... No available router to destination.
check Oracle Support document "JMS Servers Fail To Communicate via T3 in WebLogic Server (WLS): "No available router to destination" (Doc ID 948192.1)", it could be that you should declare t3s as default protocol in the config.xml section for the server.

Javassist and code instrumentation

Looking for a way to trace an Error in a static initializer block, I have stumbled upon this example of how to dynamically redefine an ExceptionInInitializerError :
import java.lang.instrument.*;
import javassist.*;

public class InitializerLoggingAgent implements ClassFileTransformer {
  public static void premain(String agentArgs, Instrumentation inst) {
    inst.addTransformer(new InitializerLoggingAgent(), true);

  private final ClassPool pool = new ClassPool(true);

  public byte[] transform(ClassLoader loader, String className, Class classBeingRedefined, ProtectionDomain protectionDomain, byte[] classfileBuffer)  {
    try {
      if (className.equals("java/lang/ExceptionInInitializerError")) {
        CtClass klass = pool.makeClass(new ByteArrayInputStream(classfileBuffer));
        CtConstructor[] ctors = klass.getConstructors();
        for (int i = 0; i < ctors.length; i++) {
        return klass.toBytecode();
      } else {
        return null;
    } catch (Throwable t) {
      return null;
Download javassist zip from here and explode it
Download Ant (Ant? yes, Ant!) from here
Build javassist source and put the javassist.jar file in the lib folder of your project, add it to build classpath.
Follow instructions to jar your JavaAgent with a MANIFEST.MF containing
Manifest-Version: 1.0
Premain-Class: InitializerLoggingAgent
Can-Retransform-Classes: true

run java -javaagent:agentjar.jar MainClass
It works! Your Error is intercepted and stacktrace is printed! No more silently failing static initializers!
If you forget to add "Can-Retransform-Classes: true" in the MANIFEST.MF, you get the infamous "java.lang.UnsupportedOperationException: adding retransformable transformers is not supported in this environment"

Sunday, April 3, 2016

HTTP/1.1 502 Bad Gateway HTTP transport error: Unable to tunnel through proxy. 
Proxy returns "HTTP/1.1 502 Bad Gateway" HTTP transport error: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 502 Bad Gateway" at at at at at at at at at at at at at 

It's not clear what is causing this problem...

ExceptionInInitializerError , NoClassDefFoundError and ClassNotFoundException
"an exception occurred during evaluation of a static initializer or the initializer for a static variable."
"no definition of the class could be found"

the above 2 are LinkageError (Error). The one below is an Exception:
Thrown when an application tries to load in a class through its string name using:

    The forName method in class Class.
    The findSystemClass method in class ClassLoader .
    The loadClass method in class ClassLoader. 

but no definition for the class with the specified name could be found. 

Difference between NoClassDefFoundError and ClassNotFoundException is well explained here: "This is thrown when at compile time the required classes are present, but at run time the classes are changed or removed or class's static initializes threw exceptions. It means the class which is getting loaded is present in classpath, but one of the classes which are required by this class are either removed or failed to load by compiler. So you should see the classes which are dependent on this class."

Examples: this will throw a
Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ArithmeticException: / by zero
 at com.pierre.pvtest.Test.(

package com.pierre.pvtest;

public class Test {
 static int a = 1 / 0;
 public static void main(String[] args) {

If I compile this:
package com.pierre.pvtest;

public class Test {
 public static void main(String[] args) {
  Test1 test1 = new Test1();

package com.pierre.pvtest;

public class Test1 {
 public void hello() {
  System.out.println("Test1 hello");


then I delete Test1.class, I get this:
java com.pierre.pvtest.Test
Exception in thread "main" java.lang.NoClassDefFoundError: com/pierre/pvtest/Test1
        at com.pierre.pvtest.Test.main(
Caused by: java.lang.ClassNotFoundException: com.pierre.pvtest.Test1
        at java.lang.ClassLoader.loadClass(
        at sun.misc.Launcher$AppClassLoader.loadClass(
        at java.lang.ClassLoader.loadClass(
        ... 1 more

Saturday, April 2, 2016

EJB 3.2 tutorial with Manning, EJB 3 in Action

The book is decent, but half of the prose could be removed and the narrative could be much more terse. It seems that often the authors get carried away by their literary velleity. Unfortunately the code examples are incomplete, as always happens the "import" statement are omitted, so that it really takes some effort to try the examples. However, code is provided on the Manning site.

java:/[app-name]//[!fully-qualifiedinterface- name] java:comp java:module java:app java:global

@AroundInvoke @Interceptors , default interceptor in ejb-jar-xml

@TransactionAttribute @ApplicationException
@Schedule @Schedules
@WebService @SOAPBinding @WebMethod @WebResult
@Produces @Consumes
@EntityManager @Entity