CDI Interceptor not being executed

810 views
Skip to first unread message

Jonathan Vila

unread,
Jun 1, 2021, 2:50:39 PM6/1/21
to Quarkus Development mailing list
Hi

I need to create an interceptor for my custom annotation, and for every method (setter) annotated it will change the value being passed.

My annotation :

@Inherited
@InterceptorBinding
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Translate {}

My Interceptor : 

@Translate
@Interceptor
public class TranslateProcessor {
    @AroundInvoke
    public Object invoke(InvocationContext context) throws Exception {
        // here I should modify the value being passed to the method
        return context.proceed();
    }
}

My class : 

public class MyDto {
    private String title;

    @Translate
    public void setTitle(String title) {
        this.title = title;
    }
}

But basically I have the problem that the code in the interceptor is never called. 
Is there anything I'm missing to enable the interceptor ? I assume Quarkus doesn't need the beans.xml file, and also I don't see the need to set the priority to the interceptor.

Jonathan Vila
-- 
Barcelona Java Users Group Leader


Martin Kouba

unread,
Jun 1, 2021, 3:04:28 PM6/1/21
to jona...@barcelonajug.org, Quarkus Development mailing list
Hi Jonathan,

comments inline...

On 01. 06. 21 20:50, Jonathan Vila wrote:
> Hi
>
> I need to create an interceptor for my custom annotation, and for every
> method (setter) annotated it will change the value being passed.
>
> *My annotation :*
>
> @Inherited
> @InterceptorBinding
> @Target({ElementType.METHOD, ElementType.TYPE})
> @Retention(RetentionPolicy.RUNTIME)
> public @interface Translate {}
>
> *My Interceptor : *
>
> @Translate
> @Interceptor
> public class TranslateProcessor {
>     @AroundInvoke
>     public Object invoke(InvocationContext context) throws Exception {
>         // here I should modify the value being passed to the method
>         return context.proceed();
>     }
> }
>
> *My class : *
>
> public class MyDto {
>     private String title;
>
>     @Translate
>     public void setTitle(String title) {
>         this.title = title;
>     }
> }
>
> But basically I have the problem that the code in the interceptor is
> never called.
> Is there anything I'm missing to enable the interceptor ? I assume
> Quarkus doesn't need the beans.xml file,

It depends. The descriptor is not needed for application classes, i.e.
if the interceptor is located in src/main/java you don't need to do
anything. However, if you place the interceptor in a dependency jar then
it has to contain a beans.xml (content is ignored) or a Jandex index
(META-INF/jandex.idx). If you can’t modify the dependency, you can still
index it by adding quarkus.index-dependency entries to your
application.properties.

See also https://quarkus.io/guides/cdi-reference#bean_discovery

> and also I don't see the need
> to set the priority to the interceptor.

If you don't set the priority the value of 0 is used.

>
> Jonathan Vila
> --
> Barcelona Java Users Group Leader
> <https://mailtrack.io/trace/link/08d3f1e6c014f590aff2afd17e958e88db6fb281?url=http%3A%2F%2Fbarcelonajug.org%2F&userId=3660166&signature=73316107bc54e7fa>
> <https://mailtrack.io/trace/link/ad6f0f889ec8dcbb4f25aaa5847e842158a4d444?url=http%3A%2F%2Fwww.meetup.com%2FBarcelonaJUG&userId=3660166&signature=a96f8c2b0a28f696><https://mailtrack.io/trace/link/5aa57dd59f5505843f93dbbaf0ce91bd51294eed?url=http%3A%2F%2Fbit.ly%2FBarcelonaJUG&userId=3660166&signature=418b74d5262398f5><https://mailtrack.io/trace/link/dcc88f5ec352179bac960039049488924d6b51e8?url=http%3A%2F%2Ftwitter.com%2FBarcelonaJUG&userId=3660166&signature=6cce41d2c6a11a81><https://mailtrack.io/trace/link/57f3a2e0f9d0098941123c31b6af1e2fd3753977?url=http%3A%2F%2Fwww.facebook.com%2FBarcelonaJUG&userId=3660166&signature=5903945ae91f3672><https://mailtrack.io/trace/link/a3755d537ba0595c80c50694091dcbb6b6525398?url=http%3A%2F%2Fj.mp%2FbarcelonaJUG&userId=3660166&signature=c27d3af352c53a28><https://mailtrack.io/trace/link/edd4592231d3bcadfdf7cb9a30534bbcfaf08f9c?url=http%3A%2F%2Fbit.ly%2FBCNjug&userId=3660166&signature=0141c50419a1d093>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Quarkus Development mailing list" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev...@googlegroups.com>.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/quarkus-dev/CABNTrS_yachzFdbmai0t2tdBFt5HXxMuueOYEpBd2OF_X8NHiw%40mail.gmail.com
> <https://groups.google.com/d/msgid/quarkus-dev/CABNTrS_yachzFdbmai0t2tdBFt5HXxMuueOYEpBd2OF_X8NHiw%40mail.gmail.com?utm_medium=email&utm_source=footer>.

--
Martin Kouba
Software Engineer
Red Hat, Czech Republic

Jonathan Vila

unread,
Jun 1, 2021, 3:23:34 PM6/1/21
to Martin Kouba, Quarkus Development mailing list
Thank you Martin

The interceptor and the annotation to intercept are in the same package and everything in the same app. 

Martin Kouba

unread,
Jun 1, 2021, 3:41:06 PM6/1/21
to Jonathan Vila, Quarkus Development mailing list
Hm, in that case we'll probably need a reproducer.

One thing I've just noticed is that you're trying to associcate the
interceptor with a "DTO" class. Is MyDto actually a CDI bean? It does
not declare any scope. Note that inteceptors are only applied to
business methods of CDI beans...

On 01. 06. 21 21:23, Jonathan Vila wrote:
> Thank you Martin
>
> The interceptor and the annotation to intercept are in the same package
> and everything in the same app.
>
> El mar., 1 jun. 2021 21:04, Martin Kouba <mko...@redhat.com
> <mailto:mko...@redhat.com>> escribió:
> <https://mailtrack.io/trace/link/08d3f1e6c014f590aff2afd17e958e88db6fb281?url=http%3A%2F%2Fbarcelonajug.org%2F&userId=3660166&signature=73316107bc54e7fa>>
> >
> <https://mailtrack.io/trace/link/ad6f0f889ec8dcbb4f25aaa5847e842158a4d444?url=http%3A%2F%2Fwww.meetup.com%2FBarcelonaJUG&userId=3660166&signature=a96f8c2b0a28f696
> <https://mailtrack.io/trace/link/ad6f0f889ec8dcbb4f25aaa5847e842158a4d444?url=http%3A%2F%2Fwww.meetup.com%2FBarcelonaJUG&userId=3660166&signature=a96f8c2b0a28f696>><https://mailtrack.io/trace/link/5aa57dd59f5505843f93dbbaf0ce91bd51294eed?url=http%3A%2F%2Fbit.ly%2FBarcelonaJUG&userId=3660166&signature=418b74d5262398f5
> <https://mailtrack.io/trace/link/5aa57dd59f5505843f93dbbaf0ce91bd51294eed?url=http%3A%2F%2Fbit.ly%2FBarcelonaJUG&userId=3660166&signature=418b74d5262398f5>><https://mailtrack.io/trace/link/dcc88f5ec352179bac960039049488924d6b51e8?url=http%3A%2F%2Ftwitter.com%2FBarcelonaJUG&userId=3660166&signature=6cce41d2c6a11a81
> <https://mailtrack.io/trace/link/dcc88f5ec352179bac960039049488924d6b51e8?url=http%3A%2F%2Ftwitter.com%2FBarcelonaJUG&userId=3660166&signature=6cce41d2c6a11a81>><https://mailtrack.io/trace/link/57f3a2e0f9d0098941123c31b6af1e2fd3753977?url=http%3A%2F%2Fwww.facebook.com%2FBarcelonaJUG&userId=3660166&signature=5903945ae91f3672
> <https://mailtrack.io/trace/link/57f3a2e0f9d0098941123c31b6af1e2fd3753977?url=http%3A%2F%2Fwww.facebook.com%2FBarcelonaJUG&userId=3660166&signature=5903945ae91f3672>><https://mailtrack.io/trace/link/a3755d537ba0595c80c50694091dcbb6b6525398?url=http%3A%2F%2Fj.mp%2FbarcelonaJUG&userId=3660166&signature=c27d3af352c53a28
> <https://mailtrack.io/trace/link/a3755d537ba0595c80c50694091dcbb6b6525398?url=http%3A%2F%2Fj.mp%2FbarcelonaJUG&userId=3660166&signature=c27d3af352c53a28>><https://mailtrack.io/trace/link/edd4592231d3bcadfdf7cb9a30534bbcfaf08f9c?url=http%3A%2F%2Fbit.ly%2FBCNjug&userId=3660166&signature=0141c50419a1d093
> <https://mailtrack.io/trace/link/edd4592231d3bcadfdf7cb9a30534bbcfaf08f9c?url=http%3A%2F%2Fbit.ly%2FBCNjug&userId=3660166&signature=0141c50419a1d093>>
> >
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Quarkus Development mailing list" group.
> > To unsubscribe from this group and stop receiving emails from it,
> send
> > an email to quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev%2Bunsu...@googlegroups.com>
> > <mailto:quarkus-dev...@googlegroups.com
> <mailto:quarkus-dev%2Bunsu...@googlegroups.com>>.
> <https://groups.google.com/d/msgid/quarkus-dev/CABNTrS_yachzFdbmai0t2tdBFt5HXxMuueOYEpBd2OF_X8NHiw%40mail.gmail.com?utm_medium=email&utm_source=footer

Jonathan Vila

unread,
Jun 1, 2021, 3:55:35 PM6/1/21
to Martin Kouba, Quarkus Development mailing list
That totally makes sense.... Thats totally my fault

But then I think this approach is not what I need 😞
I wanted to make a non intrusive translator service annotating the methods in the dtos that are the ones used in the output of the resource

Thank you a lot Martin 

Kristijan Rusu

unread,
Jun 1, 2021, 4:49:20 PM6/1/21
to Quarkus Development mailing list
Hello Jonathan. You can make this with a filter, or better yet with JAX-RS WriterInterceptor. Then you can annotate which DTOs are translatable, so translate only them.

Jonathan Vila

unread,
Jun 1, 2021, 4:57:43 PM6/1/21
to kristij...@gmail.com, Quarkus Development mailing list
Ohhh Kristijan, that sounds great.
I will explore that approach 

Thank you very much for your suggestion 

To unsubscribe from this group and stop receiving emails from it, send an email to quarkus-dev...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/quarkus-dev/17bd4a0f-a119-45d7-a298-fcc7042c50e7n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages