Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: while running batch application through from servlet i am getting this error.........

  1. #1

    Default while running batch application through from servlet i am getting this error.........

    while running batch application through from servlet i am getting this error.........

    java.lang.NullPointerException
    at org.springframework.batch.core.launch.support.Simp leJobLauncher.run(SimpleJobLauncher.java:85)
    at com.mypack.UploadServlet.doPost(UploadServlet.java :174)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

    and my servlet is like this...

    ConfigurableApplicationContext context = new ClassPathXmlApplicationContext("helloWorldJob.xml" );
    context.getAutowireCapableBeanFactory().autowireBe anProperties(this, AutowireCapableBeanFactory.AUTOWIRE_BY_TYPE, false);
    org.springframework.batch.core.Job job;
    job = (org.springframework.batch.core.Job) context.getBean("helloWorldJob");

    JobParametersBuilder builder = new JobParametersBuilder();
    JobParameters jobParameters = builder.toJobParameters();
    JobLauncher launcher = new SimpleJobLauncher();
    try {
    JobExecution jobExecution = launcher.run(job, jobParameters);
    out.println(jobExecution.getExecutionContext());
    out.println(jobExecution.getExitStatus());
    }
    catch(Exception e)
    {
    e.printStackTrace();
    }

    and memory-repository is....

    <bean id="jobRepository-transactionManager"
    class="org.springframework.batch.support.transacti on.ResourcelessTransactionManager"/>
    <bean id="jobLauncher"
    class="org.springframework.batch.core.launch.suppo rt.SimpleJobLauncher">
    <property name="jobRepository" ref="jobRepository" />
    <property name="taskExecutor">
    <bean class="org.springframework.core.task.SimpleAsyncTa skExecutor" />
    </property>
    </bean>

    helloWorldJob.xml is......

    <import resource="MEMORY-JOBREPOSITORY.xml"/>
    <!-- <import resource="DB-JOBREPOSITORY.xml"/>-->

    <bean id="hello" class="com.mypack.PrintTasklet">
    <property name="message" value="Hello"/>
    </bean>
    <bean id="world" class="com.mypack.PrintTasklet">
    <property name="message" value=" World!"/>
    </bean>

    <!-- <bean id="dynamicJobParameters"
    class="com.ecomputercoach.DynamicJobParameters" />-->

    <!-- <batch:job id="helloWorldJob" job-repository="jobRepository" incrementer="dynamicJobParameters">-->
    <batch:job id="helloWorldJob" job-repository="jobRepository">
    <batch:step id="step0" next="step1">
    <batch:tasklet ref="hello"
    transaction-manager="jobRepository-transactionManager" />
    </batch:step>
    <batch:step id="step1">
    <batch:tasklet ref="world"
    transaction-manager="jobRepository-transactionManager" />
    </batch:step>
    </batch:job>

    </beans>



    can any body help on this.......
    Last edited by Rams; May 9th, 2011 at 06:30 AM.

  2. #2
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    this isn't the proper way to use Spring in a web environment, you should take a look at the Spring reference documentation before asking on the appropriate Spring forum (this isn't directly related to Spring Batch). In short, you should use a ContextLoaderListener to bootstrap the Spring application context and WebApplicationContextUtils to get access to the application context from your servlet.

  3. #3

    Default

    Hi arno,

    i m new to this..

    can you please give more info this...
    how to use CotextLoaderListener to bootstrap the spring application contex and webapplicationconextutils to get access to the applicatio cntext from my servlet.

    Regards,
    Rams

  4. #4

    Default

    is the following snippet is correct... please correct me....

    ServletContext context = getServletContext();
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContex t(context);
    JobLauncher launcher = (JobLauncher ) applicationContext.getBean("helloWorldJob.xml");
    Job job = (Job ) applicationContext.getBean("helloWorldJob");
    JobParameters jobParameters= new JobParametersBuilder().addDate("schedule.time", date).toJobParameters();
    try
    {
    String status = launcher.run(job,jobParameters);
    }
    catch(Exceptop e)
    {

    }

    I tried earlier like this eventhough it is showing same error..........correct me... which one can i use

  5. #5
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    did you configure the ContextLoaderListener in your web.xml before using WebApplicationContextUtils? if not, take a look at here: http://static.springsource.org/sprin...context-create

  6. #6

    Default

    i had changed web.xml like this eventhough it is giving same error

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>
    UploadFile</display-name>
    <servlet>
    <description>
    </description>
    <display-name>
    UploadServlet</display-name>
    <servlet-name>UploadServlet</servlet-name>
    <servlet-class>
    com.mypack.UploadServlet</servlet-class>
    </servlet>
    <servlet-mapping>
    <servlet-name>UploadServlet</servlet-name>
    <url-pattern>/UploadServlet</url-pattern>
    </servlet-mapping>
    <welcome-file-list>
    <welcome-file>upload.html</welcome-file>
    </welcome-file-list>

    <!-- <servlet>-->
    <!-- <servlet-name>context</servlet-name>-->
    <!-- <servlet-class>org.springframework.web.context.ContextLoade rServlet</servlet-class>-->
    <!-- <load-on-startup>1</load-on-startup>-->
    <!-- </servlet>-->

    <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/helloWorldJob.xml</param-value>
    </context-param>

    <listener>
    <listener-class>org.springframework.web.context.ContextLoade rListener</listener-class>
    </listener>
    </web-app>

    and error is......

    java.lang.NullPointerException
    at org.springframework.batch.core.launch.support.Simp leJobLauncher.run(SimpleJobLauncher.java:85)
    at com.mypack.UploadServlet.doPost(UploadServlet.java :174)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)

  7. #7
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    I doubt this works (you shouldn't even get a NullPointerException) :

    Code:
    JobLauncher launcher = (JobLauncher ) applicationContext.getBean("helloWorldJob.xml");
    did you check the source code to see what the SimpleJobLauncher misses to throw a NullPointerException?

  8. #8

    Default

    SEVERE: Servlet.service() for servlet UploadServlet threw exception
    org.springframework.beans.factory.NoSuchBeanDefini tionException: No bean named 'helloWorldJob.xml' is defined
    at org.springframework.beans.factory.support.DefaultL istableBeanFactory.getBeanDefinition(DefaultListab leBeanFactory.java:387)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getMergedLocalBeanDefinition(AbstractB eanFactory.java:971)
    at org.springframework.beans.factory.support.Abstract BeanFactory.doGetBean(AbstractBeanFactory.java:246 )
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:185)
    at org.springframework.beans.factory.support.Abstract BeanFactory.getBean(AbstractBeanFactory.java:164)
    at org.springframework.context.support.AbstractApplic ationContext.getBean(AbstractApplicationContext.ja va:880)
    at com.mypack.UploadServlet.doPost(UploadServlet.java :132)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet .java:717)
    at org.apache.catalina.core.ApplicationFilterChain.in ternalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.do Filter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invo ke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invo ke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke( StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke (ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invok e(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.servic e(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11Processor.process(H ttp11Processor.java:857)
    at org.apache.coyote.http11.Http11Protocol$Http11Conn ectionHandler.process(Http11Protocol.java:588)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run( JIoEndpoint.java:489)
    at java.lang.Thread.run(Unknown Source)


    JobLauncher launcher = (JobLauncher ) applicationContext.getBean("helloWorldJob.xml");

    the above statement showing error..
    pls correct me...........

  9. #9

    Default

    here is uploadservlet code.....

    PrintWriter out = response.getWriter();
    JobExecution status =null;
    Date date=null;
    ServletContext context = getServletContext();
    WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContex t(context);
    JobLauncher launcher = (JobLauncher ) applicationContext.getBean("helloWorldJob.xml");
    Job job = (Job ) applicationContext.getBean("helloWorldJob");

    JobParameters jobParameters= new JobParametersBuilder().addDate("schedule.time", date).toJobParameters();
    try
    {
    status = launcher.run((org.springframework.batch.core.Job)j ob,jobParameters);

    }
    catch(Exception e)
    {

    }

    response.setContentType("text/html");
    out = response.getWriter();
    out.print(status.getExecutionContext());
    out.println(status.getExitStatus());
    out.flush();
    String s="Hello, spring batch!";
    String s1="hp";
    String s2="spring batch! done";
    //PrintWriter out = response.getWriter();

    out.println(s);

  10. #10
    Join Date
    Dec 2005
    Location
    Lyon, France
    Posts
    311

    Default

    look up the job launcher with its bean name, not with the name of the file!
    Code:
    JobLauncher launcher = (JobLauncher ) applicationContext.getBean("jobLauncher");

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •