Skip to content

Instantly share code, notes, and snippets.

@william-tran
Created May 26, 2017 19:49
Show Gist options
  • Save william-tran/47f386b6d27aa7ec2ac4e02c9e283edc to your computer and use it in GitHub Desktop.
Save william-tran/47f386b6d27aa7ec2ac4e02c9e283edc to your computer and use it in GitHub Desktop.
Thread management sample using spring and executors.
executor:
thread-name-prefix: foo
core-pool-size: 2
max-pool-size: 2
package com.example.lombokdemo;
import javax.annotation.PostConstruct;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
@SpringBootApplication
@EnableConfigurationProperties
@Slf4j
public class ExecutorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(LombokDemoApplication.class, args);
}
@Bean
@ConfigurationProperties("executor")
public ThreadPoolTaskExecutor executor(ConfigurableApplicationContext context) {
return new ThreadPoolTaskExecutor() {
@Override
public void execute(Runnable task) {
super.execute(() ->{
try {
task.run();
} catch (Exception e) {
log.error("Exception caught, shutting down. {}", e);
context.close();
}
});
}
};
}
@Component
@RequiredArgsConstructor
@Slf4j
public static class MyController {
private final ThreadPoolTaskExecutor executor;
@PostConstruct
public void init() {
executor.execute(() -> {
try {
log.info("sleep for 10 seconds started");
Thread.sleep(10000);
throw new Exception("I'm dying!");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
executor.execute(() -> {
try {
log.info("sleep for a long time started");
Thread.sleep(10000000);
throw new Exception("I'm dying!");
} catch (Exception e) {
throw new RuntimeException(e);
}
});
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>executor-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>executor-demo</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment