AsynchronousJobPool implements ContextResolver<AsynchronousJobPool> { public final AsynchronousJob addJob(Object resource, ResourceMethodDescriptor resourceMethod, Object[] params) throws AsynchronousJobRejectedException { final long expirationDate = System.currentTimeMillis() + MINUTES.toMillis(jobTimeout); final Callable<Object> callable = newCallable(resource, resourceMethod.getMethod(), params); final AsynchronousFuture job = asynchronousFutureFactory.createAsynchronousFuture(callable, expirationDate, resourceMethod, jobListeners); job.setJobURI(getAsynchronousJobUriBuilder(job).build().toString()); final ApplicationContext context = ApplicationContext.getCurrent(); final ContainerRequest request = createRequestCopy(context.getContainerRequest(), context.getSecurityContext()); job.getContext().put("org.everrest.async.request", request); job.getContext().put("org.everrest.async.providers", context.getProviders()); initAsynchronousJobContext(job); final Long jobId = job.getJobId(); jobs.put(jobId, job); try { pool.execute(job); } catch (RejectedExecutionException e) { jobs.remove(jobId); throw new AsynchronousJobRejectedException(e.getMessage()); } LOG.debug("Add asynchronous job, ID: {}", jobId); return job; } AsynchronousJobPool(EverrestConfiguration config); String getAsynchronousServicePath(); int getMaxCacheSize(); int getMaxQueueSize(); int getThreadPoolSize(); int getJobTimeout(); @Override AsynchronousJobPool getContext(Class<?> type); final AsynchronousJob addJob(Object resource, ResourceMethodDescriptor resourceMethod, Object[] params); AsynchronousJob getJob(Long jobId); AsynchronousJob removeJob(Long jobId); List<AsynchronousJob> getAll(); boolean registerListener(AsynchronousJobListener listener); boolean unregisterListener(AsynchronousJobListener listener); @PreDestroy void stop();  }
@Test public void failsAddNewJobIfTooManyJobsInProgress() throws Exception { asynchronousJobPool.addJob(resource, methodDescriptor, new Object[]{}); asynchronousJobPool.addJob(resource, methodDescriptor, new Object[]{}); thrown.expect(AsynchronousJobRejectedException.class); thrown.expectMessage("Can't accept new asynchronous request. Too many asynchronous jobs in progress"); asynchronousJobPool.addJob(resource, methodDescriptor, new Object[]{}); }