Adam Bien's Weblog

CDI Events: Obtaining The InjectionPoint From EventMetaData

Starting with Java EE 7, a CDI event:


package com.airhacks.events;

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

public class MetaDataResource {

    @Inject
    Event<String> event;

    public void fire() {
        event.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:


 com.airhacks.events.MetaDataResource-> 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: airhacks.io.


NEW workshop: Building HTML 5 Applications With React.js
On demand workshops: Java EE 7 Bootstrap, Effective Java EE 7, Java EE 7 Testing and NEW: Java EE 7 Microservices are available for streaming

Newsletter: airhacksnews.com

A book about rethinking Java EE Patterns

Comments:

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
realworldpatterns.com
Online Workshops
...the last 150 posts
...the last 10 comments
License