0

我正在尝试在 optaplanner 站点中运行云平衡示例。我已将 cloudBalancingSolverConfig.xml 文件放在 web-app/xmls 目录中。这是我的 CloudBalanceDomainController 保存操作的代码:

def save() {
        def serveletContext = ServletContextHolder.servletContext
        def storagePath = serveletContext.getRealPath( "/" )

        String temp=storagePath+"xmls"+File.separator+"cloudBalancingSolverConfig.xml"
        File f=new File(temp)
        if(f.exists())
            println "file exists!"
        SolverFactory solverFactory=new XmlSolverFactory(temp)
        Solver solver=solverFactory.buildSolver()
        CloudBalance unsolvedCloudBalance=new CloudBalancingGenerator().createCloudBalance(params.computers,params.processes)
        solver.setPlanningProblem(unsolvedCloudBalance);
        solver.solve()
        CloudBalance solvedCloudBalance = (CloudBalance) solver.getBestSolution()
           render(view: "create", model: [cloudBalanceDomainInstance: cloudBalanceDomainInstance])
        flash.message = message(code: 'default.created.message', args: [message(code: 'cloudBalanceDomain.label', default: 'CloudBalanceDomain'), cloudBalanceDomainInstance.id])
        render "${getToDisplayString(solvedCloudBalance)}"
        //redirect(action: "show", showStr: getToDisplayString(solvedCloudBalance))
    }

运行时,它返回以下异常:

Executing action [save] of controller [optaplannergrails.CloudBalanceDomainController]  caused exception: Runtime error executing action
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:222) ~[grails-web-2.2.1.jar:2.2.1]
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.executeAction(ProxyAwareMixedGrailsControllerHelper.java) ~[plugin-classes/:na]
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:183) ~[grails-web-2.2.1.jar:2.2.1]
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleURI(ProxyAwareMixedGrailsControllerHelper.java) ~[plugin-classes/:na]
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleURI(AbstractGrailsControllerHelper.java:116) ~[grails-web-2.2.1.jar:2.2.1]
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleURI(ProxyAwareMixedGrailsControllerHelper.java) ~[plugin-classes/:na]
    at org.codehaus.groovy.grails.web.servlet.mvc.SimpleGrailsController.handleRequest(SimpleGrailsController.java:72) ~[grails-web-2.2.1.jar:2.2.1]
    at org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48) ~[spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.codehaus.groovy.grails.web.servlet.GrailsDispatcherServlet.doDispatch(GrailsDispatcherServlet.java:328) ~[grails-web-2.2.1.jar:2.2.1]
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:852) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:882) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789) [spring-webmvc-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195) [plugin-classes/:na]
    at grails.plugin.cache.web.filter.simple.MemoryPageFragmentCachingFilter.doFilter(MemoryPageFragmentCachingFilter.java) [plugin-classes/:na]
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) [plugin-classes/:na]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:70) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:749) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:487) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:412) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:339) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:314) [grails-web-2.2.1.jar:2.2.1]
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:279) [grails-web-2.2.1.jar:2.2.1]
    at org.codehaus.groovy.grails.web.util.WebUtils.forwardRequestForUrlMappingInfo(WebUtils.java:270) [grails-web-2.2.1.jar:2.2.1]
    at org.codehaus.groovy.grails.web.mapping.filter.UrlMappingsFilter.doFilterInternal(UrlMappingsFilter.java:221) [grails-plugin-url-mappings-2.2.1.jar:2.2.1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.obtainContent(GrailsPageFilter.java:200) [grails-web-2.2.1.jar:2.2.1]
    at org.codehaus.groovy.grails.web.sitemesh.GrailsPageFilter.doFilter(GrailsPageFilter.java:151) [grails-web-2.2.1.jar:2.2.1]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at javax.servlet.FilterChain$doFilter.call(Unknown Source) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.grails.plugin.resource.DevModeSanityFilter.doFilter(DevModeSanityFilter.groovy:44) [plugin-classes/:na]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.codehaus.groovy.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:69) [grails-web-2.2.1.jar:2.2.1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.codehaus.groovy.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:66) [grails-web-2.2.1.jar:2.2.1]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259) [spring-web-3.1.4.RELEASE.jar:3.1.4.RELEASE]
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:222) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1002) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312) [tomcat-embed-core-7.0.30.jar:7.0.30]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) [na:1.7.0_17]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) [na:1.7.0_17]
    at java.lang.Thread.run(Thread.java:722) [na:1.7.0_17]
