Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion cdi/events-async/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,12 @@

<artifactId>events-async</artifactId>
<name>Java EE 8 Samples: CDI - Events Async</name>

<dependencies>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>3.1.6</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) [2019] Payara Foundation and/or its affiliates.
*/

package org.javaee8.cdi.events.async.startup;

import java.io.Serializable;

public class MyEvent implements Serializable {

private String source;

public MyEvent(String source) {
this.source = source;
}

public String getSource() {
return source;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Copyright (c) [2019] Payara Foundation and/or its affiliates.
*/

package org.javaee8.cdi.events.async.startup;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.enterprise.event.Observes;
import javax.enterprise.event.ObservesAsync;
import java.util.logging.Logger;

import static java.util.logging.Level.INFO;

@Singleton
public class ObserverAtStartup {

private static final Logger LOG = Logger.getLogger(ObserverAtStartup.class.getName());

private boolean received;

@PostConstruct
public void init() {
LOG.info("Initiating bean public class ObserverAtStartup");
}

public void trigger(@ObservesAsync MyEvent evt) {
LOG.log(INFO, "ObserverAtStartup received event from {0}", evt.getSource());
this.received = true;
}

public boolean isReceived() {
return received;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) [2019] Payara Foundation and/or its affiliates.
*/

package org.javaee8.cdi.events.async.startup;

import javax.annotation.PostConstruct;
import javax.ejb.Singleton;
import javax.ejb.Startup;
import javax.enterprise.event.Event;
import javax.inject.Inject;
import java.util.logging.Level;
import java.util.logging.Logger;

@Startup
@Singleton
public class ProducerAtStartup {

private static final Logger LOG = Logger.getLogger(ProducerAtStartup.class.getName());

@Inject
Event<MyEvent> evt;

@PostConstruct
public void init() {
LOG.info("ProducerAtStartup init");
fireEvent();
}

public void fireEvent(){
evt.fireAsync(new MyEvent("ProducerAtStartup")).whenComplete((e, t) -> {
if (t != null) {
LOG.log(Level.SEVERE, "Event dispatch failed", t);
} else {
LOG.info("Event dispatch succeeded");
}
});
LOG.info("fireAsync done");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) [2019] Payara Foundation and/or its affiliates.
*/

package org.javaee8.cdi.events.async.startup;

import org.awaitility.Awaitility;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;

import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;

@RunWith(Arquillian.class)
public class AsyncAtStartupTest {

@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class)
.addPackage(ProducerAtStartup.class.getPackage())
.addPackages(true, Awaitility.class.getPackage());
}

@Inject
ObserverAtStartup observer;

@Test
public void startupEventIsEventuallyObserved() {
await().atMost(5, TimeUnit.SECONDS).until(observer::isReceived);
}
}