Skip to content

Commit

Permalink
Merge pull request #18 from Frodez/0.3-alpha
Browse files Browse the repository at this point in the history
0.3 alpha
  • Loading branch information
Frodez committed May 10, 2019
2 parents 7b30a5d + 5f969fd commit 4c4301a
Show file tree
Hide file tree
Showing 14 changed files with 86 additions and 94 deletions.
44 changes: 44 additions & 0 deletions src/main/java/frodez/config/mvc/WebMvcConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,22 @@

import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;
import frodez.config.mvc.async.AsyncConfig;
import frodez.config.mvc.converter.JsonConverer;
import frodez.config.mvc.converter.StringEscapeConverter;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.convert.converter.Converter;
import org.springframework.format.FormatterRegistry;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
import org.springframework.web.context.request.async.TimeoutCallableProcessingInterceptor;
import org.springframework.web.context.request.async.TimeoutDeferredResultProcessingInterceptor;
import org.springframework.web.servlet.config.annotation.AsyncSupportConfigurer;
import org.springframework.web.servlet.config.annotation.ContentNegotiationConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
Expand All @@ -20,6 +28,14 @@
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

@Autowired
private AsyncConfig asyncConfig;

/**
* 配置消息转换器
* @author Frodez
* @date 2019-05-10
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//清除掉原本的AbstractJackson2HttpMessageConverter和StringHttpMessageConverter。
Expand All @@ -30,6 +46,11 @@ public void configureMessageConverters(List<HttpMessageConverter<?>> converters)
converters.add(1, new StringEscapeConverter());
}

/**
* 配置格式化器
* @author Frodez
* @date 2019-05-10
*/
@Override
public void addFormatters(FormatterRegistry registry) {
//对字符串进行转义
Expand All @@ -45,4 +66,27 @@ public String convert(String source) {

}

/**
* 配置异步
* @author Frodez
* @date 2019-05-10
*/
@Override
public void configureAsyncSupport(AsyncSupportConfigurer configurer) {
configurer.setTaskExecutor(asyncConfig.getAsyncExecutor());
configurer.setDefaultTimeout(asyncConfig.getProperties().getTimeout());
configurer.registerCallableInterceptors(new TimeoutCallableProcessingInterceptor());
configurer.registerDeferredResultInterceptors(new TimeoutDeferredResultProcessingInterceptor());
}

/**
* 配置默认媒体类型
* @author Frodez
* @date 2019-05-10
*/
@Override
public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
configurer.defaultContentType(MediaType.APPLICATION_JSON_UTF8);
}

}
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package frodez.config.async;
package frodez.config.mvc.async;

import frodez.util.spring.ContextUtil;
import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

Expand All @@ -19,8 +20,11 @@
@Configuration
public class AsyncConfig {

@Bean
public Executor getAsyncExecutor() {
@Autowired
@Getter
private AsyncProperties properties;

public AsyncTaskExecutor getAsyncExecutor() {
AsyncProperties properties = ContextUtil.get(AsyncProperties.class);
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int availableProcessors = Runtime.getRuntime().availableProcessors();
Expand All @@ -34,8 +38,9 @@ public Executor getAsyncExecutor() {
executor.setThreadNamePrefix(properties.getThreadNamePrefix());
executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
executor.initialize();
log.info("async executor is running now!");
log.info("async config:{}corePoolSize, {}maxPoolSize, {}queueCapacity, {}keepAliveSeconds, {}threadNamePrefix",
log.info("async executor is already now!");
log.info(
"async config:corePoolSize-{}, maxPoolSize-{}, queueCapacity-{}, keepAliveSeconds-{}, threadNamePrefix-{}",
corePoolSize, maxPoolSize, queueCapacity, properties.getKeepAliveSeconds(), properties
.getThreadNamePrefix());
return executor;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frodez.config.async;
package frodez.config.mvc.async;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
Expand Down Expand Up @@ -36,6 +36,11 @@ public class AsyncProperties {
*/
private int keepAliveSeconds = 60;

/**
* 超时时间,单位为毫秒
*/
private long timeout = 30000;

/**
* 线程名前缀
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frodez.config.mvc;
package frodez.config.mvc.converter;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JavaType;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package frodez.config.mvc;
package frodez.config.mvc.converter;

import com.google.common.escape.Escaper;
import com.google.common.html.HtmlEscapers;
Expand Down
14 changes: 14 additions & 0 deletions src/main/java/frodez/config/mvc/error/GlobalController.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import org.springframework.web.bind.ServletRequestBindingException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.context.request.async.AsyncRequestTimeoutException;

/***
* 错误信息处理
Expand Down Expand Up @@ -70,4 +71,17 @@ public void servletRequestBindingExceptionHandler(HttpServletResponse response,
ServletUtil.writeJson(response, Result.errorRequest());
}

/**
* 默认异常处理器
* @param HttpServletResponse 响应
* @param ServletRequestBindingException 异常
* @author Frodez
* @date 2018-12-21
*/
@ExceptionHandler(value = AsyncRequestTimeoutException.class)
public void asyncRequestTimeoutExceptionHandler(HttpServletResponse response, AsyncRequestTimeoutException e) {
log.error("[asyncRequestTimeoutExceptionHandler]{}");
ServletUtil.writeJson(response, Result.busy());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.builders.WebSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.http.SessionCreationPolicy;
Expand Down Expand Up @@ -90,13 +89,6 @@ protected void configure(HttpSecurity http) throws Exception {
.and().addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class).headers().cacheControl(); // 禁止缓存
}

/**
* 静态资源忽略配置
*/
@Override
public void configure(WebSecurity web) throws Exception {
}

/**
* 验证信息获取服务配置
*/
Expand Down
69 changes: 0 additions & 69 deletions src/main/java/frodez/util/aop/AspectUtil.java

This file was deleted.

6 changes: 0 additions & 6 deletions src/main/java/frodez/util/aop/package-info.java

This file was deleted.

1 change: 0 additions & 1 deletion src/main/java/frodez/util/package-info.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/**
* 本包用于存放工具类。<br>
* aop包存放了与AOP相关的工具类。<br>
* beans包存放了通用的java pojo,通用Result,通用分页查询参数,通用分页返回值也位于其中。<br>
* common包存放了通用的工具类。如日期,正则表达式,字符串处理,验证,判空,数字处理。<br>
* constant包存放了项目的公共常量。<br>
Expand Down
2 changes: 2 additions & 0 deletions src/main/resources/settings/dev/async.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ async.max-thread-times=1.0
async.queue-factors=16.0
#线程最长活跃时间,单位为秒
async.keep-alive-seconds=60
#超时时间,单位为毫秒
async.timeout=30000
#线程名前缀
async.thread-name-prefix=async
2 changes: 2 additions & 0 deletions src/main/resources/settings/prod/async.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ async.max-thread-times=1.0
async.queue-factors=16.0
#线程最长活跃时间,单位为秒
async.keep-alive-seconds=60
#超时时间,单位为毫秒
async.timeout=30000
#线程名前缀
async.thread-name-prefix=async
2 changes: 2 additions & 0 deletions src/main/resources/settings/release/async.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ async.max-thread-times=1.0
async.queue-factors=16.0
#线程最长活跃时间,单位为秒
async.keep-alive-seconds=60
#超时时间,单位为毫秒
async.timeout=30000
#线程名前缀
async.thread-name-prefix=async
2 changes: 2 additions & 0 deletions src/main/resources/settings/test/async.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@ async.max-thread-times=1.0
async.queue-factors=16.0
#线程最长活跃时间,单位为秒
async.keep-alive-seconds=60
#超时时间,单位为毫秒
async.timeout=30000
#线程名前缀
async.thread-name-prefix=async

0 comments on commit 4c4301a

Please sign in to comment.