Add constructor with Callable to CallableTaskletAdapter

Issue #3831
This commit is contained in:
Sanghyuk Jung
2023-03-14 00:30:42 +09:00
committed by Mahmoud Ben Hassine
parent 058dd3c1b0
commit 31955a0690
2 changed files with 25 additions and 5 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2006-2022 the original author or authors.
* Copyright 2006-2023 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.
@@ -34,6 +34,21 @@ public class CallableTaskletAdapter implements Tasklet, InitializingBean {
private Callable<RepeatStatus> callable;
/**
* Create a new {@link CallableTaskletAdapter} instance.
*/
public CallableTaskletAdapter() {
}
/**
* Create a new {@link CallableTaskletAdapter} instance.
* @param callable the {@link Callable} to use
*/
public CallableTaskletAdapter(Callable<RepeatStatus> callable) {
setCallable(callable);
afterPropertiesSet();
}
/**
* Public setter for the {@link Callable}.
* @param callable the {@link Callable} to set
@@ -48,7 +63,7 @@ public class CallableTaskletAdapter implements Tasklet, InitializingBean {
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Assert.state(callable != null, "A Callable is required");
}

View File

@@ -23,17 +23,22 @@ import org.springframework.batch.repeat.RepeatStatus;
class CallableTaskletAdapterTests {
private final CallableTaskletAdapter adapter = new CallableTaskletAdapter();
@Test
public void testHandleWithConstructor() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter(() -> RepeatStatus.FINISHED);
assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null));
}
@Test
void testHandle() throws Exception {
void testHandleWithSetter() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter();
adapter.setCallable(() -> RepeatStatus.FINISHED);
assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null));
}
@Test
void testAfterPropertiesSet() {
assertThrows(IllegalStateException.class, adapter::afterPropertiesSet);
assertThrows(IllegalStateException.class, new CallableTaskletAdapter()::afterPropertiesSet);
}
}