Error reporting java.lang.reflect.InvocationTargetException after the new environment is packaged

Background:

Business scenario: The MySQL database periodically synchronizes a small amount of data from the Oracle database, every few minutes
Version information: server jdk1.8.0_171, development and compilation environment jdk1.8.0_181 idea2023.1.2
Technical framework: Maven was used to manage dependencies, and no traces of frameworks such as spring were found

Description of the problem

After a small amount of code changes, local compilation and external dependencies are packaged into a jar package.
When deploying to a remote server, an error occurred, and the error message was checked in the system:

Error reason:
java.lang.reflect.InvocationTargetException

Cause Analysis:

Almost the same code, the original jar package can run on other servers, indicating that it is an environmental problem, search found

reason:
1. Package conflict, duplicate package or missing package
2. The jdk version of the project is different from that of the deployment jdk, which causes the InvocationTargetException exception information to return a null value, and the rewrite message method in the invoc is not called.
3. The mapping file has changed
plan:
1. Packages: Just check them next to each other, but you must pay attention to which version your system needs when you encounter duplicate packages.
2. jdk version:
(1) Replace the jdk package
(2) Use one method to handle the exception uniformly
3. The mapping file has changed
In some places in the project, the operation is performed using the reflection mechanism. Some changes may be made to the class. The department mapping file has not been changed. Such a problem will occur when using anonymity. The solution is either to change the mapping file or not to use anonymous transfer.
Original link

Therefore, it is speculated that the jdk version is inconsistent, try to use the same version of jdk for the development environment and the production environment

Solution:

Option 1: Replace jdk, repackage and deploy (not tested, if Option 2 is feasible, this solution is also theoretically feasible)
Solution 2: Recompile after replacing the jdk, and put the compiled class file of the changed code into the original executable jar package (note the modification of the IP port account password and other configurations)

  1. Install jdk
    Find the 171 version jdk from the project file and install it. The original jdk does not need to be ignored, and the environment variables do not need to be modified.
  2. ctrl + alt + shift + s to open the project configuration
    project》sdk》add sdk to add the jdk just installed
  3. Click the module tab under the project
    Switch the sdk of each module
  4. Click on platforms and settings
    Select the original jdk, click the minus sign above, and remove it from the project
    The newly added jdk remains unchanged, click apply, and then confirm
  5. The maven plugin is repackaged and sent to the server
  6. Open the new package with compression software, overwrite the modified class file to the old package and save it
  7. run successfully

Reunion and look back on August 3, 2023

Background

Business scenario: It is still a synchronization task, regularly boasting a small amount of data in the database, this time it is completely in the development environment
Version information: jdk1.8.0_171 idea2023.1.2 MySQL5.6
Technical framework: Ruoyi system 2 on springboot
The database has a three-database structure, a source database Oracle, an intermediate database MySQL, and a business database MySQL

Before the problem occurred, the master and the third data source were swapped, and the system was able to run, but at that time there was no commissioning task, could it run successfully?

Description of the problem

I wrote a new synchronization task. The jdk1.8.0_171 used before can run successfully, but this time it suddenly fails.
Error message:

09:32:24.902 [http-nio-6666-exec-13] DEBUG c.r.q.m.S.selectJobById - [debug,137] - ==> Preparing: select job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, con current, status , create_by, create_time, remark from sys_job where job_id = ?
09:32:24.903 [http-nio-6666-exec-13] DEBUG c.r.q.m.S.selectJobById - [debug,137] - ==> Parameters: 100 (Long)
09:32:24.907 [http-nio-6666-exec-13] DEBUG c.r.q.m.S.selectJobById - [debug,137] - <== Total: 1
Medication classification dictionary === view sync to business library === start
09:32:24.909 [quartzScheduler_Worker-2] ERROR c.r.q.u.AbstractQuartzJob - [execute,49] - Task execution exception - :
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:61)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:38)
at com.ruoyi.quartz.util.QuartzDisallowConcurrentExecution.doExecute(QuartzDisallowConcurrentExecution.java:19)
at com.ruoyi.quartz.util.AbstractQuartzJob.execute(AbstractQuartzJob.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException: null
at com.ruoyi.quartz.task.syncTask.drugTypeHisToLight(syncTask.java:63)
... 10 common frames omitted
09:32:24.915 [quartzScheduler_Worker-2] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, exception_info, create_time )values( ?, ?, ? , ?, ?, ?, sysdate() )
09:32:24.915 [quartzScheduler_Worker-2] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: The medication classification dictionary view is synchronized to the business library (String), DEFAULT(String), com.ruoyi.quartz.task. syncTask.drugTypeHisToLight(String), the total time it takes to synchronize the drug classification dictionary view to the business library: 1 millisecond (String), 1(String), java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:61)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:38)
at com.ruoyi.quartz.util.QuartzDisallowConcurrentExecution.doExecute(QuartzDisallowConcurrentExecution.java:19)
at com.ruoyi.quartz.util.AbstractQuartzJob.execute(AbstractQuartzJob.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.NullPointerException
at com.ruoyi.quartz.task.syncTask.drugTypeHisToLight(syncTask.java:63)
... 10 more
(String)
09:32:24.927 [quartzScheduler_Worker-2] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1
09:32:24.938 [schedule-pool-2] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - ==> Preparing: insert into sys_oper_log(title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location , oper_param, json_result, status, error_msg, cost_time, oper_time) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate())
09:32:24.939 [schedule-pool-2] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - ==> Parameters: scheduled task (String), 2(Integer), com.ruoyi.quartz.controller.SysJobController.run( )(String), PUT(String), 1(Integer), admin(String), null, /monitor/job/run(String), 127.0.0.1(String), Intranet IP(String), {<!- - -->"jobGroup":"DEFAULT","jobId":100,"misfirePolicy":"0","params":{<!-- --> }}(String), {<!-- -->"msg":"Operation succeeded","code":200}(String), 0(Integer), null, 16(Long)
09:32:24.970 [schedule-pool-2] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - <== Updates: 1

