Search in sources :

Example 1 with ManagedThreadFactory

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) {
            }
        }
    });
}
Also used : TimeoutHandler(javax.ws.rs.container.TimeoutHandler) AsyncResponse(javax.ws.rs.container.AsyncResponse) InitialContext(javax.naming.InitialContext) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory) GET(javax.ws.rs.GET)

Example 2 with ManagedThreadFactory

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);
}
Also used : ManagedThreadFactoryImpl(org.apache.openejb.threads.impl.ManagedThreadFactoryImpl) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory)

Example 3 with ManagedThreadFactory

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>");
    }
}
Also used : NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) PrintWriter(java.io.PrintWriter) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory)

Example 4 with ManagedThreadFactory

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);
    }
}
Also used : NamingException(javax.naming.NamingException) InitialContext(javax.naming.InitialContext) ManagedThreadFactory(javax.enterprise.concurrent.ManagedThreadFactory)

Aggregations

ManagedThreadFactory (javax.enterprise.concurrent.ManagedThreadFactory)4 InitialContext (javax.naming.InitialContext)3 NamingException (javax.naming.NamingException)2 PrintWriter (java.io.PrintWriter)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 GET (javax.ws.rs.GET)1 AsyncResponse (javax.ws.rs.container.AsyncResponse)1 TimeoutHandler (javax.ws.rs.container.TimeoutHandler)1 ManagedThreadFactoryImpl (org.apache.openejb.threads.impl.ManagedThreadFactoryImpl)1