Skip to content

Commit 5fddfb2

Browse files
committed
添加代码示例springboot-async
1 parent 33f127d commit 5fddfb2

File tree

14 files changed

+474
-1
lines changed

14 files changed

+474
-1
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ app-manage-api | [同时实现了需要认证授权访问的RESTful
5454

5555
## 运行
5656

57-
每个子项目都可以单独运行,都是打包成jar包后,通过使用内置jetty容器执行,有3种方式运行。
57+
每个子项目都可以单独运行,都是打包成jar包后,通过使用内置jetty容器执行,有3种方式运行。:point_right:
5858

5959
1. 在IDEA里面直接运行Application.java的main函数。
6060
2. 另一种方式是执行`mvn clean package`命令后传到linux服务器上面,通过命令`java -jar xxx.jar`方式运行

springboot-async/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# 此为注释– 将被Git 忽略
2+
# /结尾表示是目录,忽略目录和目录下的所有件
3+
# /开头表示根目录,否则是.gitignore的相对目录
4+
# !开头表示反选
5+
.idea/
6+
target/
7+
*.iml
8+
*.ipr
9+
*.iws
10+
*.log
11+
.svn/
12+
.project
13+
rebel.xml
14+
.rebel-remote.xml.*

springboot-async/LICENSE

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2018 Xiong Neng
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of
6+
this software and associated documentation files (the "Software"), to deal in
7+
the Software without restriction, including without limitation the rights to
8+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9+
the Software, and to permit persons to whom the Software is furnished to do so,
10+
subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

springboot-async/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
## 异步线程池
2+
3+
演示在SpringBoot中如何使用异步线程池
4+
5+
## 测试用例
6+
7+
`com.xncoding.pos.ApplicationTests.java`
8+
9+
## 许可证
10+
11+
Copyright (c) 2018 Xiong Neng
12+
13+
基于 MIT 协议发布: <http://www.opensource.org/licenses/MIT>

springboot-async/pom.xml

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.xncoding</groupId>
8+
<artifactId>springboot-async</artifactId>
9+
<version>1.0.0-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
12+
<name>springboot-async</name>
13+
<description>异步线程池</description>
14+
15+
<parent>
16+
<groupId>org.springframework.boot</groupId>
17+
<artifactId>spring-boot-starter-parent</artifactId>
18+
<version>1.5.9.RELEASE</version>
19+
<relativePath/>
20+
</parent>
21+
22+
<properties>
23+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
24+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
25+
<java.version>1.8</java.version>
26+
</properties>
27+
28+
<dependencies>
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter</artifactId>
32+
</dependency>
33+
<dependency>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-starter-test</artifactId>
36+
<scope>test</scope>
37+
</dependency>
38+
<dependency>
39+
<groupId>org.hamcrest</groupId>
40+
<artifactId>hamcrest-core</artifactId>
41+
<version>1.3</version>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.apache.maven.plugins</groupId>
50+
<artifactId>maven-compiler-plugin</artifactId>
51+
<version>3.6.1</version>
52+
<configuration>
53+
<!--<proc>none</proc>-->
54+
<source>1.8</source>
55+
<target>1.8</target>
56+
</configuration>
57+
</plugin>
58+
<plugin>
59+
<groupId>org.apache.maven.plugins</groupId>
60+
<artifactId>maven-surefire-plugin</artifactId>
61+
<version>2.20</version>
62+
<configuration>
63+
<skip>true</skip>
64+
</configuration>
65+
</plugin>
66+
<plugin>
67+
<groupId>org.springframework.boot</groupId>
68+
<artifactId>spring-boot-maven-plugin</artifactId>
69+
<executions>
70+
</executions>
71+
</plugin>
72+
</plugins>
73+
74+
<resources>
75+
<resource>
76+
<directory>src/main/resources</directory>
77+
</resource>
78+
<resource>
79+
<directory>src/main/java</directory>
80+
<includes>
81+
<include>**/*.xml</include>
82+
</includes>
83+
</resource>
84+
</resources>
85+
</build>
86+
87+
</project>

springboot-async/run.sh

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#!/bin/bash
2+
# 项目自动更新脚本
3+
# 先clone相应的分支下来:
4+
# git clone ssh://[email protected]:7999/xxx.git
5+
# 远程调试启动:
6+
# nohup java -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=5005 -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
7+
8+
function start {
9+
profile="$1"
10+
echo "启动环境profile=${profile}"
11+
jarfile=$(ls target/*.jar)
12+
if [[ "$?" == "0" ]]; then
13+
stop $profile $jarfile
14+
fi
15+
branch=$(git branch |awk '{print $2}')
16+
git pull origin ${branch}
17+
echo "更新完代码开始重新打包"
18+
mvn clean && mvn clean && mvn package -DskipTests=true
19+
if [[ "$?" != "0" ]]; then
20+
echo "编译出错,退出!"
21+
exit 1
22+
fi
23+
echo "nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &"
24+
nohup java -Xms512m -Xmx1024m -jar -Dspring.profiles.active=${profile} ${jarfile} >/dev/null 2>&1 &
25+
echo "启动应用中,请查看日志文件..."
26+
}
27+
28+
function stop {
29+
profile="$1"
30+
jarfile="$2"
31+
ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep > /dev/null
32+
if [[ "$?" == "0" ]]; then
33+
echo "该应用还在跑,我先停了它"
34+
pid=$(ps aux | grep "${jarfile}" | grep "spring.profiles.active=${profile}" | grep -v grep |awk '{print $2}')
35+
if [[ "$pid" != "" ]]; then
36+
kill -9 $pid
37+
fi
38+
echo "停止应用成功..."
39+
fi
40+
}
41+
42+
if [[ "$1" == "start" ]]; then
43+
if [[ "$#" < 2 ]]; then
44+
echo "请输入正确参数:./epay.sh start {profile}"
45+
exit 1
46+
fi
47+
profile="$2"
48+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
49+
echo "参数错误,请输入正确的profile参数,使用方法:"
50+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
51+
exit 1
52+
fi
53+
start "${profile}"
54+
elif [[ "$1" == "stop" ]]; then
55+
if [[ "$#" < 2 ]]; then
56+
echo "请输入正确参数:./epay.sh stop {profile}"
57+
exit 1
58+
fi
59+
profile="$2"
60+
if [[ "$profile" != "dev" && "$profile" != "test" && "$profile" != "show" && "$profile" != "production" ]]; then
61+
echo "参数错误,请输入正确的profile参数,使用方法:"
62+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
63+
exit 1
64+
fi
65+
jarfile=$(ls target/*.jar)
66+
stop $profile $jarfile
67+
else
68+
echo "参数错误,使用方法:{}参数是必填的,[]参数可选"
69+
echo "./epay.sh start {profile} ==> 启动应用,{profile}取值:dev|test|show|production"
70+
echo "./epay.sh stop {profile} ==> 停止应用,{profile}取值:dev|test|show|production"
71+
exit 1
72+
fi
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.xncoding.pos;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class Application {
8+
public static void main(String[] args) {
9+
SpringApplication.run(Application.class, args);
10+
}
11+
12+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.xncoding.pos.async;
2+
3+
/**
4+
* 异步方法异常
5+
*
6+
* @author XiongNeng
7+
* @version 1.0
8+
* @since 2018/1/25
9+
*/
10+
public class AsyncException extends RuntimeException {
11+
public AsyncException() {
12+
super();
13+
}
14+
15+
public AsyncException(String msg) {
16+
super(msg);
17+
}
18+
19+
public AsyncException(int code, String msg) {
20+
super(msg);
21+
this.code = code;
22+
}
23+
24+
/**
25+
* 错误代码
26+
*/
27+
private int code;
28+
29+
public int getCode() {
30+
return code;
31+
}
32+
33+
public void setCode(int code) {
34+
this.code = code;
35+
}
36+
37+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.xncoding.pos.async;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.aop.interceptor.AsyncUncaughtExceptionHandler;
6+
7+
import java.lang.reflect.Method;
8+
import java.util.Arrays;
9+
10+
/**
11+
* AsyncExceptionHandler
12+
*
13+
* @author XiongNeng
14+
* @version 1.0
15+
* @since 2018/1/25
16+
*/
17+
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler {
18+
private static final Logger log = LoggerFactory.getLogger(AsyncExceptionHandler.class);
19+
@Override
20+
public void handleUncaughtException(Throwable ex, Method method, Object... params) {
21+
log.error("Async method has uncaught exception, params:{}" + Arrays.toString(params));
22+
23+
if (ex instanceof AsyncException) {
24+
AsyncException asyncException = (AsyncException) ex;
25+
log.error("asyncException:" + asyncException.getMessage());
26+
}
27+
28+
log.error("Exception :", ex);
29+
}
30+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package com.xncoding.pos.async;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.scheduling.annotation.Async;
6+
import org.springframework.scheduling.annotation.AsyncResult;
7+
import org.springframework.stereotype.Component;
8+
9+
import java.util.concurrent.Future;
10+
11+
/**
12+
* AsyncDemo
13+
*
14+
* @author XiongNeng
15+
* @version 1.0
16+
* @since 2018/1/25
17+
*/
18+
@Component
19+
public class AsyncTask {
20+
private static final Logger logger = LoggerFactory.getLogger(AsyncTask.class);
21+
22+
@Async
23+
public void dealNoReturnTask() {
24+
logger.info("返回值为void的异步调用开始" + Thread.currentThread().getName());
25+
try {
26+
Thread.sleep(3000);
27+
} catch (InterruptedException e) {
28+
e.printStackTrace();
29+
}
30+
logger.info("返回值为void的异步调用结束" + Thread.currentThread().getName());
31+
}
32+
33+
@Async
34+
public Future<String> dealHaveReturnTask(int i) {
35+
logger.info("asyncInvokeReturnFuture, parementer=" + i);
36+
Future<String> future;
37+
try {
38+
Thread.sleep(1000 * i);
39+
future = new AsyncResult<String>("success:" + i);
40+
} catch (InterruptedException e) {
41+
future = new AsyncResult<String>("error");
42+
}
43+
return future;
44+
}
45+
46+
}

0 commit comments

Comments
 (0)