Cause Analysis

It is speculated that it is still an old problem, the jdk version, change the jdk to 181 again, this time I am smarter, only add 181, do not delete 171, because the jdk here will affect other projects (strange, it is only project configuration, it is reasonable to say that it is mutually cannot be affected between)

Of course, if this problem is not solved, even if only one 181jdk is kept, it is not so simple as expected.

Observe the console error log again and find that the synchronization task can be executed, but the first breakpoint in the service method has not been entered (I started it in debug mode), it seems to be a problem on the service side of the framework. It’s important!
Suddenly recalling, there seems to be a mapper that has not added mapper annotations, which is very similar to this problem, but it doesn’t work if it is added, and it doesn’t run here at all.
But, but, there is still a null pointer exception here, it seems that this service is not installed at all.

There are two exceptions on stackoverflow at the same time, but it is because the automatically injected variables are written as static. The point that inspired me is to check whether the called service method is in the interface. There is a definition, but this is obviously not, because if the interface does not exist, I cannot call it.
There is a link in the comments in the post, and it talks about some things about annotations. After reading a bit, I will take a look again when I have time.

Intuition tells me that there must be no use of framework components (component, service, mapper, etc.), or where there is no use of annotations to declare components, but I checked out before and missed one of the mapper annotations, and there are no others. Check the timing Above the task class, there are also annotations:

@Component("syncTask")

Solution

Since the scheduled task registers the component here, should the system’s scheduled task configuration also be called by the component?
Going back to the system again, I found:

Sure enough, the call target I wrote was wrong, and I didn’t notice that the above ones were all short.
Modify the call target, write the task called from the component, and the problem disappears

Interlude

Because the main function of the project is synchronization, the data has some bugs, which should be unique, and there are duplicate data in the source data, which can only be blamed for my poor consideration. When the data is repeated, an error will be reported when using the j8stream syntax to convert the list to a map:

16:44:02.853 [http-nio-6666-exec-1] INFO o.a.c.c.C.[.[.[/] - [log,173] - Initializing Spring DispatcherServlet 'dispatcherServlet'
16:44:03.279 [http-nio-6666-exec-1] DEBUG c.r.q.m.S.selectJobById - [debug,137] - ==> Preparing: select job_id, job_name, job_group, invoke_target, cron_expression, misfire_policy, concurrent , status, create_by, create_time, remark from sys_job where job_id = ?
16:44:03.281 [http-nio-6666-exec-1] DEBUG c.r.q.m.S.selectJobById - [debug,137] - ==> Parameters: 100(Long)
16:44:03.285 [http-nio-6666-exec-1] DEBUG c.r.q.m.S.selectJobById - [debug,137] - <== Total: 1
Medication classification dictionary === view sync to business library === start
16:44:03.306 [quartzScheduler_Worker-1] INFO c.r.f.d.DynamicDataSourceContextHolder - [setDataSourceType,26] - Switching to MASTER data source
16:44:03.311 [quartzScheduler_Worker-1] INFO c.r.f.d.DynamicDataSourceContextHolder - [setDataSourceType,26] - Switching to SLAVE data source
16:44:03.325 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - ==> Preparing: insert into sys_oper_log(title, business_type, method, request_method, operator_type, oper_name, dept_name, oper_url, oper_ip, oper_location , oper_param, json_result, status, error_msg, cost_time, oper_time) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, sysdate())
16:44:03.337 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - ==> Parameters: scheduled task (String), 2(Integer), com.ruoyi.quartz.controller.SysJobController.run( )(String), PUT(String), 1(Integer), admin(String), null, /monitor/job/run(String), 127.0.0.1(String), Intranet IP(String), {<!- - -->"jobGroup":"DEFAULT","jobId":100,"misfirePolicy":"0","params":{<!-- --> }}(String), {<!-- -->"msg":"Operation succeeded","code":200}(String), 0(Integer), null, 47(Long)
16:44:03.343 [schedule-pool-1] DEBUG c.r.s.m.S.insertOperlog - [debug,137] - <== Updates: 1
16:44:03.996 [quartzScheduler_Worker-1] INFO c.a.d.p.DruidDataSource - [init,996] - {<!-- -->dataSource-2} inited
16:44:03.996 [quartzScheduler_Worker-1] DEBUG c.r.t.m.V.selectVDrugTypeList - [debug,137] - ==> Preparing: select DRUG_TYPE_CODE, DRUG_TYPE_NAME from V_SW_DRUG_TYPE
16:44:04.024 [quartzScheduler_Worker-1] DEBUG c.r.t.m.V.selectVDrugTypeList - [debug,137] - ==> Parameters:
16:44:04.199 [quartzScheduler_Worker-1] DEBUG c.r.t.m.V.selectVDrugTypeList - [debug,137] - <== Total: 500
16:44:04.200 [quartzScheduler_Worker-1] INFO c.r.f.d.DynamicDataSourceContextHolder - [setDataSourceType,26] - switch to MASTER data source
16:44:04.201 [quartzScheduler_Worker-1] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - ==> Preparing: select dict_code, dict_sort, dict_label, dict_value, dict_type, css_class, list_class, is_default, status , create_by, create_time, remark from sys_dict_data WHERE dict_type = ? order by dict_sort asc
16:44:04.201 [quartzScheduler_Worker-1] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - ==> Parameters: drug_type(String)
16:44:04.227 [quartzScheduler_Worker-1] DEBUG c.r.s.m.S.selectDictDataList - [debug,137] - <== Total: 501
16:44:04.234 [quartzScheduler_Worker-1] ERROR c.r.q.u.AbstractQuartzJob - [execute,49] - Task execution exception - :
java.lang.reflect.InvocationTargetException: null
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:61)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:33)
at com.ruoyi.quartz.util.QuartzDisallowConcurrentExecution.doExecute(QuartzDisallowConcurrentExecution.java:19)
at com.ruoyi.quartz.util.AbstractQuartzJob.execute(AbstractQuartzJob.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.IllegalStateException: Duplicate key com.ruoyi.common.core.domain.entity.SysDictData@1e597602[
  dictCode=1575
  dictSort=0
  dictLabel=Troxerutin tablets
  dictValue=15070042666666
  dictType=drug_type
  cssClass=<null>
  listClass=default
  isDefault=N
  status=1
  createBy=
  createTime=Thu Aug 03 16:24:04 CST 2023
  updateBy=<null>
  updateTime=<null>
  remark=<null>
]
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.ruoyi.system.service.impl.SysDictDataServiceImpl.compareDictData(SysDictDataServiceImpl.java:193)
at com.ruoyi.system.service.impl.SysDictDataServiceImpl.getHisDrugType(SysDictDataServiceImpl.java:156)
at com.ruoyi.system.service.impl.SysDictDataServiceImpl$$FastClassBySpringCGLIB$$88d3e8dd.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:89)
at com.ruoyi.framework.aspectj.DataSourceAspect.around(DataSourceAspect.java:49)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:634)
at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:624)
at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:72)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:175)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:97)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:708)
at com.ruoyi.system.service.impl.SysDictDataServiceImpl$$EnhancerBySpringCGLIB$$4ab8d42f.getHisDrugType(<generated>)
at com.ruoyi.quartz.task.syncTask.drugTypeHisToLight(syncTask.java:63)
... 10 common frames omitted
16:44:04.237 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Preparing: insert into sys_job_log( job_name, job_group, invoke_target, job_message, status, exception_info, create_time )values( ?, ?, ? , ?, ?, ?, sysdate() )
16:44:04.238 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - ==> Parameters: Synchronize the medication classification dictionary view to the business library (String), DEFAULT(String), syncTask.drugTypeHisToLight(String), medication The total time it takes to synchronize the classification dictionary view to the business library: 931 milliseconds (String), 1 (String), java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:61)
at com.ruoyi.quartz.util.JobInvokeUtil.invokeMethod(JobInvokeUtil.java:33)
at com.ruoyi.quartz.util.QuartzDisallowConcurrentExecution.doExecute(QuartzDisallowConcurrentExecution.java:19)
at com.ruoyi.quartz.util.AbstractQuartzJob.execute(AbstractQuartzJob.java:43)
at org.quartz.core.JobRunShell.run(JobRunShell.java:202)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.IllegalStateException: Duplicate key com.ruoyi.common.core.domain.entity.SysDictData@1e597602[
  dictCode=1575
  dictSort=0
  dictLabel=Troxerutin tablets
  dictValue=15070042666666
  dictType=drug_type
  cssClass=<null>
  listClass=default
  isDefault=N
  status=1
  createBy=
  createTime=Thu Aug 03 16:24:04 CST 2023
  updateBy=<null>
  updateTime=<null>
  remark=<null>
]
at java.util.stream.Collectors.lambda$throwingMerger$0(Collectors.java:133)
at java.util.HashMap.merge(HashMap.java:1254)
at java.util.stream.Collectors.lambda$toMap$58(Collectors.java:1320)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.ruoyi.system.service.i(String)
16:44:04.249 [quartzScheduler_Worker-1] DEBUG c.r.q.m.S.insertJobLog - [debug,137] - <== Updates: 1

Note that this also includes the error in the topic of this article, but the reason is also mentioned in it, that is Duplicate key, which is another reason. Do a good job of logical processing of duplicate data and deduplication. up.