This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//type 1 | |
Future<String> f = service.foo(s); | |
String s = f.get(); //blocks the thread, but at least others can run | |
//... do something useful with the string... | |
//type 2 | |
Future<String> f = service.foo(s); | |
while(!f.isDone()){ | |
try { | |
Thread.sleep(100); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class Service2 { | |
@Asynchronous | |
public Future<String> foo(String s) { | |
// simulate some long running process | |
Thread.sleep(5000); | |
s += "<br>Service2: threadId=" + Thread.currentThread().getId(); | |
return new AsyncResult<String>(s); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class Service4 { | |
@Asynchronous | |
public void foo(Callback<String> c) { | |
// simulate some long running process | |
Thread.sleep(5000); | |
c.apply("bar"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebServlet(urlPatterns = { "/AsyncServlet3" }, asyncSupported = true) | |
public class AsyncServlet3 extends HttpServlet { | |
@EJB private Service4 service; | |
protected void doGet(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { | |
... | |
final AsyncContext ctx = request.startAsync(request, response); | |
service.foo(s -> { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Stateless | |
public class Service3 { | |
@Asynchronous | |
public void foo(CompletableFuture<String> cf) { | |
// simulate some long running process | |
Thread.sleep(5000); | |
cf.complete("bar"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@WebServlet(urlPatterns = { "/AsyncServlet2" }, asyncSupported = true) | |
public class AsyncServlet2 extends HttpServlet { | |
@EJB private Service3 service; | |
protected void doGet(HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { | |
final PrintWriter pw = response.getWriter(); | |
pw.write("<html><body>Started publishing with thread " + Thread.currentThread().getId() + "<br>"); | |
response.flushBuffer(); // send back to the browser NOW |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.maxant.async; | |
import java.util.concurrent.Future; | |
import javax.annotation.Resource; | |
import javax.ejb.AsyncResult; | |
import javax.ejb.Asynchronous; | |
import javax.ejb.Stateless; | |
import javax.ejb.TransactionAttribute; | |
import javax.ejb.TransactionAttributeType; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.maxant.async; | |
import java.util.concurrent.Future; | |
import javax.annotation.Resource; | |
import javax.ejb.EJB; | |
import javax.ejb.Stateless; | |
import javax.ejb.TransactionAttribute; | |
import javax.ejb.TransactionAttributeType; | |
import javax.ejb.TransactionManagement; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package ch.maxant.async; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.concurrent.Future; | |
import javax.ejb.EJB; | |
import javax.servlet.ServletException; | |
import javax.servlet.annotation.WebServlet; | |
import javax.servlet.http.HttpServlet; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function persistSales(sales, callback){ | |
if(sales.length === 0 || process.env.skipPersistence) { | |
callback(); //nothing to do, so continue immediately | |
}else{ | |
resources.dbConnection(function(err, connection) { | |
if(err) callback(err); else { | |
logger.info('preparing to persist ' + sales.length + ' sales'); | |
var count = sales.length; | |
_.each(sales, function(sale){ //save them in parallel | |
connection.query( |