use of javax.enterprise.concurrent.ManagedThreadFactory in project javaee7-samples by javaee-samples.
the class MyResource method getList.
// @Resource(name = "DefaultManagedThreadFactory")
// ManagedThreadFactory threadFactory;
@GET
public void getList(@Suspended final AsyncResponse ar) throws NamingException {
ar.setTimeoutHandler(new TimeoutHandler() {
@Override
public void handleTimeout(AsyncResponse ar) {
ar.resume("Operation timed out");
}
});
ar.setTimeout(4000, TimeUnit.MILLISECONDS);
ar.register(new MyCompletionCallback());
ar.register(new MyConnectionCallback());
ManagedThreadFactory threadFactory = (ManagedThreadFactory) new InitialContext().lookup("java:comp/DefaultManagedThreadFactory");
Executors.newSingleThreadExecutor(threadFactory).submit(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(3000);
ar.resume(response[0]);
} catch (InterruptedException ex) {
}
}
});
}
use of javax.enterprise.concurrent.ManagedThreadFactory in project tomee by apache.
the class ManagedExecutorServiceImplFactory method createExecutorService.
private ExecutorService createExecutorService() {
final BlockingQueue<Runnable> blockingQueue;
if (queue <= 0) {
blockingQueue = new LinkedBlockingQueue<>();
} else {
blockingQueue = new ArrayBlockingQueue<>(queue);
}
ManagedThreadFactory managedThreadFactory;
try {
managedThreadFactory = "org.apache.openejb.threads.impl.ManagedThreadFactoryImpl".equals(threadFactory) ? new ManagedThreadFactoryImpl() : ThreadFactories.findThreadFactory(threadFactory);
} catch (final Exception e) {
Logger.getInstance(LogCategory.OPENEJB, ManagedExecutorServiceImplFactory.class).warning("Can't create configured thread factory: " + threadFactory, e);
managedThreadFactory = new ManagedThreadFactoryImpl();
}
return new ThreadPoolExecutor(core, max, keepAlive.getTime(), keepAlive.getUnit(), blockingQueue, managedThreadFactory, CURejectHandler.INSTANCE);
}
use of javax.enterprise.concurrent.ManagedThreadFactory in project javaee7-samples by javaee-samples.
the class TestJNDIServlet method processRequest.
/**
* Processes requests for both HTTP
* <code>GET</code> and
* <code>POST</code> methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
out.println("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>Servlet TestJNDIServlet</title>");
out.println("</head>");
out.println("<body>");
out.println("<h1>Getting ManagedThreadFactory using JNDI lookup</h1>");
try {
InitialContext ctx = new InitialContext();
ManagedThreadFactory factory = (ManagedThreadFactory) ctx.lookup("java:comp/DefaultManagedThreadFactory");
// ManagedExecutorService executor = (ManagedExecutorService) ctx.lookup("concurrent/myExecutor");
out.println("Getting ManageableThread<br>");
Thread thread = factory.newThread(new MyTask(2));
out.println("Starting thread<br>");
thread.start();
out.println("Thread started<br><br>");
} catch (NamingException ex) {
Logger.getLogger(TestResourceServlet.class.getName()).log(Level.SEVERE, null, ex);
}
out.println("all tasks submitted<br/><br/>");
out.println("Check server.log for output from the task.");
out.println("</body>");
out.println("</html>");
}
}
use of javax.enterprise.concurrent.ManagedThreadFactory in project javaee7-samples by javaee-samples.
the class MyTaskTest method testJNDILookup.
/**
* Test of run method, of class MyTask.
*
* using JNDI lookup
*/
// @Test
public void testJNDILookup() {
try {
InitialContext ctx = new InitialContext();
// ManagedExecutorService executor = (ManagedExecutorService) ctx.lookup("concurrent/myExecutor");
ManagedThreadFactory myFactory = (ManagedThreadFactory) ctx.lookup("java:comp/DefaultManagedThreadFactory");
assertNotNull(myFactory);
Thread thread = myFactory.newThread(new MyTask(1));
assertNotNull(thread);
thread.start();
} catch (NamingException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
}
Aggregations