Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where is @AroundInvoke interceptor invoked?

I'm doing some test examples with java, and I come up with an example that uses @AroundInvoke. The question is that I don't know exactly where is the method invoked.

This test makes an invocation where it calls post() method, but I don't really know how that works (Using Interceptors explanation).

@Test
public void crudtest() {
    JsonObjectBuilder todoBuilder = Json.createObjectBuilder();
    JsonObject todoToCreate = todoBuilder.
            add("caption", "implement").
            add("priority", 10).
            build();

    //The next post execute, invoke the method
    Response postResponse = this.provider.target().request().
            post(Entity.json(todoToCreate));
}

The order of invocation is BoundaryLogger and then MonitorSink

BoundaryLogger.java

...
public class BoundaryLogger {

    @Inject
    Event<CallEvent> monitoring;

    @AroundInvoke
    public Object logCall(InvocationContext ic) throws Exception {
        long start = System.currentTimeMillis();
        try {
            return ic.proceed();
        } finally {
            long duration = System.currentTimeMillis() - start;
            monitoring.fire(new CallEvent(ic.getMethod().getName(), duration));
        }
    }
}

MonitorSink

@Singleton
@ConcurrencyManagement(ConcurrencyManagementType.BEAN)
public class MonitorSink {

    @Inject
    LogSink LOG;

    public void onCallEvent(@Observes CallEvent event){
        LOG.log(event.toString());
    }
}
like image 315
Joe Avatar asked Sep 04 '15 16:09

Joe


People also ask

How interceptors are declared?

An interceptor is defined using one of the interceptor metadata annotations listed in Table 50-1 within the target class, or in a separate interceptor class. The following code declares an @AroundTimeout interceptor method within a target class. @Stateless public class TimerBean { ...

What is interceptor in ejb?

An interceptor is a method that is automatically called when the business methods of an Enterprise JavaBeans (EJB) are invoked or lifecycle events of an EJB occur. There are three kinds of interceptor methods: business method interceptors, timeout method interceptors (which are new in EJB3.

What is the role of AroundInvoke method?

An AroundInvoke method can invoke any component or resource that the method it is intercepting can invoke. AroundInvoke method invocations occur within the same transaction and security context as the method on which they are interposing.

What is @AroundInvoke?

Annotation Type AroundInvokeDefines an interceptor method that interposes on business methods. May be applied to any non-final, non-static method with a single parameter of type InvocationContext and return type Object of the target class (or superclass) or of any interceptor class.


1 Answers

I figured out, by doing an other interceptor example.

@AroundInvoke just define an interceptor that it will be invoked by the class that has @Interceptor(name_class.class).

In my case, this was the code I was missing to look at it.

ToDoManager.java

@Stateless
@Interceptors(BoundaryLogger.class)
public class ToDoManager {

    @PersistenceContext
    EntityManager em;

    public ToDo findById(long id) {
        return this.em.find(ToDo.class,id);
    }

    public void delete(long id) {
        try {
            ToDo reference = this.em.getReference(ToDo.class, id);
            this.em.remove(reference);
        } catch (EntityNotFoundException e) {
            //we want to remove it...
        }
    }

    public List<ToDo> all() {
        return this.em.createNamedQuery(ToDo.findAll, ToDo.class).getResultList();
    }

    public ToDo save(ToDo todo) {
        return this.em.merge(todo);
    }

    public ToDo updateStatus(long id, boolean done) {
        ToDo todo = this.findById(id);
        if(todo == null){
            return null;
        }
        todo.setDone(done);
        return todo;
    }

}

The @AroundInvoke annotation is used to designate interceptor methods for managed object methods.

I hope, that this could help someone else!

like image 91
Joe Avatar answered Sep 20 '22 10:09

Joe