Java Reference
In-Depth Information
public ManagedTaskListener getManagedTaskListener() {
return new CustomManagedTaskListener();
One or more such task classes can be implemented, and then they can be processed via the builder task using
the ManagedExecutorService resource that has been registered with the application server container. The following
servlet makes use of a ManagedExecutorService to coordinate the invocation of two task classes. In this case the task
class names are EmployeeTask and EmployeeTaskTwo .
import java.math.BigDecimal;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.Resource;
import javax.enterprise.concurrent.ManagedExecutorService;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet(name = "BuilderServlet", urlPatterns = {"/builderServlet"})
public class BuilderServlet extends HttpServlet implements Servlet {
// Retrieve our executor instance.
@Resource(name = "concurrent/BuilderExecutor")
ManagedExecutorService mes;
EmployeeInfo employeeInfo;
protected void processRequest(HttpServletRequest req, HttpServletResponse resp) throws
ServletException, IOException {
try {
PrintWriter out = resp.getWriter();
// Create the task instances
Future<EmployeeInfo> empFuture = mes.submit(new EmployeeTask(new BigDecimal(1)));
Future<EmployeeInfo> empFuture2 = mes.submit(new EmployeeTask(new BigDecimal(2)));
// Wait for results
EmployeeInfo empInfo = empFuture.get();
EmployeeInfo empInfo2 = empFuture2.get();
// Process Results
Search WWH ::

Custom Search