Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I referenced the constructor of JdbcTemplate, which also provides both setter and constructor injection.

https://github.com/spring-projects/spring-framework/blob/master/spring-jdbc/src/main/java/org/springframework/jdbc/core/JdbcTemplate.java#L172

}

/**
* Public setter for the {@link Callable}.
* @param callable the {@link Callable} to set
Expand All @@ -48,7 +63,7 @@ public void setCallable(Callable<RepeatStatus> callable) {
* @see org.springframework.beans.factory.InitializingBean#afterPropertiesSet()
*/
@Override
public void afterPropertiesSet() throws Exception {
public void afterPropertiesSet() {
Assert.state(callable != null, "A Callable is required");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,22 @@

class CallableTaskletAdapterTests {

private final CallableTaskletAdapter adapter = new CallableTaskletAdapter();
@Test
public void testHandleWithConstructor() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter(
new Callable<RepeatStatus>() {
@Override
public RepeatStatus call() throws Exception {
return RepeatStatus.FINISHED;
}
}
);
assertEquals(RepeatStatus.FINISHED, adapter.execute(null, null));
}

@Test
void testHandle() throws Exception {
void testExecuteWithSetter() throws Exception {
CallableTaskletAdapter adapter = new CallableTaskletAdapter();
adapter.setCallable(new Callable<RepeatStatus>() {
@Override
public RepeatStatus call() throws Exception {
Expand All @@ -40,7 +52,6 @@ public RepeatStatus call() throws Exception {

@Test
void testAfterPropertiesSet() {
assertThrows(IllegalStateException.class, adapter::afterPropertiesSet);
assertThrows(IllegalStateException.class, new CallableTaskletAdapter()::afterPropertiesSet);
}

}