manankhh
manankhh

Reputation: 309

spring batch error not showing on my web page

My spring batch process has some bad SQL error in the console....... I am using spring batch job from URL..At this time of error I am not able to see any errors on my webpage but there is some error in my console..... I want to see some kind of error on my webpage...like(i.e 500...)

here is my job.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.1.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd 
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">

    <batch:job id="subrogJob" incrementer="incrementer">
        <batch:step id="subrn" next="readFromDataBase">
            <batch:tasklet ref="filetransferTasklet">
                <batch:listeners>
                    <batch:listener ref="setCurrentFile" />
                </batch:listeners>
            </batch:tasklet>
        </batch:step>
        <batch:step id="readFromDataBase" next="hasMoreFilesStep">
            <batch:tasklet>
                <batch:chunk reader="databaseReader" processor="Processor"
                    writer="dbToFileItemWriter" commit-interval="1" />
            </batch:tasklet>
            <batch:listeners>
                    <batch:listener ref="setCurrentFile1" />
                </batch:listeners>
        </batch:step>
        <batch:decision id="hasMoreFilesStep" decider="hasMoreFilesDecider">
            <batch:fail on="FAILED" />
            <batch:next on="CONTINUE" to="subrn" />
            <batch:end on="COMPLETED" />
        </batch:decision>
    </batch:job>

    <bean
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations"
            value="file:${UNIQUE_DIR}/${APP_NAME}/batch/nonadj_batch.properties" />
    </bean>



    <bean id="incrementer"
        class="org.springframework.batch.core.launch.support.RunIdIncrementer" ></bean>

    <bean id="setCurrentFile"
        class="com.hcsc.ccsp.nonadj.batch.InputFolderScanner"
        scope="step">
        <property name="collectionParameter" value="inputFiles" />
        <property name="outputFolder" value="${OutputFolder}" />
        <property name="inputFolder" value="${InputFolder}" />
        <property name="archiveFolder" value="${ArchiveFolder}" />
    </bean>
    <bean id="subrogationsetCurrentFile1"
        class="com.hcsc.ccsp.nonadj.subrogation.batch.SubrogationInputFolderScanner1"
        scope="step">

    </bean>

    <bean id="filetransferTasklet"
        class="com.hcsc.ccsp.nonadj.integration.FileTransferTasklet"
        scope="step">
        <property name="inputfile" value="file:#{jobExecutionContext['inputFile']}" />
        <property name="outputfile" value="file:#{jobExecutionContext['outputFile']}" />
    </bean>

    <bean id="Processor"
        class="com.hcsc.ccsp.nonadj.processor.Processor">

    </bean>

    <bean id="dbToFileItemWriter" class="org.springframework.batch.item.file.FlatFileItemWriter"
        scope="step">
        <property name="resource" value="file:#{jobExecutionContext['outputFile']}" />
        <property name="lineAggregator">
            <bean
                class="com.hcsc.ccsp.nonadj.writer.SubrogationLineAggregator" />
        </property>
        <property name="footerCallback" ref="HeaderFooterWriter" />
        <property name="headerCallback" ref="HeaderFooterWriter" />
        <property name="transactional" value="true" />

        <property name="appendAllowed" value="true" />
        <property name="saveState" value="true" />
    </bean>
    <bean id="HeaderFooterWriter"
        class="com.hcsc.ccsp.nonadj.writer.HeaderFooterWriter">
        <property name="delegate" ref="dbToFileItemWriter" />
    </bean>

    <bean id="hasMoreFilesDecider"
        class="com.hcsc.ccsp.nonadj.subrogation.batch.CollectionEmptyDecider">
        <property name="collectionParameter" value="inputFiles" />
        <property name="archiveFolder" value="${nArchiveFolder}" />
        <property name="errorFolder" value="${ErrorFolder}" />
        <property name="DeleteFilesOlderthanXDays" value="${DeleteFilesOlderthanXDays}" />
    </bean>

    <context:component-scan base-package="com.hcsc.ccsp.nonadj.batch"
        use-default-filters="false">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Component" />
    </context:component-scan>

    <context:annotation-config />

    <bean id="databaseReader"
        class="org.springframework.batch.item.database.JdbcCursorItemReader"
        scope="step">
        <property name="dataSource" ref="DataSource" />
        <property name="sql"
            value="SELECT Query" />
        <property name="rowMapper">
            <bean
                class="com.hcsc.ccsp.nonadj.integration.FieldSetMapper" />
        </property>
    </bean>



</beans>

and here is my jobLauncher class...

package com.hcsc.ccsp.nonadj.batch.web;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.apache.log4j.LogManager;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;


@Controller
public class JobLauncherController {

    private Logger LOG = LogManager.getLogger(JobLauncherController.class);

    private static final String JOB_PARAM = "job";

    private JobLauncher jobLauncher;

    private JobRegistry jobRegistry;

    private String param;

    private String paramValue;

    @Autowired
    public JobLauncherController(JobLauncher jobLauncher, JobRegistry jobRegistry) {

        super();

        this.jobLauncher = jobLauncher;

        this.jobRegistry = jobRegistry;

        DateFormat dateFormat = new SimpleDateFormat("ss");

        Date date = new Date();

        this.paramValue = dateFormat.format(date);

        LOG.info("Param Start Value :: " + dateFormat.format(date));
    }

    @RequestMapping(value="launcher",method=RequestMethod.GET)
    @ResponseStatus(HttpStatus.ACCEPTED)
    public void launch(@RequestParam String job, HttpServletRequest request) throws Exception {

        JobParametersBuilder builder = extractParameters(request);



        jobLauncher.run(jobRegistry.getJob(request.getParameter(JOB_PARAM)), builder.toJobParameters());
    }

    private JobParametersBuilder extractParameters(HttpServletRequest request) {

        JobParametersBuilder builder = new JobParametersBuilder();

        this.param = "param" + this.paramValue;

        if(!JOB_PARAM.equals(this.param)) {

            builder.addString(this.param,this.paramValue);
        }

        LOG.info("Param  :: " + this.param);

        LOG.info("Param Value :: " + this.paramValue);

        int paramVal = Integer.parseInt(this.paramValue) + 1;

        this.paramValue = paramVal + "";

        LOG.info("Incremented Param Value :: " + this.paramValue);

        return builder;
    }

}

Thanks in advance...

Upvotes: 0

Views: 554

Answers (1)

Barun
Barun

Reputation: 1622

Just check if execution was not COMPLETED; get all exceptions list and log them then throw exception or return 500 or whatever you like.

Your launch() should be something like this:

public void launch(@RequestParam String job, HttpServletRequest request) throws Exception {
  JobParametersBuilder builder = extractParameters(request);
  JobExecution execution = jobLauncher.run(jobRegistry.getJob(request.getParameter(JOB_PARAM)), builder.toJobParameters());
  if(BatchStatus.COMPLETED != execution.getStatus()){
    List<Throwable> throwList = execution.getAllFailureExceptions();
    for (Throwable throwable : throwList) {
      logger.error("error",throwable);
    }
      throw new Exception();
  }
}

Upvotes: 3

Related Questions