Skip to content
Merged
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
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
package org.javaee7.ejb.async;

import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Arun Gupta
Expand All @@ -14,17 +14,16 @@
@Asynchronous
public class MyAsyncBeanClassLevel {

public static final long AWAIT = 3000;

public Future<Integer> addNumbers(int n1, int n2) {
try {
// simulating a long running query
Thread.sleep(3000);
Thread.sleep(AWAIT);
} catch (InterruptedException ex) {
Logger.getLogger(MyAsyncBeanClassLevel.class.getName()).log(Level.SEVERE, null, ex);
}
return new AsyncResult(n1 + n2);
}

public void doSomething(String what) {
System.out.println("Just did " + what);
}

}
Original file line number Diff line number Diff line change
@@ -1,31 +1,29 @@
package org.javaee7.ejb.async;

import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.AsyncResult;
import javax.ejb.Asynchronous;
import javax.ejb.Stateless;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* @author Arun Gupta
*/
@Stateless
public class MyAsyncBeanMethodLevel {

public static final long AWAIT = 3000;

@Asynchronous
public Future<Integer> addNumbers(int n1, int n2) {
try {
// simulating a long running query
Thread.sleep(3000);
Thread.sleep(AWAIT);
} catch (InterruptedException ex) {
Logger.getLogger(MyAsyncBeanMethodLevel.class.getName()).log(Level.SEVERE, null, ex);
}
return new AsyncResult(n1 + n2);
}

@Asynchronous
public void doSomething(String what) {
System.out.println("Just did " + what);
}

}
127 changes: 0 additions & 127 deletions ejb/async-ejb/src/main/java/org/javaee7/ejb/async/TestServlet.java

This file was deleted.

56 changes: 0 additions & 56 deletions ejb/async-ejb/src/main/webapp/index.jsp

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/

package org.javaee7.ejb.async;

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.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
import org.junit.Test;
import org.junit.runner.RunWith;

import javax.inject.Inject;
import java.io.File;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

import static com.jayway.awaitility.Awaitility.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;

/**
* @author Jakub Marchwicki
*/
@RunWith(Arquillian.class)
public class AsyncClassBeanTest {

@Inject
MyAsyncBeanClassLevel bean;

@Deployment
public static WebArchive createDeployment() {
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
.resolve("com.jayway.awaitility:awaitility")
.withTransitivity().asFile();

return ShrinkWrap.create(WebArchive.class)
.addAsLibraries(jars)
.addClasses(MyAsyncBeanClassLevel.class)
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
}

@Test
public void should_return_async_sum() throws ExecutionException, InterruptedException {
final Integer numberOne = 5;
final Integer numberTwo = 10;


long start = System.currentTimeMillis();
final Future<Integer> resultFuture = bean.addNumbers(numberOne, numberTwo);

assertThat(resultFuture.isDone(), is(equalTo(false)));
assertThat(System.currentTimeMillis() - start, is(lessThan(MyAsyncBeanMethodLevel.AWAIT)));

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return resultFuture.isDone();
}
});

assertThat(resultFuture.get(), is(equalTo(numberOne + numberTwo)));
}

}
Loading