Adam Bien's Weblog

CDI Events: Obtaining The InjectionPoint From EventMetaData

Starting with Java EE 7, a CDI event:


import javax.enterprise.event.Event;
import javax.inject.Inject;

public class MetaDataResource {

    Event<String> event;

    public void fire() {"hey duke");


is delivered with injectable javax.enterprise.inject.spi.EventMetadata:

import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.EventMetadata;
import javax.enterprise.inject.spi.InjectionPoint;

public class Listener {

    public void onArrival(@Observes String event, EventMetadata metaData) {
        InjectionPoint ip = metaData.getInjectionPoint();
        Class clazz = ip.getMember().getDeclaringClass();
        String fieldName = ip.getMember().getName();
        System.out.println(clazz.getName() + "-> " + fieldName);


The code creates the following output:> event

See you at Java EE Workshops at Munich Airport, Terminal 2 or Virtual Dedicated Workshops / consulting. Is Munich's airport too far? Learn from home:

NEW workshop: Building HTML 5 Applications Without Frameworks, Full week in December 2016: from Java EE 7 Bootstrap, Effective, Architectures over Testing to Microservices
On demand workshops: Java EE 7: Bootstrap, Effective, Testing and Microservices available for streaming.


A book about rethinking Java EE Patterns


I hoped the EventMetaData would provide the calling method fire() instead of the injection point event field.

I liked that multiple @Observes all get called in one fire().

Wondering if @Observes method can be executed in a separate REQUIRES_NEW transaction. Then it can db persist new audit log entity, even when the first transaction is rollback.

Question on the side; if a @Stateless gets injected with a @Stateful, what creates and destroys the stateful? Is a new @Statefull injected at each call to the stateless? Is the stateless calling a @Remove method. How to let the @Statefull having a REQUIRES_NEW? Is this architecturally wrong?

Posted by Rene on January 02, 2016 at 07:28 PM CET #

Post a Comment:
  • HTML Syntax: NOT allowed
Online Workshops
...the last 150 posts
...the last 10 comments