org.springframework.batch.core.JobParameters Java Examples

The following examples show how to use org.springframework.batch.core.JobParameters. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SpringBatchFlowRunner.java    From spring-cloud-release-tools with Apache License 2.0 7 votes vote down vote up
private ExecutionResult runJob(Job job) {
	try {
		JobExecution execution = this.jobLauncher.run(job, new JobParameters());
		if (!ExitStatus.COMPLETED.equals(execution.getExitStatus())) {
			return ExecutionResult.failure(new IllegalStateException(
					"Job failed to get executed successfully. Failed with exit code ["
							+ execution.getExitStatus().getExitCode()
							+ "] and description ["
							+ execution.getExitStatus().getExitDescription() + "]"));
		}
		List<Exception> thrownExceptions = exceptionsThrownBySteps(execution);
		return new ExecutionResult(thrownExceptions);
	}
	catch (JobExecutionException ex) {
		return ExecutionResult.failure(ex);
	}
}
 
Example #2
Source File: SpringbatchPartitionerApp.java    From tutorials with MIT License 6 votes vote down vote up
public static void main(final String[] args) {
    // Spring Java config
    final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    context.register(SpringbatchPartitionConfig.class);
    context.refresh();

    final JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
    final Job job = (Job) context.getBean("partitionerJob");
    LOGGER.info("Starting the batch job");
    try {
        final JobExecution execution = jobLauncher.run(job, new JobParameters());
        LOGGER.info("Job Status : {}", execution.getStatus());
    } catch (final Exception e) {
        e.printStackTrace();
        LOGGER.error("Job failed {}", e.getMessage());
    }
}
 
Example #3
Source File: QuartzJobLauncher.java    From spring-batch-rest with Apache License 2.0 6 votes vote down vote up
@Override
protected void executeInternal(JobExecutionContext context) {
	String jobName = null;
	try {

		JobDetail jobDetail = context.getJobDetail();
		JobParameters jobParams = new JobParameters();
		if (jobDetail instanceof JobParamsDetail) {
			jobParams = JobParamUtil.convertRawToJobParams(((JobParamsDetail) jobDetail).getRawJobParameters());
		}

		JobDataMap dataMap = context.getJobDetail().getJobDataMap();
		jobName = dataMap.getString(JOB_NAME);

		JobLocator jobLocator = (JobLocator) context.getScheduler().getContext().get(JOB_LOCATOR);
		JobLauncher jobLauncher = (JobLauncher) context.getScheduler().getContext().get(JOB_LAUNCHER);

		Job job = jobLocator.getJob(jobName);
		log.info("Starting {}", job.getName());
		JobExecution jobExecution = jobLauncher.run(job, jobParams);
		log.info("{}_{} was completed successfully", job.getName(), jobExecution.getId());
	} catch (Exception e) {
		log.error("Job {} failed", jobName, e);
	}
}
 
Example #4
Source File: PostService.java    From wallride with Apache License 2.0 6 votes vote down vote up
@Transactional(propagation = Propagation.NOT_SUPPORTED)
public void updatePostViews() {
	LocalDateTime now = LocalDateTime.now();
	Set<JobExecution> jobExecutions = jobExplorer.findRunningJobExecutions("updatePostViewsJob");
	for (JobExecution jobExecution : jobExecutions) {
		LocalDateTime startTime = LocalDateTime.ofInstant(jobExecution.getStartTime().toInstant(), ZoneId.systemDefault());
		Duration d = Duration.between(now, startTime);
		if (Math.abs(d.toMinutes()) == 0) {
			logger.info("Skip processing because the job is running.");
			return;
		}
	}

	JobParameters params = new JobParametersBuilder()
			.addDate("now", Date.from(now.atZone(ZoneId.systemDefault()).toInstant()))
			.toJobParameters();
	try {
		jobLauncher.run(updatePostViewsJob, params);
	} catch (Exception e) {
		throw new ServiceException(e);
	}
}
 
Example #5
Source File: JobOperationsController.java    From spring-boot-starter-batch-web with Apache License 2.0 6 votes vote down vote up
/**
 * Borrowed from CommandLineJobRunner.
 *
 * @param job
 *            the job that we need to find the next parameters for
 * @return the next job parameters if they can be located
 * @throws JobParametersNotFoundException
 *             if there is a problem
 */
private JobParameters getNextJobParameters(Job job) throws JobParametersNotFoundException {
	String jobIdentifier = job.getName();
	JobParameters jobParameters;
	List<JobInstance> lastInstances = jobExplorer.getJobInstances(jobIdentifier, 0, 1);

	JobParametersIncrementer incrementer = job.getJobParametersIncrementer();

	if (lastInstances.isEmpty()) {
		jobParameters = incrementer.getNext(new JobParameters());
		if (jobParameters == null) {
			throw new JobParametersNotFoundException(
					"No bootstrap parameters found from incrementer for job=" + jobIdentifier);
		}
	} else {
		List<JobExecution> lastExecutions = jobExplorer.getJobExecutions(lastInstances.get(0));
		jobParameters = incrementer.getNext(lastExecutions.get(0).getJobParameters());
	}
	return jobParameters;
}
 
Example #6
Source File: ComposedTaskRunnerConfigurationWithPropertiesTests.java    From composed-task-runner with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testComposedConfiguration() throws Exception {
	JobExecution jobExecution = this.jobRepository.createJobExecution(
			"ComposedTest", new JobParameters());
	job.execute(jobExecution);

	Map<String, String> props = new HashMap<>(1);
	props.put("format", "yyyy");
	props.put("memory", "2048m");
	assertEquals(COMPOSED_TASK_PROPS, composedTaskProperties.getComposedTaskProperties());
	assertEquals(1010, composedTaskProperties.getMaxWaitTime());
	assertEquals(1100, composedTaskProperties.getIntervalTimeBetweenChecks());
	assertEquals("https://bar", composedTaskProperties.getDataflowServerUri().toASCIIString());
	List<String> args = new ArrayList<>(1);
	args.add("--baz=boo");
	Assert.isNull(job.getJobParametersIncrementer(), "JobParametersIncrementer must be null.");
	verify(this.taskOperations).launch("AAA", props, args, null);
}
 
Example #7
Source File: JobLaunchService.java    From spring-cloud with Apache License 2.0 6 votes vote down vote up
public JobResult launchJob(Job job) {
    try {
        JobParameters jobParameters = new JobParametersBuilder()
                .addDate("timestamp", Calendar.getInstance().getTime())
                .toJobParameters();
        JobExecution jobExecution = jobLauncher.run(job, jobParameters);
        return JobResult.builder()
                .jobName(job.getName())
                .jobId(jobExecution.getJobId())
                .jobExitStatus(jobExecution.getExitStatus())
                .timestamp(Calendar.getInstance().getTimeInMillis())
                .build();
    } catch (Exception e) {
        log.error(e.getMessage());
        throw new RuntimeException("launch job exception ", e);
    }
}
 
Example #8
Source File: TaskJobLauncherCommandLineRunnerCoreTests.java    From spring-cloud-task with Apache License 2.0 6 votes vote down vote up
@DirtiesContext
@Test
public void retryFailedExecutionOnNonRestartableJob() throws Exception {
	this.job = this.jobs.get("job").preventRestart()
			.start(this.steps.get("step").tasklet(throwingTasklet()).build())
			.incrementer(new RunIdIncrementer()).build();
	runFailedJob(new JobParameters());
	runFailedJob(new JobParameters());
	// A failed job that is not restartable does not re-use the job params of
	// the last execution, but creates a new job instance when running it again.
	assertThat(this.jobExplorer.getJobInstances("job", 0, 100)).hasSize(2);

	// try to re-run a failed execution
	Executable executable = () -> this.runner.execute(this.job,
			new JobParametersBuilder().addLong("run.id", 1L).toJobParameters());
	assertThatExceptionOfType(JobRestartException.class)
			.isThrownBy(executable::execute)
			.withMessage("JobInstance already exists and is not restartable");
}
 
Example #9
Source File: TaxCalculationStepITest.java    From batchers with Apache License 2.0 6 votes vote down vote up
@Test
public void taxCalculationStep_generatesCorrectCalculation() throws Exception {
    Employee employee = haveOneEmployee();

    JobParameters jobParameters = new JobParametersBuilder()
            .addLong("year", 2014L, true)
            .addLong("month", 5L, true)
            .toJobParameters();

    JobExecution jobExecution = jobLauncherTestUtils.launchStep(EmployeeJobConfigSingleJvm.TAX_CALCULATION_STEP, jobParameters);

    assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);

    List<TaxCalculation> byEmployee = taxCalculationRepository.findByEmployee(employee);

    assertThat(byEmployee).hasSize(1);
    TaxCalculation taxCalculation = byEmployee.get(0);
    assertThat(taxCalculation.getEmployee().getId()).isEqualTo(employee.getId());
    assertThat(taxCalculation.getYear()).isEqualTo(2014);
    assertThat(taxCalculation.getMonth()).isEqualTo(5);

    List<TaxCalculation> byYearAndMonth = taxCalculationRepository.find(2014, 5, 1L);
    assertThat(byYearAndMonth).hasSize(1);
}
 
Example #10
Source File: DefaultTaskJobServiceTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private void createSampleJob(JobRepository jobRepository, TaskBatchDao taskBatchDao,
		TaskExecutionDao taskExecutionDao, String jobName,
		int jobExecutionCount, BatchStatus status) {
	JobInstance instance = jobRepository.createJobInstance(jobName, new JobParameters());
	TaskExecution taskExecution = taskExecutionDao.createTaskExecution(jobName, new Date(), new ArrayList<>(), null);
	JobExecution jobExecution;

	for (int i = 0; i < jobExecutionCount; i++) {
		jobExecution = jobRepository.createJobExecution(instance,
				this.jobParameters, null);
		StepExecution stepExecution = new StepExecution("foo", jobExecution, 1L);
		stepExecution.setId(null);
		jobRepository.add(stepExecution);
		taskBatchDao.saveRelationship(taskExecution, jobExecution);
		jobExecution.setStatus(status);
		jobExecution.setStartTime(new Date());
		jobRepository.update(jobExecution);
	}
}
 
Example #11
Source File: DefaultTaskJobServiceTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Before
public void setup() {
	// not adding platform name as default as we want to check that this only one
	// gets replaced
	this.launcherRepository.save(new Launcher("fakeplatformname", "local", this.taskLauncher));
	Map<String, JobParameter> jobParameterMap = new HashMap<>();
	jobParameterMap.put("-spring.cloud.data.flow.platformname", new JobParameter("demo"));
	jobParameterMap.put("identifying.param", new JobParameter("testparam"));
	this.jobParameters = new JobParameters(jobParameterMap);

	JdbcTemplate template = new JdbcTemplate(this.dataSource);
	template.execute("DELETE FROM TASK_EXECUTION_PARAMS");
	template.execute("DELETE FROM TASK_EXECUTION;");
	initializeSuccessfulRegistry(this.appRegistry);
	template.execute("INSERT INTO TASK_EXECUTION (TASK_EXECUTION_ID, TASK_NAME) VALUES (0, 'myTask_ORIG');");
	initializeJobs();
	when(this.taskLauncher.launch(any())).thenReturn("1234");
}
 
Example #12
Source File: JobExecutionUtils.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
private static void createSampleJob(JobRepository jobRepository, TaskBatchDao taskBatchDao,
		TaskExecutionDao taskExecutionDao, String jobName,
		int jobExecutionCount, BatchStatus status) {
	JobInstance instance = jobRepository.createJobInstance(jobName, new JobParameters());
	TaskExecution taskExecution = taskExecutionDao.createTaskExecution(jobName, new Date(), new ArrayList<>(), null);
	JobExecution jobExecution;

	for (int i = 0; i < jobExecutionCount; i++) {
		jobExecution = jobRepository.createJobExecution(instance, new JobParameters(), null);
		StepExecution stepExecution = new StepExecution("foo", jobExecution, 1L);
		stepExecution.setId(null);
		jobRepository.add(stepExecution);
		taskBatchDao.saveRelationship(taskExecution, jobExecution);
		jobExecution.setStatus(status);
		jobExecution.setStartTime(new Date());
		if (BatchStatus.STOPPED.equals(status)) {
			jobExecution.setEndTime(new Date());
		}
		jobRepository.update(jobExecution);
	}
}
 
Example #13
Source File: SimpleJobService.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Override
public JobParameters getLastJobParameters(String jobName) {
	Collection<JobExecution> executions = jobExecutionDao.getJobExecutions(jobName, null, 0, 1);

	JobExecution lastExecution = null;
	if (!CollectionUtils.isEmpty(executions)) {
		lastExecution = executions.iterator().next();
	}

	JobParameters oldParameters = new JobParameters();
	if (lastExecution != null) {
		oldParameters = lastExecution.getJobParameters();
	}

	return oldParameters;

}
 
Example #14
Source File: DefaultTaskJobService.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
/**
 * Apply identifying job parameters to arguments.  There are cases (incrementers)
 * that add parameters to a job and thus must be added for each restart so that the
 * JobInstanceId does not change.
 * @param taskExecutionArgs original set of task execution arguments
 * @param jobParameters for the job to be restarted.
 * @return deduped list of arguments that contains the original arguments and any
 * identifying job parameters not in the original task execution arguments.
 */
private List<String>restartExecutionArgs(List<String> taskExecutionArgs, JobParameters jobParameters) {
	List<String> result = new ArrayList<>(taskExecutionArgs);
	Map<String, JobParameter> jobParametersMap = jobParameters.getParameters();
	for (String key : jobParametersMap.keySet()) {
		if (!key.startsWith("-")) {
			boolean existsFlag = false;
			for(String arg : taskExecutionArgs) {
				if(arg.startsWith(key)) {
					existsFlag = true;
					break;
				}
			}
			if(!existsFlag) {
				result.add(String.format("%s(%s)=%s", key, jobParametersMap.get(key).getType().toString().toLowerCase(), jobParameters.getString(key)));
			}
		}
	}
	return result;
}
 
Example #15
Source File: ComposedTaskRunnerConfigurationWithPropertiesTests.java    From spring-cloud-dataflow with Apache License 2.0 6 votes vote down vote up
@Test
@DirtiesContext
public void testComposedConfiguration() throws Exception {
	JobExecution jobExecution = this.jobRepository.createJobExecution(
			"ComposedTest", new JobParameters());
	job.execute(jobExecution);

	Map<String, String> props = new HashMap<>(1);
	props.put("format", "yyyy");
	props.put("memory", "2048m");
	assertEquals(COMPOSED_TASK_PROPS, composedTaskProperties.getComposedTaskProperties());
	assertEquals(1010, composedTaskProperties.getMaxWaitTime());
	assertEquals(1100, composedTaskProperties.getIntervalTimeBetweenChecks());
	assertEquals("https://bar", composedTaskProperties.getDataflowServerUri().toASCIIString());
	List<String> args = new ArrayList<>(1);
	args.add("--baz=boo --foo=bar");
	Assert.notNull(job.getJobParametersIncrementer(), "JobParametersIncrementer must not be null.");
	verify(this.taskOperations).launch("ComposedTest-AAA", props, args, null);
}
 
Example #16
Source File: JobLauncherDetails.java    From spring-batch-quartz-admin with Apache License 2.0 5 votes vote down vote up
private JobParameters getJobParametersFromJobMap(Map<String, Object> jobDataMap) {

        JobParametersBuilder builder = new JobParametersBuilder();

        for (Entry<String, Object> entry : jobDataMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            if (value instanceof String && !key.equals(Constants.JOB_NAME)) {
                builder.addString(key, (String) value);
            } else if (value instanceof Float || value instanceof Double) {
                builder.addDouble(key, ((Number) value).doubleValue());
            } else if (value instanceof Integer || value instanceof Long) {
                builder.addLong(key, ((Number) value).longValue());
            } else if (value instanceof Date) {
                builder.addDate(key, (Date) value);
            } else {
                // JobDataMap contains values which are not job parameters
                // (ignoring)
            }
        }

        // Needs a unique job parameter to rerun the completed job
        builder.addDate(Constants.JOB_RUN_DATE, new Date());

        return builder.toJobParameters();

    }
 
Example #17
Source File: DefaultSchedulerService.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
private String registerScheduler(final JobConfiguration jobConfiguration, final Class<?> schedulerClass) {
    try {
        final Set<Object> constructorValues = new HashSet<>();
        final JobLauncher jobLauncher = this.createLobLauncher(
                jobConfiguration.getJobSchedulerConfiguration().getTaskExecutorType(),
                this.jobRepository);
        final Job job = this.jobRegistry.getJob(jobConfiguration.getJobName());
        final JobParameters jobParameters = ServiceUtil.mapToJobParameters(jobConfiguration.getJobParameters());
        final JobSchedulerConfiguration jobSchedulerConfiguration = jobConfiguration.getJobSchedulerConfiguration();
        final String beanName;
        if (jobSchedulerConfiguration.getBeanName() == null || jobSchedulerConfiguration.getBeanName().isEmpty()) {
            beanName = this.generateSchedulerBeanName(jobConfiguration.getJobName(),
                    jobConfiguration.getJobSchedulerConfiguration().getJobSchedulerType());
        } else {
            beanName = jobSchedulerConfiguration.getBeanName();
        }
        final ThreadPoolTaskScheduler threadPoolTaskScheduler = this.registerThreadPoolTaskScheduler(beanName);
        final SchedulerConstructorWrapper schedulerConstructorWrapper = new SchedulerConstructorWrapper();
        schedulerConstructorWrapper.setJobParameters(jobParameters);
        schedulerConstructorWrapper.setJob(job);
        schedulerConstructorWrapper.setJobLauncher(jobLauncher);
        schedulerConstructorWrapper.setJobIncrementer(jobConfiguration.getJobIncrementer());
        schedulerConstructorWrapper.setJobConfiguration(jobConfiguration);
        schedulerConstructorWrapper.setThreadPoolTaskScheduler(threadPoolTaskScheduler);
        constructorValues.add(schedulerConstructorWrapper);
        this.beanRegistrar.registerBean(
                schedulerClass,
                beanName,
                constructorValues,
                null,
                null,
                null,
                null);
        return beanName;
    } catch (final Exception e) {
        throw new SpringBatchLightminConfigurationException(e, e.getMessage());
    }
}
 
Example #18
Source File: EmployeeBatchJobITest.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    mockServer = MockRestServiceServer.createServer(restTemplate);
    SmtpServerStub.start();

    Map<String, JobParameter> jobParamsMap = new HashMap<>();
    jobParamsMap.put("month", new JobParameter(MONTH));
    jobParamsMap.put("year", new JobParameter(YEAR));

    jobParams = new JobParameters(jobParamsMap);

    setInternalState(emailSenderService, "emailSendCounter", 0);
}
 
Example #19
Source File: JobInvokerController.java    From POC with Apache License 2.0 5 votes vote down vote up
@RequestMapping("/run-batch-job")
public String handle() throws Exception {

	JobParameters jobParameters = new JobParametersBuilder().addString("key", "Post")
			.addDate("currentDate", new Date()).toJobParameters();
	this.jobLauncher.run(this.executionJob, jobParameters);

	return "Batch job has been invoked";
}
 
Example #20
Source File: TaxCalculationStepITest.java    From batchers with Apache License 2.0 5 votes vote down vote up
@Test
public void taxCalculationStep_noWork() throws Exception {
    JobParameters jobParameters = new JobParametersBuilder()
            .addLong("year", 2014L, true)
            .addLong("month", 5L, true)
            .toJobParameters();

    JobExecution jobExecution = jobLauncherTestUtils.launchStep(EmployeeJobConfigSingleJvm.TAX_CALCULATION_STEP, jobParameters);

    assertThat(jobExecution.getExitStatus()).isEqualTo(ExitStatus.COMPLETED);
    assertThat(taxCalculationRepository.find(2014, 5, 1L)).isEmpty();
}
 
Example #21
Source File: ArgumentSanitizerTest.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testSanitizeJobParameters() {
	String[] JOB_PARAM_KEYS = {"username", "password", "name", "C", "D", "E"};
	Date testDate = new Date();
	JobParameter[] PARAMETERS = {new JobParameter("foo", true),
			new JobParameter("bar", true),
			new JobParameter("baz", true),
			new JobParameter(1L, true),
			new JobParameter(1D, true),
			new JobParameter(testDate, false)};

	Map<String, JobParameter> jobParamMap = new LinkedHashMap<>();
	for (int paramCount = 0; paramCount < JOB_PARAM_KEYS.length; paramCount++) {
		jobParamMap.put(JOB_PARAM_KEYS[paramCount], PARAMETERS[paramCount]);
	}
	JobParameters jobParameters = new JobParameters(jobParamMap);
	JobParameters sanitizedJobParameters = this.sanitizer.sanitizeJobParameters(jobParameters);
	for(Map.Entry<String, JobParameter> entry : sanitizedJobParameters.getParameters().entrySet()) {
		if (entry.getKey().equals("username") || entry.getKey().equals("password")) {
			Assert.assertEquals("******", entry.getValue().getValue());
		}
		else if (entry.getKey().equals("name")) {
			Assert.assertEquals("baz", entry.getValue().getValue());
		}
		else if (entry.getKey().equals("C")) {
			Assert.assertEquals(1L, entry.getValue().getValue());
		}
		else if (entry.getKey().equals("D")) {
			Assert.assertEquals(1D, entry.getValue().getValue());
		}
		else if (entry.getKey().equals("E")) {
			Assert.assertEquals(testDate, entry.getValue().getValue());
		}
	}
}
 
Example #22
Source File: JobStepExecutionControllerTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
private void createStepExecution(String jobName, String... stepNames) {
	JobInstance instance = jobRepository.createJobInstance(jobName, new JobParameters());
	JobExecution jobExecution = jobRepository.createJobExecution(instance, new JobParameters(), null);
	for (String stepName : stepNames) {
		StepExecution stepExecution = new StepExecution(stepName, jobExecution, 1L);
		stepExecution.setId(null);
		ExecutionContext context = new ExecutionContext();
		context.put("stepval", stepName);
		stepExecution.setExecutionContext(context);
		jobRepository.add(stepExecution);
	}
	TaskExecution taskExecution = dao.createTaskExecution(jobName, new Date(), new ArrayList<String>(), null);
	taskBatchDao.saveRelationship(taskExecution, jobExecution);
}
 
Example #23
Source File: FlatFileJobTest.java    From spring-boot-starter-batch-web with Apache License 2.0 5 votes vote down vote up
@Test
public void runXmlJob() throws Exception {
	File file = new File("target/out-xmlconfig.txt");
	file.delete();
	jobOperator.start("flatFileJobXml", "");
	while (jobRepository.getLastJobExecution("flatFileJobXml", new JobParameters()).getStatus().isRunning()) {
		Thread.sleep(100);
	}
	assertEquals(BatchStatus.COMPLETED,
			jobRepository.getLastJobExecution("flatFileJobXml", new JobParameters()).getStatus());
	assertEquals(10, FileUtils.readLines(file, StandardCharsets.UTF_8).size());
}
 
Example #24
Source File: TaxCalculatorJobService.java    From batchers with Apache License 2.0 5 votes vote down vote up
protected void startJobs(long year, long month) {
    try {
        JobParameters jobParameters = new JobParametersBuilder()
                .addLong("month", month)
                .addLong("year", year)
                .toJobParameters();

        LOG.info("Running job in jobservice");
        jobLauncher.run(employeeJob, jobParameters);
    } catch (JobExecutionAlreadyRunningException | JobRestartException
            | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {
        LOG.error("Job running failed", e);
        //TODO shouldn't we handle this differently?
    }
}
 
Example #25
Source File: JobExecutionControllerTests.java    From spring-cloud-dataflow with Apache License 2.0 5 votes vote down vote up
@Test
public void testStopStoppedJobExecution() throws Exception {
	mockMvc.perform(put("/jobs/executions/7").accept(MediaType.APPLICATION_JSON).param("stop", "true"))
			.andExpect(status().isUnprocessableEntity());

	final JobExecution jobExecution = jobRepository.getLastJobExecution(JobExecutionUtils.JOB_NAME_STOPPED,
			new JobParameters());
	Assert.assertNotNull(jobExecution);
	Assert.assertEquals(Long.valueOf(7), jobExecution.getId());
	Assert.assertEquals(BatchStatus.STOPPED, jobExecution.getStatus());

}
 
Example #26
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testFieldExtractorFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(FieldExtractorConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.delimited=true");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		AssertFile.assertLineCount(3, this.outputFile);

		String results = FileCopyUtils.copyToString(new InputStreamReader(
				new FileSystemResource(this.outputFile).getInputStream()));
		assertThat(results).isEqualTo("f\nb\nb\n");
	});
}
 