Caused by: org.codehaus.groovy.grails.web.servlet.mvc.exceptions.ControllerExecutionException: Runtime error executing action
    at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:73) ~[grails-web-2.2.1.jar:2.2.1]
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.invoke(ProxyAwareMixedGrailsControllerHelper.java) ~[plugin-classes/:na]
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.handleAction(AbstractGrailsControllerHelper.java:334) ~[grails-web-2.2.1.jar:2.2.1]
    at grails.plugin.cache.web.ProxyAwareMixedGrailsControllerHelper.handleAction(ProxyAwareMixedGrailsControllerHelper.java) ~[plugin-classes/:na]
    at org.codehaus.groovy.grails.web.servlet.mvc.AbstractGrailsControllerHelper.executeAction(AbstractGrailsControllerHelper.java:217) ~[grails-web-2.2.1.jar:2.2.1]
    ... 76 common frames omitted
Caused by: java.lang.reflect.InvocationTargetException: null
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.7.0_17]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[na:1.7.0_17]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_17]
    at java.lang.reflect.Method.invoke(Method.java:601) ~[na:1.7.0_17]
    at org.springsource.loaded.ri.ReflectiveInterceptor.jlrMethodInvoke(ReflectiveInterceptor.java:1243) ~[springloaded-core-1.1.1.jar:1.0.0]
    at org.codehaus.groovy.grails.web.servlet.mvc.MixedGrailsControllerHelper.invoke(MixedGrailsControllerHelper.java:69) ~[grails-web-2.2.1.jar:2.2.1]
    ... 80 common frames omitted
Caused by: java.lang.IllegalArgumentException: The solver configuration (C:\Users\rahulserver\IdeaProjects\OptaPlannerGrails\web-app\xmls\cloudBalancingSolverConfig.xml) does not exist.
    at org.optaplanner.core.config.solver.XmlSolverFactory.configure(XmlSolverFactory.java:65) ~[optaplanner-core-6.0.0.CR5.jar:6.0.0.CR5]
    at org.optaplanner.core.config.solver.XmlSolverFactory.<init>(XmlSolverFactory.java:51) ~[optaplanner-core-6.0.0.CR5.jar:6.0.0.CR5]
    at optaplannergrails.CloudBalanceDomainController.save(CloudBalanceDomainController.groovy:35) ~[classes/:na]
    at grails.plugin.cache.web.filter.PageFragmentCachingFilter.doFilter(PageFragmentCachingFilter.java:195) [plugin-classes/:na]
    at grails.plugin.cache.web.filter.AbstractFilter.doFilter(AbstractFilter.java:63) [plugin-classes/:na]
    ... 3 common frames omitted

它说java.lang.IllegalArgumentException: The solver configuration (C:\Users\....\IdeaProjects\OptaPlannerGrails\web-app\xmls\cloudBalancingSolverConfig.xml) does not exist. 但是,令人惊讶的是f.exists()返回true,因此正在找到文件。那么可能导致此错误的原因是什么?

编辑: f.canRead()并且f.canWrite()还返回true。因此该文件具有读取和写入权限。虽然这里不需要写入文件。

4

2 回答 2

1

它正在使用getClass().getResource("..."). 这种方式不支持文件或 URL(但您可以通过在文件上构建自己的KieBase基础来模仿这一点)。

因此,如果您的文件位于C:\Users\....\IdeaProjects\OptaPlannerGrails\src\main\resources\org\foo\bar\solverConfig.xmlmaven 项目中,您会得到它,/org/foo/bar/solverConfig.xml因为它src/main/resources是 maven 中 java 类路径的一部分。如果web-app\xmls\是类路径的一部分(它可能不是因为它可能是战争资源的一部分)就cloudBalancingSolverConfig.xml可以了。

无论如何,您可能想确保您的 xml 最终进入yourWebApp.war!/WEB-INF/classes/org/foo/bar/solverConfig.xml并加载它 /org/foo/bar/solverConfig.xml

于 2014-02-09T13:05:19.017 回答
0

如果其他答案没有帮助(比如在我将 solverConfig.xml 打包到 Spring Boot 应用程序中时),那么这个小改动可能会帮助你:

SolverFactory.<PlanningSolution<BendableScore>>createFromXmlInputStream(
                    getClass().getClassLoader().getResourceAsStream("org/foo/bar/solverConfig.xml"))
于 2016-11-18T16:31:57.870 回答