Announcement Announcement Module
Collapse
No announcement yet.
Execution of restartSample through Servlet Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Execution of restartSample through Servlet

    Hello,
    I have tried to trigger the execution of the BatchCommandLineLauncher for restartSample through a servlet. For this purpose, I have made following changes in BatchCommandLineLauncher :

    Created Default constructor
    Code:
    /**In BatchCommandLineLauncher code*******/
    //appended below lines.
    
    //class level variable or Class Member.
    private String beanRefContextPath=null;	
    private String parentKey=null;
    
    /**
    * When you create the object of BatchCommandLineLauncher,the
    * values for beanRefContextPath and parentKey are initialised.
    */
    public BatchCommandLineLauncher()
    {
    beanRefContextPath = System.getProperty(BEAN_REF_CONTEXT_KEY,
    				DEFAULT_BEAN_REF_CONTEXT_PATH);
    parentKey= System.getProperty(BATCH_EXECUTION_ENVIRONMENT_KEY,
    				DEFAULT_PARENT_KEY);
    beanFactoryLocator = ContextSingletonBeanFactoryLocator
    		.getInstance(beanRefContextPath);
    }

    Changed the signature of the start function to :

    /**
    * No need to provided parentKey parameter as it is * initialised to the required value while creating the * object : command
    */

    /**In BatchCommandLineLauncher code*******/
    int start(String path, String jobName)




    On Client side, I have designed a HTML page with a button. On Click of that button, I have call the servlet. The servlet has following service function :

    Code:
    protected void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException 
    {
    	try
    	{
    	PrintWriter pw=res.getWriter();
    	pw.println("Calling BatchCommandLineLauncher default constructor i.e. with " +
    				"no parameter");
    		/**
    		 * When you create the object of BatchCommandLineLauncher, the values for 
    		 * beanRefContextPath and parentKey are initialised.
    		 */
    
    	BatchCommandLineLauncher command = new BatchCommandLineLauncher();
    pw.println("Calling start method of the BatchCommandLineLauncher with parameter :path and jobName");
    
    		/**
    		 * No need to provided parentKey parameter as it is initialised to the required
    		 * value while creating the object : command 
    		 */
    
    int result = command.start("restartSample.xml", "restartSampleJob");		
    	pw.println("After start is called.");
    		}
    		catch (Exception e)
    		{
    			e.printStackTrace();
    		}
    	}


    But on the click on the button , I encountered an exception on Tomcat console stated :


    Code:
    09:53:09,068 FATAL BatchCommandLineLauncher:276 - org.springframework.beans.fact
    ory.BeanCreationException: Error creating bean with name 'restartSampleJob' defi
    ned in class path resource [restartSample.xml]: Cannot create inner bean 'step1'
     of type [org.springframework.batch.execution.step.SimpleStepConfiguration] whil
    e setting bean property 'steps'; nested exception is org.springframework.beans.f
    actory.BeanCreationException: Error creating bean with name 'step1' defined in c
    lass path resource [restartSample.xml]: Cannot create inner bean 'ExceptionResta
    rtableTasklet#fdb413' of type [ExceptionRestartableTasklet] while setting bean p
    roperty 'tasklet'; nested exception is org.springframework.beans.factory.BeanCre
    ationException: Error creating bean with name 'ExceptionRestartableTasklet#fdb41
    3' defined in class path resource [restartSample.xml]: Cannot create inner bean
    'org.springframework.batch.item.provider.ValidatingItemProvider#4b3f8b' of type
    [org.springframework.batch.item.provider.ValidatingItemProvider] while setting b
    ean property 'itemProvider'; nested exception is org.springframework.beans.facto
    ry.BeanCreationException: Error creating bean with name 'org.springframework.bat
    ch.item.provider.ValidatingItemProvider#4b3f8b' defined in class path resource [
    restartSample.xml]: Cannot resolve reference to bean 'fileInputSource' while set
    ting bean property 'inputSource'; nested exception is org.springframework.beans.
    factory.BeanCreationException: Error creating bean with name 'fileInputSource':
    Initialization of bean failed; nested exception is java.lang.ExceptionInInitiali
    zerError
    According to my observation, the ConfigurableApplicationContext in start() function is working properly.The exeception is raise at this line :

    context = new ClassPathXmlApplicationContext(new String[] {path},parent);

    Just for clarification : The values for path and parent which are supplied to above function are proper. I have checked the values by printing method on console.


    If someone can help me.
    Thanks in advance.

  • #2
    The log says there was an error executing a static initializer of the 'fileInputSource' bean - the only static variable I can see in the source code is a logger, so that might be a clue. If logging itself is not the problem try to set log level to debug for the spring framework classes and you should get more information.

    Comment


    • #3
      Why are you using the BatchCommandLineLauncher from within a servlet? It's really only intended to allow launching from the command line. Instead, I would use an implementation of the JobLauncher interface, and pass it a JobIdentifier.

      Comment


      • #4
        Care to be more specific, w/ sample or document?

        Comment


        • #5
          A basic example follows, it assumes that your application contexts have already been loaded at startup:

          Code:
          //assume the context has already been obtained
          JobLauncher jobLauncher = (JobLauncher)applicationContext.getBean("jobLauncher");
          
          JobIdentifier myJobId = new Jobidentifier("myJob");
          
          jobLauncher.run(myJobId);
          Please excuse any errors, as I just typed it out directly into the post.

          This is essentially all the batch command line launcher is doing, except it's doing a lot of work to locate and load application contexts (which isn't necessary in a servlet container) and pull parameters from the command line (also not necessary).

          Comment


          • #6
            Thanks!
            Code:
            //assume the context has already been obtained
            SimpleJobLauncher jobLauncher = (SimpleJobLauncher)context.getBean( "jobLauncher" );
            JobIdentifier myJobId = new SimpleJobIdentifier( "myJob" );
            jobLauncher.run( myJobId );

            Comment


            • #7
              Lucasward,

              Thanks for the suggestion, it worked for me.

              Comment


              • #8
                @ Guarav/All,
                Can you be more Specific , even my usecase is some what similar to your code .
                I happened to change a little bit of commandline class as per my requirement.
                But i want to trigger the batch process with a submit button .

                Can you be more specific.

                How did you trigger the batch using a screen.

                Dumb brain of mine cant understand anything that easily.

                Thanks & Regards
                Sandeep.S

                Comment

                Working...
                X