Springbatch Failed to execute CommandLineRunner报错解决

 Springbatch Failed to execute CommandLineRunner报错解决

java.lang.IllegalStateException: Failed to execute CommandLineRunner
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:816) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:797) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:324) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at hoperun.pagoda.batchprocessingdata.BatchprocessingdataApplication.main(BatchprocessingdataApplication.java:31) [classes/:na]
Caused by: org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException: A job instance already exists and is complete for parameters={-spring.output.ansi.enabled=always}.  If you want to run this job again, change the parameters.
	at org.springframework.batch.core.repository.support.SimpleJobRepository.createJobExecution(SimpleJobRepository.java:132) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) ~[na:1.8.0_201]
	at java.lang.reflect.Method.invoke(Unknown Source) ~[na:1.8.0_201]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:198) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.batch.core.repository.support.AbstractJobRepositoryFactoryBean$1.invoke(AbstractJobRepositoryFactoryBean.java:181) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.1.6.RELEASE.jar:5.1.6.RELEASE]
	at com.sun.proxy.$Proxy64.createJobExecution(Unknown Source) ~[na:na]
	at org.springframework.batch.core.launch.support.SimpleJobLauncher.run(SimpleJobLauncher.java:134) ~[spring-batch-core-4.1.2.RELEASE.jar:4.1.2.RELEASE]
	at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.execute(JobLauncherCommandLineRunner.java:214) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.executeLocalJobs(JobLauncherCommandLineRunner.java:186) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.launchJobFromProperties(JobLauncherCommandLineRunner.java:172) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner.run(JobLauncherCommandLineRunner.java:166) ~[spring-boot-autoconfigure-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:813) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
	... 5 common frames omitted

springbatch 第二次启动报

Failed to execute CommandLineRunner   --执行CommandLineRunner失败

A job instance already exists and is complete for parameters={-spring.output.ansi.enabled=always}.  If you want to run this job again, change the parameters.

--job实例已经存在,并且完成了参数={-spring.output.ansi.enabled=always}。如果您想再次运行此job,请更改参数

看看代码:

package hoperun.pagoda.batchprocessingdata;

import java.util.Date;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.configuration.JobRegistry;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan
@EnableAutoConfiguration
public class BatchprocessingdataApplication {
    public static void main(String[] args) {
        //        String jobName = args[0];
        String jobName = "userMigrationJob0";
        try {
            //System.out.println("节点1");
            ConfigurableApplicationContext context = SpringApplication.run(BatchprocessingdataApplication.class, args);
            JobRegistry jobRegistry = context.getBean(JobRegistry.class);
            Job job = jobRegistry.getJob(jobName);
            JobLauncher jobLauncher = context.getBean(JobLauncher.class);
            //System.out.println("节点2:");
            JobExecution jobExecution = jobLauncher.run(job, createJobParams());
            if (!jobExecution.getExitStatus().equals(ExitStatus.COMPLETED)) {
                throw new RuntimeException(jobName +" Job execution failed.");
            }
        } catch (Exception e) {
            throw new RuntimeException(jobName +" Job execution failed.");
        }
    }

    private static JobParameters createJobParams() {
        //System.out.println("节点3");
        return new JobParametersBuilder().addDate("date", new Date()).toJobParameters();
    }
}

我明明设置了return new JobParametersBuilder().addDate("date", new Date()).toJobParameters();

这里的.addDate("date", new Date()) 明明每次参数不一样!!!但为啥还报参数一样呢!!!

解决办法:

在application.properties中增加配置:

spring.batch.job.enabled=false

把job都设置成不可用,程序便会根据jobLauncher.run来执行job。

 

产生错误原因:

因为spring batch在加载的时候每个job默认都会执行,默认先去执行job 库里面判断已经有这个job并且执行成了则报错,

--这是没有设置spring.batch.job.enabled=false时的打印

--这是设置了spring.batch.job.enabled=false时的打印

### Canal CommandLineRunner 执行失败解决方案 当遇到 `Canal` 项目中的 `CommandLineRunner` 执行失败问题时,通常会收到错误提示信息。对于命令执行失败的情况,例如在尝试连接到 IP 地址为 `192.168.10.172` 的服务器时出现问题[^1]。 #### 错误分析 为了有效解决问题,先要理解可能的原因: - **网络配置不当**:目标主机不可达或防火墙阻止访问。 - **服务未启动**:远程机器上的服务没有正常运行。 - **权限不足**:当前用户缺乏必要的权限来执行特定操作。 - **参数设置有误**:传递给命令的参数不正确或缺失重要选项。 #### 解决方法 ##### 验证网络连通性 确认本地环境能够成功 ping 到远端地址 `192.168.10.172`: ```bash ping 192.168.10.172 ``` 如果无法通信,则需检查路由器/交换机配置以及是否存在安全组规则限制流量。 ##### 检查服务状态 登录至目标服务器并验证所需的服务是否处于活动状态: ```bash systemctl status canal.service ``` 确保该服务已安装并且正在监听预期端口。 ##### 审核日志文件 查看应用程序的日志记录可以帮助定位具体原因。一般情况下,这些日志位于 `/var/log/canal/` 或者通过容器化部署的话,在相应的管理平台上获取。 ##### 修改应用配置 调整 `application.properties` 中的相关属性以适应实际场景需求: ```properties canal.server.ip=192.168.10.172 canal.server.port=11111 ``` 注意替换上述示例中的 IP 和端口号为你自己的具体情况。 ##### 权限校验 保证用于发起请求的身份具备足够的权利去调用 API 接口或其他资源;必要时可考虑临时提升权限级别来进行测试。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值