Skip to content

Commit 2974959

Browse files
author
xuebing feng
committed
自己写的工具类
1 parent 354fddd commit 2974959

File tree

129 files changed

+7515
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

129 files changed

+7515
-0
lines changed

spring-boot-demo-alwaysuse/pom.xml

Lines changed: 190 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.3.12.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>awaysuse</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>awaysuse</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>1.8</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-web</artifactId>
23+
</dependency>
24+
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-test</artifactId>
28+
<scope>test</scope>
29+
</dependency>
30+
<!-- lombok -->
31+
<dependency>
32+
<groupId>org.projectlombok</groupId>
33+
<artifactId>lombok</artifactId>
34+
<optional>true</optional>
35+
</dependency>
36+
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-configuration-processor</artifactId>
40+
<optional>true</optional>
41+
</dependency>
42+
43+
<dependency>
44+
<groupId>cn.hutool</groupId>
45+
<artifactId>hutool-all</artifactId>
46+
<version>5.5.2</version>
47+
</dependency>
48+
<dependency>
49+
<groupId>com.alibaba</groupId>
50+
<artifactId>fastjson</artifactId>
51+
<version>1.2.68</version>
52+
<scope>compile</scope>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>uyun.whale</groupId>
57+
<artifactId>common-encryption</artifactId>
58+
<version>2.0.4-SNAPSHOT</version>
59+
</dependency>
60+
61+
<dependency>
62+
<groupId>com.squareup.okhttp</groupId>
63+
<artifactId>okhttp</artifactId>
64+
<version>2.7.5</version>
65+
</dependency>
66+
67+
<dependency>
68+
<groupId>org.apache.commons</groupId>
69+
<artifactId>commons-lang3</artifactId>
70+
</dependency>
71+
72+
<!--poi依赖-->
73+
<dependency>
74+
<groupId>org.apache.poi</groupId>
75+
<artifactId>poi</artifactId>
76+
<version>3.9</version>
77+
</dependency>
78+
<dependency>
79+
<groupId>org.apache.poi</groupId>
80+
<artifactId>poi-ooxml</artifactId>
81+
<version>3.9</version>
82+
</dependency>
83+
<dependency>
84+
<groupId>org.apache.poi</groupId>
85+
<artifactId>poi-ooxml-schemas</artifactId>
86+
<version>3.9</version>
87+
</dependency>
88+
89+
<!--elasticsearch-->
90+
<dependency>
91+
<groupId>org.elasticsearch</groupId>
92+
<artifactId>elasticsearch</artifactId>
93+
<version>7.5.1</version>
94+
</dependency>
95+
<!-- Java High Level REST Client -->
96+
<dependency>
97+
<groupId>org.elasticsearch.client</groupId>
98+
<artifactId>elasticsearch-rest-high-level-client</artifactId>
99+
<version>7.5.1</version>
100+
<exclusions>
101+
<exclusion>
102+
<groupId>org.elasticsearch</groupId>
103+
<artifactId>elasticsearch</artifactId>
104+
</exclusion>
105+
<exclusion>
106+
<groupId>org.elasticsearch.client</groupId>
107+
<artifactId>elasticsearch-rest-client</artifactId>
108+
</exclusion>
109+
</exclusions>
110+
</dependency>
111+
<!-- Java Low Level REST Client -->
112+
<dependency>
113+
<groupId>org.elasticsearch.client</groupId>
114+
<artifactId>elasticsearch-rest-client</artifactId>
115+
<version>7.5.1</version>
116+
</dependency>
117+
118+
<dependency>
119+
<groupId>commons-lang</groupId>
120+
<artifactId>commons-lang</artifactId>
121+
<version>2.6</version>
122+
</dependency>
123+
124+
<!--map工具类-->
125+
<dependency>
126+
<groupId>org.apache.commons</groupId>
127+
<artifactId>commons-collections4</artifactId>
128+
<version>4.1</version>
129+
</dependency>
130+
131+
132+
133+
<dependency>
134+
<groupId>net.sf.ezmorph</groupId>
135+
<artifactId>ezmorph</artifactId>
136+
<version>1.0.6</version>
137+
</dependency>
138+
139+
<dependency>
140+
<groupId>org.springframework.retry</groupId>
141+
<artifactId>spring-retry</artifactId>
142+
</dependency>
143+
144+
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
145+
<dependency>
146+
<groupId>org.aspectj</groupId>
147+
<artifactId>aspectjrt</artifactId>
148+
<version>1.9.1</version>
149+
</dependency>
150+
<dependency>
151+
<groupId>org.aspectj</groupId>
152+
<artifactId>aspectjweaver</artifactId>
153+
<version>1.9.1</version>
154+
</dependency>
155+
156+
<dependency>
157+
<groupId>org.apache.commons</groupId>
158+
<artifactId>commons-lang3</artifactId>
159+
</dependency>
160+
161+
<dependency>
162+
<groupId>com.cronutils</groupId>
163+
<artifactId>cron-utils</artifactId>
164+
<version>8.0.0</version>
165+
</dependency>
166+
167+
<dependency>
168+
<groupId>com.google.guava</groupId>
169+
<artifactId>guava</artifactId>
170+
<version>28.1-jre</version>
171+
</dependency>
172+
173+
<dependency>
174+
<groupId>com.hankcs</groupId>
175+
<artifactId>hanlp</artifactId>
176+
<version>portable-1.7.8</version>
177+
</dependency>
178+
179+
</dependencies>
180+
181+
<build>
182+
<plugins>
183+
<plugin>
184+
<groupId>org.springframework.boot</groupId>
185+
<artifactId>spring-boot-maven-plugin</artifactId>
186+
</plugin>
187+
</plugins>
188+
</build>
189+
190+
</project>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.awaysuse;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.scheduling.annotation.EnableScheduling;
6+
7+
@SpringBootApplication
8+
@EnableScheduling
9+
public class AwaysuseApplication {
10+
11+
public static void main(String[] args) {
12+
SpringApplication.run(AwaysuseApplication.class, args);
13+
}
14+
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.example.awaysuse.callmsg;
2+
3+
import com.example.awaysuse.result.ResultCode;
4+
import lombok.AllArgsConstructor;
5+
import lombok.Data;
6+
7+
@Data
8+
@AllArgsConstructor
9+
public class CallResultMsg<T> {
10+
private boolean result;
11+
private int code;
12+
private String message;
13+
private T data;
14+
15+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.awaysuse.callmsg;
2+
3+
import lombok.Data;
4+
import lombok.Getter;
5+
import lombok.NoArgsConstructor;
6+
import lombok.Setter;
7+
8+
9+
@Getter
10+
public enum CodeAndMsg{
11+
SUCCESS(1000, "SUCCESS"),
12+
METHODFAIL(2000, "ENCOUNTER AN ERROR WHEN EXECUTE METHOD"),
13+
NOT_FOUND(404, "Not Found"),
14+
UNKNOWEXCEPTION(3000, "THIS IS AN UNKNOW EXCEPTION"),
15+
BAD_GATEWAY(502, "Bad Gateway");
16+
17+
private int code;
18+
private String msg;
19+
20+
CodeAndMsg(int code, String msg){
21+
this.code = code;
22+
this.msg = msg;
23+
}
24+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.example.awaysuse.callmsg;
2+
3+
import com.example.awaysuse.result.ResultCode;
4+
import org.springframework.http.HttpStatus;
5+
import org.springframework.web.bind.annotation.ExceptionHandler;
6+
import org.springframework.web.bind.annotation.ResponseStatus;
7+
import org.springframework.web.bind.annotation.RestController;
8+
import org.springframework.web.bind.annotation.RestControllerAdvice;
9+
10+
@RestControllerAdvice(annotations = RestController.class)
11+
public class UniformReponseHandler<T> {
12+
13+
@ResponseStatus(HttpStatus.OK)
14+
public CallResultMsg sendSuccessResponse(){
15+
return new CallResultMsg(true, CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMsg(), null);
16+
}
17+
18+
@ResponseStatus(HttpStatus.OK)
19+
public CallResultMsg sendSuccessResponse(T data) {
20+
return new CallResultMsg(true, CodeAndMsg.SUCCESS.getCode(), CodeAndMsg.SUCCESS.getMsg(), data);
21+
}
22+
23+
@ExceptionHandler(UserDefinedException.class)
24+
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
25+
public CallResultMsg sendErrorResponse_UserDefined(Exception exception){
26+
// return new CallResultMsg(false, ((UserDefinedException)exception).getException().getCode(), ((UserDefinedException)exception).getException().getMsg(), null);
27+
return new CallResultMsg(false, HttpStatus.INTERNAL_SERVER_ERROR.value(), HttpStatus.INTERNAL_SERVER_ERROR.toString(), null);
28+
29+
}
30+
31+
32+
@ExceptionHandler(Exception.class)
33+
@ResponseStatus(HttpStatus.BAD_GATEWAY)
34+
public CallResultMsg badGatewayResponse(Exception exception){
35+
if (exception instanceof UserDefinedException) {
36+
return this.sendErrorResponse_UserDefined(exception);
37+
}
38+
39+
return new CallResultMsg(false, CodeAndMsg.BAD_GATEWAY.getCode(),CodeAndMsg.BAD_GATEWAY.getMsg(),null);
40+
}
41+
42+
43+
@ResponseStatus(HttpStatus.NOT_FOUND)
44+
public CallResultMsg notFoundErrorResponse_System(){
45+
return new CallResultMsg(false, CodeAndMsg.NOT_FOUND.getCode(),CodeAndMsg.NOT_FOUND.getMsg(),null);
46+
}
47+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.example.awaysuse.callmsg;
2+
3+
import com.example.awaysuse.result.ResultCode;
4+
5+
6+
public class UserDefinedException extends RuntimeException {
7+
private CodeAndMsg exception;
8+
9+
public UserDefinedException(CodeAndMsg exception){
10+
this.exception = exception;
11+
}
12+
13+
public CodeAndMsg getException() {
14+
return exception;
15+
}
16+
17+
public void setException(CodeAndMsg exception) {
18+
this.exception = exception;
19+
}
20+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package com.example.awaysuse.config;
2+
3+
import org.apache.http.HttpHost;
4+
import org.apache.http.auth.AuthScope;
5+
import org.apache.http.auth.UsernamePasswordCredentials;
6+
import org.apache.http.client.CredentialsProvider;
7+
import org.apache.http.impl.client.BasicCredentialsProvider;
8+
import org.elasticsearch.client.RestClient;
9+
import org.elasticsearch.client.RestClientBuilder;
10+
import org.elasticsearch.client.RestHighLevelClient;
11+
import org.springframework.beans.factory.annotation.Value;
12+
import org.springframework.context.annotation.Bean;
13+
import org.springframework.context.annotation.Configuration;
14+
15+
import java.util.ArrayList;
16+
import java.util.List;
17+
18+
/**
19+
* @auther: fengh
20+
* @Date: 2020/7/2 17:04
21+
* @Description:
22+
*/
23+
@Configuration
24+
public class ESConfig {
25+
26+
@Value("${elasticsearch.hosts}")
27+
private String hosts;
28+
@Value("${elasticsearch.username}")
29+
private String username;
30+
@Value("${elasticsearch.passowrd}")
31+
private String password;
32+
@Value("${elasticsearch.schema}")
33+
private String schema;
34+
@Value("${elasticsearch.connectTimeOut}")
35+
private int connectTimeOut;
36+
@Value("${elasticsearch.socketTimeOut}")
37+
private int socketTimeOut;
38+
@Value("${elasticsearch.connectionRequestTimeOut}")
39+
private int connectionRequestTimeOut;
40+
@Value("${elasticsearch.maxConnectNum}")
41+
private int maxConnectNum;
42+
@Value("${elasticsearch.maxConnectPerRoute}")
43+
private int maxConnectPerRoute;
44+
45+
@Bean("restHighLevelClient")
46+
public RestHighLevelClient client(){
47+
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
48+
credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password));
49+
List<HttpHost> hostList = new ArrayList<>();
50+
String[] hostArray = hosts.split(",");
51+
for (String host : hostArray) {
52+
String[] ipPort = host.split(":");
53+
hostList.add(new HttpHost(ipPort[0], Integer.parseInt(ipPort[1]), schema));
54+
}
55+
RestClientBuilder builder = RestClient.builder(hostList.toArray(new HttpHost[0]));
56+
// 异步httpclient连接延时配置
57+
builder.setRequestConfigCallback(requestConfigBuilder -> {
58+
requestConfigBuilder.setConnectTimeout(connectTimeOut);
59+
requestConfigBuilder.setSocketTimeout(socketTimeOut);
60+
requestConfigBuilder.setConnectionRequestTimeout(connectionRequestTimeOut);
61+
return requestConfigBuilder;
62+
});
63+
// 异步httpclient连接数配置
64+
builder.setHttpClientConfigCallback(httpClientBuilder -> {
65+
httpClientBuilder.setMaxConnTotal(maxConnectNum);
66+
httpClientBuilder.setMaxConnPerRoute(maxConnectPerRoute);
67+
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider);
68+
return httpClientBuilder;
69+
});
70+
return new RestHighLevelClient(builder);
71+
}
72+
73+
}

0 commit comments

Comments
 (0)