2

This may be bit old question. I am confused with the ExecutorService work in Jboss environment. I used some sample code, where i am submitting the task with ExecutorService and after everything is done i am shutdown the executor.

Problem i am facing is after submitting one request, i am getting below exception for subsequent request. Caused by: java.util.concurrent.RejectedExecutionException: Task java.util.concurrent.FutureTask@518ad6a2 rejected from java.util.concurrent.ThreadPoolExecutor@72114f80[Shutting down, pool size = 1, active threads = 1, queued tasks = 0, completed tasks = 0]

ExecutorService executorService = Executors.newFixedThreadPool(3);

@POST
@Path("/request")
public Response checkAsync(final MultiMedia multiMedia) {
    final Random rand = new Random();
    final String random = String.valueOf(rand.nextInt(50) + 1);

    multiMediaJobs.put(random, multiMedia);

    final String jobId = "{ 'jobId' : " + random + "}";

    executorService.submit(new Runnable() {
            @Override
            public void run() {
                boolean result = veryExpensiveOperation(jobId);
                if (result) {
                    try {
                        MultiMedia multiMedia = (MultiMedia) multiMediaJobs.get(random);
                        multiMedia.getMediadata().getMetadata()
                                  .setAssetId(random);

                        final String uri = multiMedia.getCallback().getUri()+multiMedia.getCallback().getResource();

                        RestTemplate restTemplate = new RestTemplate();
                        String code = restTemplate.postForObject(uri,
                                multiMedia, String.class);

                        System.out.println(code);
                    } finally {
                        logger.debug("Map size: " + multiMediaJobs.size());
                        logger.debug("Time: "+System.currentTimeMillis());
                        multiMediaJobs.remove(random);
                    }
                }
            }

            private boolean veryExpensiveOperation(String jobId) {
                try {
                    Thread.sleep(7000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                logger.debug("Task is processed fully");

                return true;
            }
        });

    executorService.shutdown();
    try {
        executorService.awaitTermination(1, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return Response.status(Status.ACCEPTED)
                   .entity(commonHelper.toJSON(jobId)).build();
}

Is it really required to call shutdown in JBOSS environment? If i remove that it is accept all my request. Example i am seeing in all here is just main method. I just want to know how it works in real application.

Forgive me if i am misunderstood some concept.

Mohan
  • 699
  • 1
  • 11
  • 27

2 Answers2

2

The problem is that you shutdown the ExecutorService. So any subsequent task being submitted is rejected right away.

Jean Logeart
  • 52,687
  • 11
  • 83
  • 118
  • Thanks Jean for your response! I got that shutdown is causing issue in my case. But i am confused then where to add that shutdown in my case. If i restart my server, again it working for first request and same issue happens again for subsequent request. Is it compulsory to shutdown the executor in real application? If so where exactly we need to handle that. Or we can use some alternative pooling concept. – Mohan Jan 29 '16 at 17:22
1

I think you have some misunderstanding here.

When you submit to an executor, you will normally get a Future<T> object back. If you need a result from this, you'll call Future.get() and that will block until the threadpool executes your job. Otherwise you can just leave your jobs to be executed.

You wouldn't normally shutdown the executor unless you really want to shut it down, not accept any jobs, and let those queued up execute.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440