1

I have my Batch Application Program, in which I am using ThreadPoolExecutor in my Batch Application Program. So as soon as it hits the-

executorService.shutdown();

line in my Batch Application program, I always get this below error in my Batch Application Program. And I am not able to ShutDown my own Executor Service. Can anyone suggest me how should I overcome this error.

Batch Execution Failed!
TaskResponse[exitCode=
   <ExitCode 
       type="User"
       code="30"
       msg="Unexpected error encounted"/>,
    causedByException=java.security.AccessControlException : Access denied (java.lang.RuntimePermission modifyThread)
]

java.security.AccessControlException: Access denied (java.lang.RuntimePermission modifyThread)
   at java.security.AccessController.checkPermission(AccessController.java:108)
   at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
   at java.util.concurrent.ThreadPoolExecutor.shutdown(ThreadPoolExecutor.java:1104)
   at com.host.task.Testing.CommandExecutor.runNextCommand(CommandExecutor.java:185)
   at com.host.task.Testing.PDSBatchTask.execute(PDSBatchTask.java:66)
   at com.host.nel.batch.runtime.Task.start(Task.java:234)
   at com.host.nel.batch.runtime.rt.TaskManager.__executeTask(TaskManager.java:138)
   at com.host.nel.batch.runtime.rt.TaskManager.executeTask(TaskManager.java:73)
   at com.host.nel.batch.runtime.rt.BatchManager.__executeTask(BatchManager.java:302)
   at com.host.nel.batch.runtime.rt.BatchManager.executeBatchApplication(BatchManager.java:126)
   at com.host.nel.batch.driver.BatchMain.main(BatchMain.java:95)
   at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
   at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
   at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
   at java.lang.reflect.Method.invoke(Method.java:599)
   at com.host.nel.batch.testutils.TestBatchMain$1.run(TestBatchMain.java:50)
   at java.security.AccessController.doPrivileged(AccessController.java:251)
   at com.host.nel.batch.testutils.TestBatchMain.driver(TestBatchMain.java:37)
   at com.host.nel.batch.testutils.TestBatchMain.driver(TestBatchMain.java:21)
   at test.com.host.task.Testing.Driver.main(Driver.java:21)

Update- I tried doing this way, but still no luck-

// create thread pool with given size
            final ExecutorService service = Executors.newFixedThreadPool(10); 

            // queue some tasks
            for(int i = 0; i < 3 * 10; i++) {
                service.submit(new ThreadTask(i));
            }


            AccessController.doPrivileged(new PrivilegedAction() {
                public Object run() {
                    // privileged code goes here, for example:
                    service.shutdown();
                    try {
                        if (!service.awaitTermination(Long.MAX_VALUE, TimeUnit.DAYS)) {
                            service.shutdownNow();
                        }
                    } catch (final InterruptedException pCaught) {
                        service.shutdownNow();
                        Thread.currentThread().interrupt();
                    }

                    return null; // nothing to return
                }
            });
arsenal
  • 23,366
  • 85
  • 225
  • 331

2 Answers2

1

Don't run your batch test code with a SecurityManager. Or, grant your code the modifyThread RuntimePermission.

jtahlborn
  • 52,909
  • 5
  • 76
  • 118
  • I have to run my Batch Test Code using Security Manager as I think its part of framework. How can I grant my code modifyThreadRunttimePermission? – arsenal May 21 '12 at 01:31
0

You can try to shutdown your Executor in a doPriviledged block

AccessController.doPrivileged(new PrivilegedAction() {
            public Object run() {
                // privileged code goes here, for example:
                executorService.shutdown();
                return null; // nothing to return
            }
        });
Svilen
  • 1,377
  • 1
  • 16
  • 23
  • Well if this doesn't work, then you should try what jtahlborn suggested - grant your code appropriate rights. – Svilen May 28 '12 at 05:47
  • I am not sure how should I grant my code appropriate rights? Can you help me with that? – arsenal May 28 '12 at 05:49
  • This is quite a topic on its own. This might help http://java.sun.com/developer/onlineTraining/Programming/JDCBook/appA.html. You should also have a look at the Java tutorial. Do a search on StackOverflow also - surely similar questions have been asked. – Svilen May 28 '12 at 05:57