Example #27
Source File: FlatFileItemWriterAutoConfigurationTests.java    From spring-cloud-task with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeaderFooterFileGeneration() {
	ApplicationContextRunner applicationContextRunner = new ApplicationContextRunner()
			.withUserConfiguration(HeaderFooterConfiguration.class)
			.withConfiguration(
					AutoConfigurations.of(PropertyPlaceholderAutoConfiguration.class,
							BatchAutoConfiguration.class,
							SingleStepJobAutoConfiguration.class,
							FlatFileItemWriterAutoConfiguration.class))
			.withPropertyValues("spring.batch.job.jobName=job",
					"spring.batch.job.stepName=step1", "spring.batch.job.chunkSize=5",
					"spring.batch.job.flatfilewriter.name=fooWriter",
					String.format(
							"spring.batch.job.flatfilewriter.resource=file://%s",
							this.outputFile.getAbsolutePath()),
					"spring.batch.job.flatfilewriter.encoding=UTF-8",
					"spring.batch.job.flatfilewriter.delimited=true",
					"spring.batch.job.flatfilewriter.names=item");

	applicationContextRunner.run((context) -> {
		JobLauncher jobLauncher = context.getBean(JobLauncher.class);

		Job job = context.getBean(Job.class);

		JobExecution jobExecution = jobLauncher.run(job, new JobParameters());

		JobExplorer jobExplorer = context.getBean(JobExplorer.class);

		while (jobExplorer.getJobExecution(jobExecution.getJobId()).isRunning()) {
			Thread.sleep(1000);
		}

		AssertFile.assertLineCount(5, this.outputFile);

		String results = FileCopyUtils.copyToString(new InputStreamReader(
				new FileSystemResource(this.outputFile).getInputStream()));
		assertThat(results).isEqualTo("header\nfoo\nbar\nbaz\nfooter");
	});
}
 
Example #28
Source File: JobRunner.java    From spring-batch-performance-tuning with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	ApplicationContext applicationContext = new ClassPathXmlApplicationContext(CONFIG);

	Job job = applicationContext.getBean(Job.class);
	JobLauncher jobLauncher = applicationContext.getBean(JobLauncher.class);

	jobLauncher.run(job, new JobParameters());
}
 
Example #29
Source File: CsvService.java    From SpringBootBucket with MIT License 5 votes vote down vote up
/**
 * 根据类名反射运行相应的任务
 *
 * @param c 定义的Bean类
 */
public void runTask(Class c) throws Exception {
    TableName a = (TableName) c.getAnnotation(TableName.class);
    String tableName = a.value();
    Field[] fields = c.getDeclaredFields();
    List<String> fieldNames = new ArrayList<>();
    List<String> paramNames = new ArrayList<>();
    for (Field f : fields) {
        fieldNames.add(f.getName());
        paramNames.add(":" + f.getName());
    }
    String columnsStr = String.join(",", fieldNames);
    String paramsStr = String.join(",", paramNames);
    String csvFileName;
    if (p.getLocation() == 1) {
        csvFileName = p.getCsvDir() + tableName + ".csv";
    } else {
        csvFileName = tableName + ".csv";
    }
    JobParameters jobParameters1 = new JobParametersBuilder()
            .addLong("time", System.currentTimeMillis())
            .addString(KEY_JOB_NAME, tableName)
            .addString(KEY_FILE_NAME, csvFileName)
            .addString(KEY_VO_NAME, c.getCanonicalName())
            .addString(KEY_COLUMNS, String.join(",", fieldNames))
            .addString(KEY_SQL, "insert into " + tableName + " (" + columnsStr + ")" + " values(" + paramsStr + ")")
            .toJobParameters();
    jobLauncher.run(commonJob, jobParameters1);
}
 
Example #30
Source File: DomainParameterParser.java    From spring-batch-lightmin with Apache License 2.0 5 votes vote down vote up
/**
 * maps {@link JobParameters} to a String representation
 *
 * @param jobParameters {@link JobParameters} to map
 * @return a String representation of {@link JobParameters}
 */
public static String parseJobParametersToString(final JobParameters jobParameters) {
    final Map<String, JobParameter> jobParametersMap = jobParameters.getParameters();
    final Map<String, Object> paramatersMap = new HashMap<>();
    for (final Entry<String, JobParameter> entry : jobParametersMap.entrySet()) {
        paramatersMap.put(entry.getKey(), entry.getValue().getValue());
    }
    return parseParameterMapToString(paramatersMap);
}