sdk-java/cdi
Francesco Guardiani cd913f368f
Changed version to 2.0.0-SNAPSHOT (#127)
* Changed version to 2.0.0-SNAPSHOT

Changed the README for the V2 to specify the sdk is currently under development

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Fixed poms

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>

* Fixed json-jackson pom

Signed-off-by: Francesco Guardiani <francescoguard@gmail.com>
2020-04-22 16:59:41 +02:00
..
src Import the new v0.2 impl for CloudEvent 2019-09-03 15:33:48 -03:00
README.md 🔖 Set versions to 1.3.0 2020-03-04 20:48:47 -03:00
pom.xml Changed version to 2.0.0-SNAPSHOT (#127) 2020-04-22 16:59:41 +02:00

README.md

CDI Integration

Firing CloudEvents using CDI

For Maven based projects, use the following to configure the CloudEvents CDI library:

<dependency>
    <groupId>io.cloudevents</groupId>
    <artifactId>cdi</artifactId>
    <version>1.3.0</version>
</dependency>

In Enterprise Java applications, implemented with Jakarta EE or the Eclipse MicroProfile, it's trivial to combine this CloudEvents API with CDI. Application developers can now fire a CloudEvent for further processing inside of the application:

import io.cloudevents.v02.CloudEventBuilder;
import io.cloudevents.v02.CloudEventImpl;
import io.cloudevents.cdi.EventTypeQualifier;

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

import java.net.URI;
import java.util.UUID;

public class Router {

    @Inject
    private Event<CloudEventImpl<MyCustomEvent>> cloudEvent;

    public void routeMe() throws Exception {

        final CloudEventImpl<MyCustomEvent> event =
            CloudEventBuilder.<MyCustomEvent>builder()
                .withType("Cloud.Storage.Item.Created")
                .withSource(new URI("/trigger"))
                .withId(UUID.randomUUID().toString())
                .withData(new MyCustomEvent(...))
                .build();

        cloudEvent.select(
                new EventTypeQualifier("Cloud.Storage.Item.Created"))
                .fire(event);
    }
}

The method above creates a CloudEvent object, and uses the injected CDI Event implementation, where a select() is performed to fire a qualified event, for all interested parties.

Receiving CloudEvents with CDI

If other parts of the application are interested in the Cloud.Storage.Item.Created event, it needs a matching @Observes annotation:

public void receiveCloudEvent(
  @Observes @EventType(name = "My.Cloud.Event.Type") CloudEvent cloudEvent) {
  // handle the event
}                                                                                       

The application now is able to work with the observed CloudEvent object.