Set the spring.cloud.task.closecontext_enable default to false
resolves #364
This commit is contained in:
committed by
Michael Minella
parent
3f316a0170
commit
94e074d841
@@ -60,7 +60,7 @@ public class TaskProperties {
|
||||
* When set to true the context is closed at the end of the task. Else
|
||||
* the context remains open.
|
||||
*/
|
||||
private Boolean closecontextEnabled = true;
|
||||
private Boolean closecontextEnabled = false;
|
||||
|
||||
public String getExternalExecutionId() {
|
||||
return externalExecutionId;
|
||||
|
||||
@@ -47,10 +47,10 @@ Upon completion of all of the `*Runner#run` calls from Spring Boot or the failur
|
||||
`ApplicationContext` (indicated via a `ApplicationFailedEvent`), the task execution is
|
||||
updated in the repository with the results.
|
||||
|
||||
NOTE: At the completion of a task (all `*Runner#run` methods are called and the task
|
||||
repository has been updated) the `ApplicationContext` will be closed by default. This
|
||||
behavior can be overriden by setting the property `spring.cloud.task.closecontext_enable`
|
||||
to false.
|
||||
NOTE: If the application requires the `ApplicationContext` to be closed at the
|
||||
completion of a task (all `*Runner#run` methods are called and the task
|
||||
repository has been updated), set the property `spring.cloud.task.closecontext_enable`
|
||||
to true.
|
||||
|
||||
[[features-task-execution-details]]
|
||||
=== The TaskExecution
|
||||
|
||||
@@ -290,28 +290,3 @@ If you notice, there are three lines of interest in the above output:
|
||||
NOTE: A simple task application can be found in the samples module
|
||||
of the Spring Cloud Task Project
|
||||
https://github.com/spring-cloud/spring-cloud-task/tree/master/spring-cloud-task-samples/timestamp[here].
|
||||
|
||||
|
||||
=== Writing your test
|
||||
|
||||
When writing your unit tests for a Spring Cloud Task application we have to keep
|
||||
in mind that Spring Cloud Task closes the context at the completion of the task
|
||||
as discussed <<features.adoc#features-lifecycle, here>>. If you are using Spring
|
||||
Framework's testing functionality to manage the application context, you'll want to turn
|
||||
off Spring Cloud Task's auto-closing of the context. Add the following
|
||||
line: `@TestPropertySource(properties = {"spring.cloud.task.closecontext_enable=false"})`
|
||||
to your tests will keep the context open. For example:
|
||||
|
||||
```
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
@TestPropertySource(properties = {"spring.cloud.task.closecontext_enabled=false"})
|
||||
public class DemoApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
//your test here
|
||||
}
|
||||
|
||||
}
|
||||
```
|
||||
@@ -25,6 +25,7 @@ import java.util.Map;
|
||||
import javax.sql.DataSource;
|
||||
|
||||
import org.h2.tools.Server;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
@@ -39,6 +40,7 @@ import org.springframework.cloud.task.repository.support.SimpleTaskExplorer;
|
||||
import org.springframework.cloud.task.repository.support.SimpleTaskRepository;
|
||||
import org.springframework.cloud.task.repository.support.TaskExecutionDaoFactoryBean;
|
||||
import org.springframework.context.ApplicationContextException;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
@@ -88,6 +90,15 @@ public class TaskStartTests {
|
||||
|
||||
private TaskRepository taskRepository;
|
||||
|
||||
private ConfigurableApplicationContext applicationContext;
|
||||
|
||||
@After
|
||||
public void tearDown() {
|
||||
if (this.applicationContext != null && this.applicationContext.isActive() ) {
|
||||
this.applicationContext.close();
|
||||
}
|
||||
}
|
||||
|
||||
@Autowired
|
||||
public void setDataSource(DataSource dataSource) {
|
||||
this.dataSource = dataSource;
|
||||
@@ -135,7 +146,7 @@ public class TaskStartTests {
|
||||
public void testWithGeneratedTaskExecution() throws Exception {
|
||||
taskRepository.createTaskExecution();
|
||||
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
|
||||
getTaskApplication(1).run(new String[0]);
|
||||
this.applicationContext = getTaskApplication(1).run(new String[0]);
|
||||
assertTrue(waitForDBToBePopulated());
|
||||
|
||||
Page<TaskExecution> taskExecutions = taskExplorer.findAll(PageRequest.of(0, 10));
|
||||
@@ -151,7 +162,7 @@ public class TaskStartTests {
|
||||
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
|
||||
assertEquals(TASK_EXECUTION_NAME, taskExplorer.getTaskExecution(1).getTaskName());
|
||||
|
||||
getTaskApplication(1).run(new String[0]);
|
||||
this.applicationContext = getTaskApplication(1).run(new String[0]);
|
||||
assertTrue(waitForDBToBePopulated());
|
||||
|
||||
Page<TaskExecution> taskExecutions = taskExplorer.findAll(PageRequest.of(0, 10));
|
||||
@@ -163,7 +174,7 @@ public class TaskStartTests {
|
||||
|
||||
@Test(expected = ApplicationContextException.class)
|
||||
public void testWithNoTaskExecution() throws Exception {
|
||||
getTaskApplication(55).run(new String[0]);
|
||||
this.applicationContext = getTaskApplication(55).run(new String[0]);
|
||||
}
|
||||
|
||||
@Test(expected = ApplicationContextException.class)
|
||||
@@ -171,7 +182,7 @@ public class TaskStartTests {
|
||||
taskRepository.createTaskExecution();
|
||||
assertEquals("Only one row is expected", 1, taskExplorer.getTaskExecutionCount());
|
||||
taskRepository.completeTaskExecution(1, 0, new Date(),"");
|
||||
getTaskApplication(1).run(new String[0]);
|
||||
this.applicationContext = getTaskApplication(1).run(new String[0]);
|
||||
}
|
||||
private SpringApplication getTaskApplication(Integer executionId) {
|
||||
SpringApplication myapp = new SpringApplication(TaskStartApplication.class);
|
||||
|
||||
Reference in New Issue
Block a user