We upgraded our application to springboot 3.1.4 from springboot 2.5.4 and JDK from 1.8 to 17. We get the java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.concurrent.ExecutorService when we submit a callable task to the Executor service.

The code is as below

@Component
public class ThreadPoolInitializer {
private static ExecutorService parallelExecutor = null;
private static ExecutorService sequenceExecutor = null;

private static final int DEFAULT_THREAD_POOL_SIZE = 50;

public void setupExecutor() {
    parallelExecutor = Executors.newFixedThreadPool(DEFAULT_THREAD_POOL_SIZE);
    sequenceExecutor = Executors.newSingleThreadExecutor();
}

public ExecutorService getParallelExecutor() {
    return parallelExecutor;
}

public ExecutorService getSequenceExecutor() {
    return sequenceExecutor;
}
}

we are calling this sequenceExecutor from another method and submitting the task.

@Autowired
private ThreadPoolInitializer execPool;

public void addTasktoPool(CnfConfigPushTask task) {
    VesEventRoot event = task.getEvent();
        try {
            TaskHandler taskHandler = (TaskHandler) ctx.getBean(event.getType().toString());
            if (taskHandler != null) {
                taskHandler.setTask(task);
            } else {
                log.warn("No Task Handler for task : " + event.getType() + task);
            }
            task.setRetryCount(4);
            execPool.getSequenceExecutor().submit((Callable<String>) taskHandler);
        } catch (Exception e) {
            log.error("Exception while updating task : " +  task, e);
        }
    
}

We are seeing the exception here execPool.getSequenceExecutor().submit((Callable<String>) taskHandler);

the exception is as below

java.lang.reflect.InaccessibleObjectException: Unable to make field private final java.util.concurrent.ExecutorService java.util.concurrent.Executors$DelegatedExecutorService.e accessible: module java.base does not "opens java.util.concurrent" to unnamed module @327471b5
at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354) ~[?:?]
at java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297) ~[?:?]
at java.lang.reflect.Field.checkCanSetAccessible(Field.java:178) ~[?:?]
at java.lang.reflect.AccessibleObject.setAccessible(AccessibleObject.java:130) ~[?:?]
at org.apache.commons.lang.builder.ReflectionToStringBuilder.appendFieldsIn(ReflectionToStringBuilder.java:597) ~[commons-lang-2.6.jar!/:2.6]
at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:768) ~[commons-lang-2.6.jar!/:2.6]
at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:287) ~[commons-lang-2.6.jar!/:2.6]
at org.apache.commons.lang.builder.ReflectionToStringBuilder.toString(ReflectionToStringBuilder.java:121) ~[commons-lang-2.6.jar!/:2.6]
at com.mavenir.cms.vnf.util.LoggingAspect.getValue(LoggingAspect.java:107) ~[classes!/:0.0.1]
at com.mavenir.cms.vnf.util.LoggingAspect.logAfterReturning(LoggingAspect.java:65) ~[classes!/:0.0.1]
at jdk.internal.reflect.GeneratedMethodAccessor46.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:637) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:620) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.AspectJAfterReturningAdvice.afterReturning(AspectJAfterReturningAdvice.java:66) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:58) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:173) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89) ~[spring-aop-6.0.12.jar!/:6.0.12]
at com.mavenir.cms.vnf.util.LoggingAspect.logAround(LoggingAspect.java:52) ~[classes!/:0.0.1]
at jdk.internal.reflect.GeneratedMethodAccessor42.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:637) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:627) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:71) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:173) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:184) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:703) ~[spring-aop-6.0.12.jar!/:6.0.12]
at com.mavenir.cms.ThreadPoolInitializer$$SpringCGLIB$$0.getSequenceExecutor(<generated>) ~[classes!/:0.0.1]
at com.mavenir.cms.cnf.task.CnfConfigPushTaskManager.addTasktoPool(CnfConfigPushTaskManager.java:66) ~[classes!/:0.0.1]
at com.mavenir.cms.cnf.task.CnfConfigPushTaskManager.populateCnfConfigPushTask(CnfConfigPushTaskManager.java:53) ~[classes!/:0.0.1]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]
at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:196) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.AspectJAfterThrowingAdvice.invoke(AspectJAfterThrowingAdvice.java:64) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:173) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.adapter.AfterReturningAdviceInterceptor.invoke(AfterReturningAdviceInterceptor.java:57) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:173) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:751) ~[spring-aop-6.0.12.jar!/:6.0.12]
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89) ~[spring-aop-6.0.12.jar!/:6.0.12]
at com.mavenir.cms.vnf.util.LoggingAspect.logAround(LoggingAspect.java:52) ~[classes!/:0.0.1]
at jdk.internal.reflect.GeneratedMethodAccessor42.invoke(Unknown Source) ~[?:?]
at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
at java.lang.reflect.Method.invoke(Method.java:568) ~[?:?]

So we instantiated a singleThreadExecutor and used it and the exception is gone.

Working code:

private ExecutorService singleThreadExecutor =Executors.newSingleThreadExecutor();

public void addTasktoPool(CnfConfigPushTask task) {
    VesEventRoot event = task.getEvent();
        try {
            TaskHandler taskHandler = (TaskHandler) ctx.getBean(event.getType().toString());
            if (taskHandler != null) {
                taskHandler.setTask(task);
            } else {
                log.warn("No Task Handler for task : " + event.getType() + task);
            }
            task.setRetryCount(4);
            singleThreadExecutor.submit((Callable<String>) taskHandler);
        } catch (Exception e) {
            log.error("Exception while updating task : " +  task, e);
        }
    
}

Could not understand why the old code is not working in JDK 17. Please help.

0

There are 0 best solutions below