无法将ArrayList强制转换为org.springframework.batch.core.JobParameter

时间:2015-07-25 02:26:13

标签: spring spring-mvc spring-batch

我想从rest客户端向rest Web服务发送一个列表,该服务将在Spring Batch中启动一个作业。这是可能的还是我必须在启动作业之前将列表保存在database / flatfile中并从database / flatfile中读取输入值?我猜有人指出了在某些Jira问题上是如何做到的(见下文)但我无法弄清楚如何向前推进的基本想法。我放在我的控制器下面以及我是如何将它转换为JobParameter的。我放置了Jira链接和可能的方向,也许我应该采取但我真的不理解这个Jira问题的建议。我在SoapUi客户端下面添加了,最后我在底部粘贴了我的整个错误日志。

我的控制器:

@RequestMapping(value = "runit/family", method = RequestMethod.POST)
       public void handle(@RequestBody List<Person> myFamily) throws Exception {
              System.out.println(myFamily); //until here, it is fine

              JobParameters jobParameters = new JobParametersBuilder()
                           .addParameter("parametersReceived", (JobParameter) myFamily)
                           .addLong("time", System.currentTimeMillis()).toJobParameters();

              jobLauncher.run(job, jobParameters);
       }

SoapUi(帖子):

[{"firstName":"aa","lastName":"bb"}]

POJO的

public class Person {

    private String lastName;
    private String firstName;
    ...getters and setters

可能的工作方式,但我真的很困惑这个Jira问题。老实说,我期待某种方式更简单,就像我正在尝试使用JobParameter

https://jira.spring.io/browse/BATCH-966 (this suggestion is from 2009. Probably some more up-to-date technique is available)
<bean class="MyItemReader">
<property name="dynamicValuesHashMap" ref="map"/>
...
</bean>

<bean class="MyLauncher">
<property name="dynamicValuesHashMap" ref="map"/>
...
</bean>

整个错误:

java.lang.ClassCastException: java.util.ArrayList cannot be cast to org.springframework.batch.core.JobParameter

       at hello.BatchController.handle(BatchController.java:42)

       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:497)

       at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)

       at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)

       at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)

       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:776)

       at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:705)

       at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

       at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:959)

       at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:893)

       at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)

       at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)

       at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)

       at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)

       at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)

       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

       at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

       at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)

       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

       at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:85)

       at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

       at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)

       at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)

       at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)

       at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)

       at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)

       at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)

       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)

       at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)

       at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)

       at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)

       at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:668)

       at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1521)

       at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1478)

       at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)

       at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)

       at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)

       at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:1)

如果您查看spring batch的实际schema which stores meta data,您会看到作为参数的作业支持的可用类型列表(string_valdate_val,{{1 },long_val)。因此,当作业启动时,每个作业参数都保存在DB中,double_val作为参数名称,并且值存储在一个提到的key_name表中。 _val提示使用了哪种类型。

JobParameter的文档还提供了可用作作业参数的提示:

  

批处理作业的参数的域表示。只有   以下类型可以是参数:String,Long,Date和Double。该   识别标志用于指示参数是否用作   部分工作实例的识别。

我认为最好的方法是在DB中创建存储参数列表的表,并将该记录的id传递为type_cd,或者将列表序列化为json,并将其作为JobParameter传递给作业String。如果您选择第二个选项,请注意JobParameter作为string_val 250存储在数据库中,因此限制为250个字符。

答案 1 :(得分:0)

批处理作业是基于时间而不是基于事件。因此,您无法将Rest服务中的信息传递给您的Job实例。在您的控制器方法中触发作业之前,请将列表写入临时平面文件或将其存储在数据库中,您的作业可以从中读取它。