2

This is what I've tried so far and my interceptor is not triggered (no "TATATA" in my logs) :

My interceptor AopLoggingInterceptor.java :

package fr.mycompany.bus.flow.reco.ani.custom.interceptor;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

@Aspect
public class AopLoggingInterceptor {

    @Around("execution(* org.mule.api.transport.MessageReceiver.routeMessage(org.mule.api.MuleMessage))")   
    public Object addMonitor(ProceedingJoinPoint pjp) throws Throwable {
        System.out.println("TATATA Before");
        Object object = pjp.proceed();
        System.out.println("TATATA After");
        return object;
    }   
}

META-INF/aop.xml :

<aspectj>

    <aspects>
        <aspect name="fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor" />
    </aspects>

    <weaver options="-verbose">
        <!-- Weave types that are within the org.mule.* packages. -->
        <include within="org.mule.*" />
    </weaver>
</aspectj>

My Mule/Spring config file :

<?xml version="1.0" encoding="UTF-8"?>
<mule >
<spring:beans>
    <context:component-scan base-package="fr.mycompany.bus" />
    <context:annotation-config />
    <aop:aspectj-autoproxy />

    <!-- Aspect -->
    <spring:bean name="aopLoggingInterceptor" class="fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor" />

</spring:beans>
</mule>

My Mule file config consists of one flow with one inbound endpoint, 2 outbound endpoints, loggers and transformers (valid flow widely tested).

VM args :

-XX:PermSize=128M -XX:MaxPermSize=256M -javaagent:D:\path\to\mule\opt\aspectjweaver-1.6.11.jar

Extract from mule file starting in Eclipse which shows weaving is created :

[MuleApplicationClassLoader@2934847] info AspectJ Weaver Version 1.6.11 built on Tuesday Mar 15, 2011 at 15:31:04 GMT
[MuleApplicationClassLoader@2934847] info register classloader org.mule.module.launcher.MuleApplicationClassLoader@2934847
[MuleApplicationClassLoader@2934847] info using configuration /D:/BusToolBox/workspaces/dev/.mule/apps/bus-esb-mrc-reco-ani/classes/META-INF/aop.xml
[MuleApplicationClassLoader@2934847] info register aspect fr.mycompany.bus.flow.reco.ani.custom.interceptor.AopLoggingInterceptor

EDIT

It works nicely with a class included in my project, but not with mule classes : [MuleApplicationClassLoader@6ad5934d] debug generating class 'fr.mycompany.bus.flow.reco.ani.custom.transformer.CustomerDetailToSiebelRecoAniOutputTransformer$AjcClosure1'

EDIT 2

Here is the best result I can get (by using <context:load-time-weaver />), the loading process tries to look for more classes loaded by difference classloaders is , but it results in :

ERROR 2014-08-08 16:00:46,802 [main] org.mule.module.launcher.application.DefaultMuleApplication: null java.lang.IllegalStateException: ClassLoader [org.mule.module.launcher.MuleApplicationClassLoader] does NOT provide an 'addTransformer(ClassFileTransformer)' method. Specify a custom LoadTimeWeaver or start your Java virtual machine with Spring's agent: -javaagent:org.springframework.instrument.jar

If I try to use spring-instrument-3.2.1.RELEASE.jar, I get same result as before (only main classloader is seen). Does it mean there is no hope with Mule ?

Andrei Stefan
  • 51,654
  • 6
  • 98
  • 89
Tristan
  • 8,733
  • 7
  • 48
  • 96
  • Try something: remove everything from your Mule/Spring config file (the interceptor, autoproxy, load-time-weaver etc), change your `weaver` attribute in `aop.xml` to `` and run your app with `-javaagent` pointing to `aspectjweaver-1.6.11.jar`. – Andrei Stefan Aug 20 '14 at 09:18
  • My intercepting pattern is declared only in an "@Around" annotation, so without it it tries to weave the set of classes he can reach (no mule classes) without success. – Tristan Aug 25 '14 at 13:35

2 Answers2

0

Have a look at this example for using Mule and Spring AOP. The example shows how to invoke Around advice for a component, but should be similar for the interceptor.

Gabriel Dimech
  • 699
  • 5
  • 10
0

There is something very important when working with Spring AOP. In spring aop documentation is stated:

Use the simplest thing that can work. Spring AOP is simpler than using full AspectJ as there is no requirement to introduce the AspectJ compiler / weaver into your development and build processes. If you only need to advise the execution of operations on Spring beans, then Spring AOP is the right choice. If you need to advise objects not managed by the Spring container (such as domain objects typically), then you will need to use AspectJ. You will also need to use AspectJ if you wish to advise join points other than simple method executions (for example, field get or set join points, and so on).

So if you want that your AopLoggingInterceptor is invoked for MessageReceiver method calls, that is not going to work because the MessageReceiver object is not managed by the Spring container. The Spring container doesn't "see" this objects.

In other words Spring-AOP cannot add an aspect to anything that is not created by the Spring factory. I found that statement here.

Charmin
  • 711
  • 20
  • 30
  • "If you need to advise objects not managed by the Spring container (such as domain objects typically), then you will need to use AspectJ". And that's precisely what I'm doing, I'm using AspectJ, so I don't see your point. – Tristan Feb 04 '16 at 18:54