ClassCastException发生在Solver.solve

时间:2018-10-01 07:56:08

标签: java optaplanner

我在修改optaplanner-webexamples vrp。 所以我有点用spring-boot,并且我编辑了一些模型等。

但是运行解决方案时,它会在VehicleRoutingSolution类上产生ClassCastException

java.lang.ClassCastException: id.co.asyst.vrp.core.solution.VehicleRoutingSolution cannot be cast to id.co.asyst.vrp.core.solution.VehicleRoutingSolution
at org.optaplanner.core.impl.domain.common.accessor.LambdaBeanPropertyMemberAccessor.executeGetter(LambdaBeanPropertyMemberAccessor.java:120)
at org.optaplanner.core.impl.domain.solution.descriptor.SolutionDescriptor.getScore(SolutionDescriptor.java:1071)
at org.optaplanner.core.impl.score.director.AbstractScoreDirector.cloneSolution(AbstractScoreDirector.java:212)
at org.optaplanner.core.impl.solver.scope.DefaultSolverScope.setWorkingSolutionFromBestSolution(DefaultSolverScope.java:230)
at org.optaplanner.core.impl.solver.AbstractSolver.solvingStarted(AbstractSolver.java:75)
at org.optaplanner.core.impl.solver.DefaultSolver.solvingStarted(DefaultSolver.java:210)
at org.optaplanner.core.impl.solver.DefaultSolver.solve(DefaultSolver.java:190)
at id.co.asyst.vrp.rest.cdi.VehicleRoutingSolverManager.lambda$solve$1(VehicleRoutingSolverManager.java:105)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:266)
at java.util.concurrent.FutureTask.run(FutureTask.java)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at java.lang.Thread.run(Thread.java:748)

我在哪里做错了? 我直接从optaplanner-webexamples中获取的大部分代码,对模型的修改很少。主要只是表关系。

非常感谢。

4 个答案:

答案 0 :(得分:2)

原来是因为spring-boot-devtools。从pom.xml中删除它后,它运行正常

答案 1 :(得分:2)

我想出的最佳解决方案是按照Spring Boot docs中的建议使用上下文类加载器。使用上下文类加载器作为SolverFactory.createFromXmlResource()的第二个参数。例如:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
SolverFactory<VehicleRoutingSolution> sf = SolverFactory.createFromXmlResource(
        "org/optaplanner/examples/vehiclerouting/solver/vehicleRoutingSolverConfig.xml",
        classLoader
);

与我之前的回答一样,它可以与Developer Tools一起使用,因此您可以将spring-boot-devtools留在POM中并继续使用Automatic Restart功能。

答案 2 :(得分:1)

有关更多详细信息,请参见我的答案here。您必须检查重复出现的位置:

System.out.println(TheGhostClass.class.getProtectionDomain().getCodeSource().getLocation());

答案 3 :(得分:0)

不需要放弃spring-boot-devtools的另一种解决方案是将optaplanner-core JAR提升为spring-boot-devtools的“重新启动”类加载器。这可以通过创建具有以下内容的src/main/resources/META-INF/spring-devtools.properties来实现:

restart.include.optaplanner=/optaplanner-core.*jar

更多信息:https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-devtools.html#using-boot-devtools-customizing-classload