3

I am trying to build a custom performance measuring library, which an user can use to measure the performance of any method in an application. My goal is to achieve this thing without changing the existing Java code of the user's application.

The library will use a MethodInterceptor implementation as an advice to measure the performance. The only input from the user will be a pointcut expression of his/her choice, nothing else. And using the provided pointcut expression, my library will be able to intercept the matching method call to measure the performance.

In a nutshell user will only configure like below in spring xml:

<bean id="configBean" class="com.something.PointCutConfigBean">
    <property name="patterns">
        <list>
            <value>"execution(* com.company.CustomerDao.addCustomer(..))"</value>
        </list>
    </property>
</bean>

My question is how can I achieve this in my library? How do I implement the PointCutConfigBean class? I can use spring 2 and aspectj.

I know, same thing can be achieved if the user just write a normal <aop:config and use my advice. But I am just looking for something cooler if at all possible.

Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82
  • Have I look to [this](http://stackoverflow.com/questions/29033690/executing-pointcut-depending-on-environment-variable-or-property) question I asked some time ago, you might find it useful... – Aritz Sep 16 '15 at 18:58
  • @XtremeBiker Thanks. That was good, but I want to provide multiple pointcut expression as a list property not as an annotation. That's where the dynamic requirement comes in and thus the challenge :) – Anindya Chatterjee Sep 16 '15 at 19:43
  • Have you tried the [`aop:pointcut`](http://docs.spring.io/spring-framework/docs/current/spring-framework-reference/html/aop.html#aop-schema-pointcuts) notation? It should do what you want... – Aritz Sep 16 '15 at 19:47
  • @XtremeBiker `aop:config`, `aop:pointcut` is the usual way, that I know. As I mentioned in the question, I am looking for an alternate way with less config overhead for the user. – Anindya Chatterjee Sep 16 '15 at 19:53
  • Ouch! Just haven't pay enough attention to the final part.. :-S So you're looking for something like a `aop:pointcut` manager? – Aritz Sep 16 '15 at 20:02
  • exactly @XtremeBiker.. – Anindya Chatterjee Sep 16 '15 at 20:25
  • Basically the only thing you want your user to prevent from is adding `advice-ref="performance"` to the `` block. That is the only gain that I'm seeing. And instead of using the default mechanism you are trying to be smarter, why? Go with the default I would say saves you documententation, writing and maintaining code. – M. Deinum Sep 17 '15 at 07:45
  • @M.Deinum you are right. But the only reason I want to do this way, is to make the library spring-agnostic. A user will be able to leverage the library without spring also. – Anindya Chatterjee Sep 17 '15 at 09:49
  • 1
    If it is a regular aspect it already should be agnostic. For use without spring you would/could specify the expressions in the `aop.xml` as specified by AspectJ. Also trying to integrate it with spring can be hard and if you do it wrong even break proper functioning software, as it is very easy that way to trigger the creation of a proxy around a proxy when done wrong. – M. Deinum Sep 17 '15 at 09:56

2 Answers2

0

That's a good question !

Why don't you create an annotation, for tagging method or fields if you need, and write an aspect who can get annotations . Class.getAnnotations(), and if the your annotation is present, you run your code ?

fabien t
  • 358
  • 1
  • 9
  • 1
    That would involve changing Java code, I think it's what the OP doesn't want to... – Aritz Sep 16 '15 at 18:53
  • That's true, but a nice approach. Do you try "execution(* package1.*.*(..)) || execution(* package2.*.*(..))" – fabien t Sep 16 '15 at 19:01
  • What you have post should be a comment below the question, IMO. – Aritz Sep 16 '15 at 19:48
  • You are right ! Yes AspectJ work fine with spring 2 i did it on a project, but you need to specify the java agent, and declare the weaving on the java command line. – fabien t Sep 16 '15 at 19:53
0

After reviewing possible ways, I decided the comment of @M.Deinum has the most valuable points against my decision. Finally, I concluded to go with <aop:advisor advice-ref="..." pointcut="execution(.. route only.

If anybody has any other view, please share.

Anindya Chatterjee
  • 5,824
  • 13
  • 58
  • 82