com.my.batch.controller.JobOperationsController.java Source code

Java tutorial

Introduction

Here is the source code for com.my.batch.controller.JobOperationsController.java

Source

/*
 * Copyright 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.my.batch.controller;

import com.github.base.json.JsonResponse;
import com.google.common.collect.Ordering;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.explore.JobExplorer;
import org.springframework.batch.core.jsr.launch.JsrJobOperator;
import org.springframework.batch.core.launch.*;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.batch.core.repository.JobRepository;
import org.springframework.batch.core.repository.JobRestartException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

/**
 *
 */
@RestController
@RequestMapping("/batch/operation")
@Slf4j
public class JobOperationsController {

    @Autowired
    private JobOperator jobOperator;
    @Autowired
    private JobExplorer jobExplorer;
    @Autowired
    private JobRegistry jobRegistry;
    @Autowired
    private JobRepository jobRepository;
    @Autowired
    private JobLauncher jobLauncher;
    //   @Autowired
    //   private JsrJobOperator jsrJobOperator;

    @RequestMapping(value = "job/{jobName}", method = RequestMethod.POST)
    public JsonResponse launch(@RequestBody LinkedHashMap<String, String> jobParams, @PathVariable String jobName)
            throws NoSuchJobException, JobInstanceAlreadyExistsException, JobParametersInvalidException,
            JobExecutionAlreadyRunningException, JobRestartException, JobInstanceAlreadyCompleteException,
            JobParametersNotFoundException {
        try {
            //         LinkedHashMap<String, Object> jobParams = new LinkedHashMap<>();
            Job job = jobRegistry.getJob(jobName);
            JobParametersBuilder jobParametersBuilder = new JobParametersBuilder();
            for (Map.Entry<String, String> entry : jobParams.entrySet()) {
                jobParametersBuilder.addString(entry.getKey(), String.valueOf(entry.getValue()));
            }
            jobLauncher.run(job, jobParametersBuilder.toJobParameters());
        } catch (NoSuchJobException e) {
            log.error("job start fail, jobName " + jobName, e);
            JsonResponse.createFailMsg(e.getMessage());
        }
        return JsonResponse.createSuccess();
    }

    @RequestMapping(value = "job/{jobName}", method = RequestMethod.PUT)
    public JsonResponse reStart(@PathVariable String jobName)
            throws NoSuchJobExecutionException, JobExecutionNotRunningException, NoSuchJobException,
            JobRestartException, JobParametersInvalidException, JobInstanceAlreadyCompleteException {
        Set<Long> executions = jobOperator.getRunningExecutions(jobName);
        Ordering<Long> orders = Ordering.natural();
        //      List<Long> executionLists = orders.greatestOf(executions,10);
        jobOperator.restart(orders.max(executions));
        return JsonResponse.createSuccess();
    }

    @RequestMapping(value = "job/{jobName}", method = RequestMethod.DELETE)
    public JsonResponse stop(@PathVariable String jobName)
            throws NoSuchJobExecutionException, JobExecutionNotRunningException, NoSuchJobException {
        Set<Long> executions = jobOperator.getRunningExecutions(jobName);
        Ordering<Long> orders = Ordering.natural();
        //      List<Long> executionLists = orders.greatestOf(executions,10);
        jobOperator.stop(orders.max(executions));
        return JsonResponse.createSuccess();
    }

}