Interceptors used for logging app performance

A good idea when we test performance is to not touch the actual production code.

In this case the best thing would be to use a bit of AOP in our case an  EJB3 interceptor which we can remove (or override in XML).

All you need to do is add one line in the Facade /Manager you need to test:

@Interceptors(PerformanceLoggingInterceptor.class)

And have the interceptor as below in your project :

package com.snef.si.util;

import javax.interceptor.AroundInvoke;
import javax.interceptor.InvocationContext;

import org.apache.log4j.Logger;

/**
* Simple performance logging
*
*
*
*/
public class PerformanceLoggingInterceptor {
private static final Logger LOGGER = Logger
.getLogger(PerformanceLoggingInterceptor.class);

@AroundInvoke
public Object trackExecution(InvocationContext context) throws Exception {
LOGGER.info(“Method is ” + context.getMethod() + ” “);
long startTime = System.currentTimeMillis();
Object invocation = context.proceed();
long endTime = System.currentTimeMillis();
LOGGER.info(“Method  ” + context.getMethod() + “took ”
+ (endTime – startTime) + ” miliseconds”);
return invocation;
}

}

The important thing in the code above is the @AroundInvoke annotation which tells the container to wrap the ejb3 call with this interceptor (no matter which business method is called).

All you need to remember is to remove the logging line from the EJB class before the actual release.

Junit – ManyToOne = OneToMany expected

java.lang.ExceptionInInitializerError
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:501)
at junit.framework.TestSuite.createTest(TestSuite.java:61)
at junit.framework.TestSuite.addTestMethod(TestSuite.java:283)
at junit.framework.TestSuite.<init>(TestSuite.java:146)
at org.junit.internal.runners.JUnit38ClassRunner.<init>(JUnit38ClassRunner.java:71)
at org.junit.internal.builders.JUnit3Builder.runnerForClass(JUnit3Builder.java:14)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:29)
at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:57)
at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>(JUnit4TestReference.java:32)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>(JUnit4TestClassReference.java:25)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:41)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:31)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:452)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.lang.RuntimeException: Cant´t resolve releations for property (statutClientSiret) in class (class com.snef.si.referentiel.cli.domaine.ClientSiret)
at com.bm.introspectors.relations.RelationPropertyResolver.findAttributeForRelationAtOtherSide(RelationPropertyResolver.java:74)
at com.bm.introspectors.AbstractPersistentClassIntrospector.processAnnotations(AbstractPersistentClassIntrospector.java:312)
at com.bm.introspectors.AbstractPersistentClassIntrospector.processAccessTypeField(AbstractPersistentClassIntrospector.java:86)
at com.bm.introspectors.EntityBeanIntrospector.processAccessTypeField(EntityBeanIntrospector.java:147)
at com.bm.introspectors.EntityBeanIntrospector.<init>(EntityBeanIntrospector.java:125)
at com.bm.testsuite.dataloader.CSVInitialDataSet.initialize(CSVInitialDataSet.java:163)
at com.bm.testsuite.dataloader.CSVInitialDataSet.<init>(CSVInitialDataSet.java:91)
at com.bm.testsuite.dataloader.CSVInitialDataSet.<init>(CSVInitialDataSet.java:140)
at com.snef.si.referentiel.rd.cli.manager.ClientSiretManagerTest.<clinit>(ClientSiretManagerTest.java:17)
… 21 more

The error was caused by the fact that in the entity class that we were making the test for we had an @ManyToOne mapping made, but in the entity that was targetted by the ManyToOne, the mapping @OneToMany to our first entity was required.

After that we had another error in the managers from because the adnotation @Local(interfaceClass.class) was missing from the Stateless bean.

After that we need to make sure we have all the entities that are being used declared in our Junit test. That means that we need to put them in the private static final CSVInitialDataSet, and in the private static final Class<?>[] USED_BEANS in the class that extends BaseSessionBeanFixture<?>.

No we had another error:

javax.persistence.PersistenceException: org.hibernate.HibernateException: Dialect class not found: com.snef.si.util.CustomHSQLDialect
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:720)
at org.hibernate.ejb.Ejb3Configuration.createEntityManagerFactory(Ejb3Configuration.java:700)
at com.bm.cfg.Ejb3UnitCfg.getEntityManagerFactory(Ejb3UnitCfg.java:161)
at com.bm.creators.SessionBeanFactory.getEntityManager(SessionBeanFactory.java:151)
at com.bm.creators.SessionBeanFactory.getInjector(SessionBeanFactory.java:104)
at com.bm.creators.SessionBeanFactory.createSessionBean(SessionBeanFactory.java:85)
at com.bm.testsuite.BaseSessionBeanFixture.setUp(BaseSessionBeanFixture.java:114)
at org.jmock.core.VerifyingTestCase.runBare(Unknown Source)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: org.hibernate.HibernateException: Dialect class not found: com.snef.si.util.CustomHSQLDialect
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:81)
at org.hibernate.dialect.DialectFactory.buildDialect(DialectFactory.java:42)
at org.hibernate.cfg.SettingsFactory.determineDialect(SettingsFactory.java:409)
at org.hibernate.cfg.SettingsFactory.buildSettings(SettingsFactory.java:119)
at org.hibernate.cfg.Configuration.buildSettings(Configuration.java:2006)
at org.hibernate.cfg.Configuration.buildSessionFactory(Configuration.java:1289)
at org.hibernate.ejb.Ejb3Configuration.buildEntityManagerFactory(Ejb3Configuration.java:713)
… 20 more

That because in the ejb3unit.properties it was defined the property ejb3unit.dialect=com.snef.si.util.CustomHSQLDialect. So We’ve created the class that extends HQLDialect and it worked.

public class CustomHSQLDialect extends HSQLDialect {

public CustomHSQLDialect() {
registerColumnType(Types.BOOLEAN, “number(1,0)”);
registerHibernateType(Types.BOOLEAN, Hibernate.BOOLEAN.getName());
}

}

Now we have another error like the one bellow (that happens to any manager in the project that we want to run a test for):

java.lang.annotation.AnnotationFormatError: Invalid default: public abstract java.lang.Class org.jboss.annotation.ejb.RemoteBinding.factory()
at java.lang.reflect.Method.getDefaultValue(Method.java:716)
at sun.reflect.annotation.AnnotationType.<init>(AnnotationType.java:98)
at sun.reflect.annotation.AnnotationType.getInstance(AnnotationType.java:66)
at sun.reflect.annotation.AnnotationParser.parseAnnotation(AnnotationParser.java:202)
at sun.reflect.annotation.AnnotationParser.parseAnnotations2(AnnotationParser.java:69)
at sun.reflect.annotation.AnnotationParser.parseAnnotations(AnnotationParser.java:52)
at java.lang.Class.initAnnotationsIfNecessary(Class.java:3031)
at java.lang.Class.getAnnotation(Class.java:2989)
at com.bm.ejb3guice.inject.InjectorImpl.getImplicitBinding(InjectorImpl.java:924)
at com.bm.ejb3guice.inject.InjectorImpl.getInternalFactory(InjectorImpl.java:325)
at com.bm.ejb3guice.inject.BindingBuilderImpl$FactoryProxy$1.run(BindingBuilderImpl.java:294)
at com.bm.ejb3guice.inject.InjectorImpl.withDefaultSource(InjectorImpl.java:159)
at com.bm.ejb3guice.inject.BindingBuilderImpl$FactoryProxy.notify(BindingBuilderImpl.java:292)
at com.bm.ejb3guice.inject.BinderImpl.createInjector(BinderImpl.java:277)
at com.bm.ejb3guice.inject.Ejb3Guice.createInjector(Ejb3Guice.java:128)
at com.bm.creators.SessionBeanFactory.getInjector(SessionBeanFactory.java:107)
at com.bm.creators.SessionBeanFactory.createSessionBean(SessionBeanFactory.java:85)
at com.bm.testsuite.BaseSessionBeanFixture.setUp(BaseSessionBeanFixture.java:114)
at org.jmock.core.VerifyingTestCase.runBare(Unknown Source)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:232)
at junit.framework.TestSuite.run(TestSuite.java:227)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:49)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

So, what should we do ?