Skip to content

Commit ec708ee

Browse files
committed
Merge branch 'master' of github.com:javaee-samples/javaee7-samples
2 parents ad0a268 + b243e10 commit ec708ee

File tree

36 files changed

+931
-693
lines changed

36 files changed

+931
-693
lines changed
Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
package org.javaee7.ejb.async;
22

3-
import java.util.concurrent.Future;
4-
import java.util.logging.Level;
5-
import java.util.logging.Logger;
63
import javax.ejb.AsyncResult;
74
import javax.ejb.Asynchronous;
85
import javax.ejb.Stateless;
6+
import java.util.concurrent.Future;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
99

1010
/**
1111
* @author Arun Gupta
@@ -14,17 +14,16 @@
1414
@Asynchronous
1515
public class MyAsyncBeanClassLevel {
1616

17+
public static final long AWAIT = 3000;
18+
1719
public Future<Integer> addNumbers(int n1, int n2) {
1820
try {
1921
// simulating a long running query
20-
Thread.sleep(3000);
22+
Thread.sleep(AWAIT);
2123
} catch (InterruptedException ex) {
2224
Logger.getLogger(MyAsyncBeanClassLevel.class.getName()).log(Level.SEVERE, null, ex);
2325
}
2426
return new AsyncResult(n1 + n2);
2527
}
26-
27-
public void doSomething(String what) {
28-
System.out.println("Just did " + what);
29-
}
28+
3029
}
Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,29 @@
11
package org.javaee7.ejb.async;
22

3-
import java.util.concurrent.Future;
4-
import java.util.logging.Level;
5-
import java.util.logging.Logger;
63
import javax.ejb.AsyncResult;
74
import javax.ejb.Asynchronous;
85
import javax.ejb.Stateless;
6+
import java.util.concurrent.Future;
7+
import java.util.logging.Level;
8+
import java.util.logging.Logger;
99

1010
/**
1111
* @author Arun Gupta
1212
*/
1313
@Stateless
1414
public class MyAsyncBeanMethodLevel {
1515

16+
public static final long AWAIT = 3000;
17+
1618
@Asynchronous
1719
public Future<Integer> addNumbers(int n1, int n2) {
1820
try {
1921
// simulating a long running query
20-
Thread.sleep(3000);
22+
Thread.sleep(AWAIT);
2123
} catch (InterruptedException ex) {
2224
Logger.getLogger(MyAsyncBeanMethodLevel.class.getName()).log(Level.SEVERE, null, ex);
2325
}
2426
return new AsyncResult(n1 + n2);
2527
}
26-
27-
@Asynchronous
28-
public void doSomething(String what) {
29-
System.out.println("Just did " + what);
30-
}
28+
3129
}

ejb/async-ejb/src/main/java/org/javaee7/ejb/async/TestServlet.java

Lines changed: 0 additions & 127 deletions
This file was deleted.

ejb/async-ejb/src/main/webapp/index.jsp

Lines changed: 0 additions & 56 deletions
This file was deleted.

ejb/async-ejb/src/test/java/org/javaee7/ejb/async/AccountSessionBeanTest.java

Lines changed: 0 additions & 53 deletions
This file was deleted.
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
* To change this license header, choose License Headers in Project Properties.
3+
* To change this template file, choose Tools | Templates
4+
* and open the template in the editor.
5+
*/
6+
7+
package org.javaee7.ejb.async;
8+
9+
import org.jboss.arquillian.container.test.api.Deployment;
10+
import org.jboss.arquillian.junit.Arquillian;
11+
import org.jboss.shrinkwrap.api.ShrinkWrap;
12+
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
13+
import org.jboss.shrinkwrap.api.spec.WebArchive;
14+
import org.jboss.shrinkwrap.resolver.api.maven.Maven;
15+
import org.junit.Test;
16+
import org.junit.runner.RunWith;
17+
18+
import javax.inject.Inject;
19+
import java.io.File;
20+
import java.util.concurrent.Callable;
21+
import java.util.concurrent.ExecutionException;
22+
import java.util.concurrent.Future;
23+
24+
import static com.jayway.awaitility.Awaitility.*;
25+
import static org.hamcrest.MatcherAssert.*;
26+
import static org.hamcrest.Matchers.*;
27+
28+
/**
29+
* @author Jakub Marchwicki
30+
*/
31+
@RunWith(Arquillian.class)
32+
public class AsyncClassBeanTest {
33+
34+
@Inject
35+
MyAsyncBeanClassLevel bean;
36+
37+
@Deployment
38+
public static WebArchive createDeployment() {
39+
File[] jars = Maven.resolver().loadPomFromFile("pom.xml")
40+
.resolve("com.jayway.awaitility:awaitility")
41+
.withTransitivity().asFile();
42+
43+
return ShrinkWrap.create(WebArchive.class)
44+
.addAsLibraries(jars)
45+
.addClasses(MyAsyncBeanClassLevel.class)
46+
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml");
47+
}
48+
49+
@Test
50+
public void should_return_async_sum() throws ExecutionException, InterruptedException {
51+
final Integer numberOne = 5;
52+
final Integer numberTwo = 10;
53+
54+
55+
long start = System.currentTimeMillis();
56+
final Future<Integer> resultFuture = bean.addNumbers(numberOne, numberTwo);
57+
58+
assertThat(resultFuture.isDone(), is(equalTo(false)));
59+
assertThat(System.currentTimeMillis() - start, is(lessThan(MyAsyncBeanMethodLevel.AWAIT)));
60+
61+
await().until(new Callable<Boolean>() {
62+
@Override
63+
public Boolean call() throws Exception {
64+
return resultFuture.isDone();
65+
}
66+
});
67+
68+
assertThat(resultFuture.get(), is(equalTo(numberOne + numberTwo)));
69+
}
70+
71+
}

0 commit comments

Comments
 (0)