已合并
!226 优化

iBase4J:developiBase4J:master

A圣旭 创建于: 2017-10-12 15:11
728904_ibase2jA圣旭 合并于 2017-10-12 15:11

0 条评论, 1 人参与 728904_ibase2j

登录 后才可以发表评论

2017-10-12
1 个提交记录
-/**
- *
- */
-package org.ibase4j.core.base;
-
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Map;
-
-import javax.servlet.http.HttpServletRequest;
-import javax.servlet.http.HttpServletResponse;
-
-import org.apache.commons.lang3.StringUtils;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.Logger;
-import org.apache.shiro.authz.UnauthorizedException;
-import org.ibase4j.core.Constants;
-import org.ibase4j.core.exception.BaseException;
-import org.ibase4j.core.exception.IllegalParameterException;
-import org.ibase4j.core.support.DateFormat;
-import org.ibase4j.core.support.HttpCode;
-import org.ibase4j.core.util.InstanceUtil;
-import org.ibase4j.core.util.WebUtil;
-import org.springframework.beans.propertyeditors.CustomDateEditor;
-import org.springframework.http.ResponseEntity;
-import org.springframework.ui.ModelMap;
-import org.springframework.web.bind.WebDataBinder;
-import org.springframework.web.bind.annotation.ExceptionHandler;
-import org.springframework.web.bind.annotation.InitBinder;
-
-import com.alibaba.fastjson.JSON;
-import com.alibaba.fastjson.serializer.SerializerFeature;
-import com.baomidou.mybatisplus.plugins.Page;
-
-/**
- * 控制器基类
- *
- * @author ShenHuaJie
- * @version 2016年5月20日 下午3:47:58
- */
-public abstract class BaseController {
- protected final Logger logger = LogManager.getLogger(this.getClass());
-
- /** 获取当前用户Id */
- protected Long getCurrUser() {
- return WebUtil.getCurrentUser();
- }
-
- @InitBinder
- protected void initBinder(WebDataBinder binder) {
- binder.registerCustomEditor(Date.class, new CustomDateEditor(new DateFormat(), true));
- }
-
- /** 设置成功响应代码 */
- protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap) {
- return setSuccessModelMap(modelMap, null);
- }
-
- /** 设置成功响应代码 */
- protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap, Object data) {
- return setModelMap(modelMap, HttpCode.OK, data);
- }
-
- /** 设置响应代码 */
- protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code) {
- return setModelMap(modelMap, code, null);
- }
-
- /** 设置响应代码 */
- protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data) {
- Map<String, Object> map = InstanceUtil.newLinkedHashMap();
- map.putAll(modelMap);
- modelMap.clear();
- for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
- String key = iterator.next();
- if (!key.startsWith("org.springframework.validation.BindingResult") && !key.equals("void")) {
- modelMap.put(key, map.get(key));
- }
- }
- if (data != null) {
- if (data instanceof Page) {
- Page<?> page = (Page<?>) data;
- modelMap.put("data", page.getRecords());
- modelMap.put("current", page.getCurrent());
- modelMap.put("size", page.getSize());
- modelMap.put("pages", page.getPages());
- modelMap.put("total", page.getTotal());
- modelMap.put("iTotalRecords", page.getTotal());
- modelMap.put("iTotalDisplayRecords", page.getTotal());
- } else if (data instanceof List<?>) {
- modelMap.put("data", data);
- modelMap.put("iTotalRecords", ((List<?>) data).size());
- modelMap.put("iTotalDisplayRecords", ((List<?>) data).size());
- } else {
- modelMap.put("data", data);
- }
- }
- modelMap.put("httpCode", code.value());
- modelMap.put("msg", code.msg());
- modelMap.put("timestamp", System.currentTimeMillis());
- logger.info("RESPONSE : " + JSON.toJSONString(modelMap));
- return ResponseEntity.ok(modelMap);
- }
-
- /** 异常处理 */
- @ExceptionHandler(Exception.class)
- public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
- throws Exception {
- logger.error(Constants.Exception_Head, ex);
- ModelMap modelMap = new ModelMap();
- if (ex instanceof BaseException) {
- ((BaseException) ex).handler(modelMap);
- } else if (ex instanceof IllegalArgumentException) {
- new IllegalParameterException(ex.getMessage()).handler(modelMap);
- } else if (ex instanceof UnauthorizedException) {
- modelMap.put("httpCode", HttpCode.FORBIDDEN.value());
- modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
- } else {
- modelMap.put("httpCode", HttpCode.INTERNAL_SERVER_ERROR.value());
- String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
- modelMap.put("msg", msg.length() > 100 ? "系统走神了,请稍候再试." : msg);
- }
- response.setContentType("application/json;charset=UTF-8");
- modelMap.put("timestamp", System.currentTimeMillis());
- logger.info("RESPONSE : " + JSON.toJSON(modelMap));
- byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
- response.getOutputStream().write(bytes);
- }
-}
+/**
+ *
+ */
+package org.ibase4j.core.base;
+
+import java.util.Date;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.Logger;
+import org.apache.shiro.authz.UnauthorizedException;
+import org.ibase4j.core.Constants;
+import org.ibase4j.core.exception.BaseException;
+import org.ibase4j.core.exception.IllegalParameterException;
+import org.ibase4j.core.support.DateFormat;
+import org.ibase4j.core.support.HttpCode;
+import org.ibase4j.core.util.InstanceUtil;
+import org.ibase4j.core.util.WebUtil;
+import org.springframework.beans.propertyeditors.CustomDateEditor;
+import org.springframework.http.ResponseEntity;
+import org.springframework.ui.ModelMap;
+import org.springframework.web.bind.WebDataBinder;
+import org.springframework.web.bind.annotation.ExceptionHandler;
+import org.springframework.web.bind.annotation.InitBinder;
+
+import com.alibaba.fastjson.JSON;
+import com.alibaba.fastjson.serializer.SerializerFeature;
+import com.baomidou.mybatisplus.plugins.Page;
+
+/**
+ * 控制器基类
+ *
+ * @author ShenHuaJie
+ * @version 2016年5月20日 下午3:47:58
+ */
+public abstract class BaseController {
+ protected final Logger logger = LogManager.getLogger(this.getClass());
+
+ /** 获取当前用户Id */
+ protected Long getCurrUser() {
+ return WebUtil.getCurrentUser();
+ }
+
+ @InitBinder
+ protected void initBinder(WebDataBinder binder) {
+ binder.registerCustomEditor(Date.class, new CustomDateEditor(new DateFormat(), true));
+ }
+
+ /** 设置成功响应代码 */
+ protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap) {
+ return setSuccessModelMap(modelMap, null);
+ }
+
+ /** 设置成功响应代码 */
+ protected ResponseEntity<ModelMap> setSuccessModelMap(ModelMap modelMap, Object data) {
+ return setModelMap(modelMap, HttpCode.OK, data);
+ }
+
+ /** 设置响应代码 */
+ protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code) {
+ return setModelMap(modelMap, code, null);
+ }
+
+ /** 设置响应代码 */
+ protected ResponseEntity<ModelMap> setModelMap(ModelMap modelMap, HttpCode code, Object data) {
+ Map<String, Object> map = InstanceUtil.newLinkedHashMap();
+ map.putAll(modelMap);
+ modelMap.clear();
+ for (Iterator<String> iterator = map.keySet().iterator(); iterator.hasNext();) {
+ String key = iterator.next();
+ if (!key.startsWith("org.springframework.validation.BindingResult") && !key.equals("void")) {
+ modelMap.put(key, map.get(key));
+ }
+ }
+ if (data != null) {
+ if (data instanceof Page) {
+ Page<?> page = (Page<?>) data;
+ modelMap.put("data", page.getRecords());
+ modelMap.put("current", page.getCurrent());
+ modelMap.put("size", page.getSize());
+ modelMap.put("pages", page.getPages());
+ modelMap.put("total", page.getTotal());
+ modelMap.put("iTotalRecords", page.getTotal());
+ modelMap.put("iTotalDisplayRecords", page.getTotal());
+ } else if (data instanceof List<?>) {
+ modelMap.put("data", data);
+ modelMap.put("iTotalRecords", ((List<?>) data).size());
+ modelMap.put("iTotalDisplayRecords", ((List<?>) data).size());
+ } else {
+ modelMap.put("data", data);
+ }
+ }
+ modelMap.put("httpCode", code.value());
+ modelMap.put("msg", code.msg());
+ modelMap.put("timestamp", System.currentTimeMillis());
+ logger.info("RESPONSE : " + JSON.toJSONString(modelMap));
+ return ResponseEntity.ok(modelMap);
+ }
+
+ /** 异常处理 */
+ @ExceptionHandler(Exception.class)
+ public void exceptionHandler(HttpServletRequest request, HttpServletResponse response, Exception ex)
+ throws Exception {
+ logger.error(Constants.Exception_Head, ex);
+ ModelMap modelMap = new ModelMap();
+ if (ex instanceof BaseException) {
+ ((BaseException) ex).handler(modelMap);
+ } else if (ex instanceof IllegalArgumentException) {
+ new IllegalParameterException(ex.getMessage()).handler(modelMap);
+ } else if (ex instanceof UnauthorizedException) {
+ modelMap.put("httpCode", HttpCode.FORBIDDEN.value().toString());
+ modelMap.put("msg", StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.FORBIDDEN.msg()));
+ } else {
+ modelMap.put("httpCode", HttpCode.INTERNAL_SERVER_ERROR.value().toString());
+ String msg = StringUtils.defaultIfBlank(ex.getMessage(), HttpCode.INTERNAL_SERVER_ERROR.msg());
+ modelMap.put("msg", msg.length() > 100 ? "系统走神了,请稍候再试." : msg);
+ }
+ response.setContentType("application/json;charset=UTF-8");
+ modelMap.put("timestamp", System.currentTimeMillis());
+ logger.info("RESPONSE : " + JSON.toJSON(modelMap));
+ byte[] bytes = JSON.toJSONBytes(modelMap, SerializerFeature.DisableCircularReferenceDetect);
+ response.getOutputStream().write(bytes);
+ }
+}
@@ -9,9 +9,10 @@ rpc.protocol.threads=100
rpc.protocol.maxThread=500
rpc.protocol.minThread=20
rpc.cache.dir=/output/cache/
-rpc.timeout=20000
+rpc.connect.timeout=20000
+rpc.request.timeout=20000
#\u5931\u8d25\u91cd\u8bd5\u6b21\u6570
-rpc.consumer.retries=2
+rpc.consumer.retries=0
#
redis.host=127.0.0.1
redis.port=6379
@@ -14,12 +14,13 @@
<!-- 提供方应用名称信息,这个相当于起一个名字,我们dubbo管理页面比较清晰是哪个应用暴露出来的 -->
<dubbo:application name="${rpc.registry.name}" logger="slf4j" />
<!-- 使用zookeeper注册中心暴露服务地址 -->
- <dubbo:registry address="${rpc.registry.address}" check="false" timeout="${rpc.timeout}"
+ <dubbo:registry address="${rpc.registry.address}" check="false" timeout="${rpc.connect.timeout}"
file="${rpc.cache.dir}/dubbo-${rpc.registry.name}.cache" />
<!-- 用dubbo协议在20880端口暴露服务 -->
<dubbo:protocol name="dubbo" port="${rpc.protocol.port}" threads="${rpc.protocol.threads}" />
<!-- 服务端要暴露的服务接口,注解@com.alibaba.dubbo.config.annotation.Service -->
<dubbo:annotation package="org.ibase4j" />
<!-- 服务消费端配置 -->
- <dubbo:consumer timeout="${rpc.timeout}" loadbalance="leastactive" check="false" retries="${rpc.consumer.retries}" />
+ <dubbo:consumer timeout="${rpc.timeout}" loadbalance="leastactive" check="false"
+ timeout="${rpc.request.timeout}" retries="${rpc.consumer.retries}" />
</beans>
\ No newline at end of file
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:motan="http://api.weibo.com/schema/motan"
- xmlns:p="http://www.springframework.org/schema/p"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://api.weibo.com/schema/motan
- http://api.weibo.com/schema/motan.xsd">
-
- <!-- 注册中心的配置 -->
- <motan:registry regProtocol="${rpc.type}" name="rpc_registry"
- address="${rpc.address}" connectTimeout="${rpc.timeout}" />
-
- <!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。 -->
- <motan:protocol id="motan" name="motan" default="true"
- maxServerConnection="80000" maxContentLength="1048576"
- maxWorkerThread="${rpc.protocol.maxThread}" minWorkerThread="${rpc.protocol.minThread}" />
- <!-- 服务的默认配置 -->
- <motan:basicService id="motanServiceBasicConfig" shareChannel="true"
- registry="rpc_registry" export="motan:${rpc.protocol.port}" />
- <!-- 注解服务端要暴露的服务接口 -->
- <motan:annotation />
- <!-- 通用referer基础配置 -->
- <motan:basicReferer id="motanClientBasicConfig"
- registry="rpc_registry" protocol="motan" requestTimeout="2000"
- retries="${rpc.consumer.retries}" throwException="false" check="true" />
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns="http://www.springframework.org/schema/beans"
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:motan="http://api.weibo.com/schema/motan"
+ xmlns:p="http://www.springframework.org/schema/p"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
+ http://api.weibo.com/schema/motan
+ http://api.weibo.com/schema/motan.xsd">
+
+ <!-- 注册中心的配置 -->
+ <motan:registry regProtocol="${rpc.type}" name="rpc_registry"
+ address="${rpc.address}" connectTimeout="${rpc.connect.timeout}" />
+
+ <!-- 协议配置。为防止多个业务配置冲突,推荐使用id表示具体协议。 -->
+ <motan:protocol id="motan" name="motan" default="true"
+ timeout="${rpc.request.timeout}" maxServerConnection="80000"
+ maxContentLength="1048576" maxWorkerThread="${rpc.protocol.maxThread}"
+ minWorkerThread="${rpc.protocol.minThread}" />
+ <!-- 服务的默认配置 -->
+ <motan:basicService id="motanServiceBasicConfig"
+ shareChannel="true" registry="rpc_registry" export="motan:${rpc.protocol.port}" />
+ <!-- 注解服务端要暴露的服务接口 -->
+ <motan:annotation />
+ <!-- 通用referer基础配置 -->
+ <motan:basicReferer id="motanClientBasicConfig"
+ registry="rpc_registry" protocol="motan" throwException="false"
+ retries="${rpc.consumer.retries}" check="true" />
</beans>
\ No newline at end of file
-<?xml version="1.0" encoding="UTF-8"?>
-<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
- xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-4.0.xsd">
- <!-- jedis 配置-->
- <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
- <!--最小空闲数-->
- <property name="minIdle" value="${redis.minIdle}" />
- <!--最大空闲数-->
- <property name="maxIdle" value="${redis.maxIdle}" />
- <!--最大连接数-->
- <property name="maxTotal" value="${redis.maxTotal}" />
- <!--最大建立连接等待时间-->
- <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
- <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
- <property name="testOnBorrow" value="${redis.testOnBorrow}" />
- </bean >
- <!-- redisCluster配置 -->
- <!-- <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
- <constructor-arg name="propertySource">
- <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource"
- c:name="redis.properties" c:resource="classpath*:config/system.properties"/>
- </bean>
- </constructor-arg>
- </bean>
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- c:cluster-config-ref="redisClusterConfiguration" c:pool-config-ref="jedisPoolConfig" p:timeout="${redis.timeout}"
- p:password="${redis.password}" /> -->
- <!-- redis服务器中心 -->
- <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
- p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
- p:pool-config-ref="jedisPoolConfig" p:timeout="${redis.timeout}" />
- <!-- 缓存序列化方式 -->
- <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
- <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
- <!-- 缓存 -->
- <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
- <property name="connectionFactory" ref="jedisConnectionFactory" />
- <property name="enableTransactionSupport" value="true" />
- <property name="keySerializer" ref="keySerializer" />
- <property name="valueSerializer" ref="valueSerializer" />
- <property name="hashKeySerializer" ref="keySerializer" />
- <property name="hashValueSerializer" ref="valueSerializer" />
- </bean>
- <bean class="org.ibase4j.core.support.cache.RedisHelper" >
- <property name="redisTemplate" ref="redisTemplate" />
- </bean>
- <!-- 缓存管理 -->
- <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
- <constructor-arg index="0" ref="redisTemplate" />
- <property name="defaultExpiration" value="${redis.expiration}" />
- </bean>
+<?xml version="1.0" encoding="UTF-8"?>
+<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xmlns:p="http://www.springframework.org/schema/p" xmlns:c="http://www.springframework.org/schema/c"
+ xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
+ xsi:schemaLocation="http://www.springframework.org/schema/beans
+ http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
+ http://www.springframework.org/schema/context
+ http://www.springframework.org/schema/context/spring-context-4.0.xsd">
+ <!-- jedis 配置-->
+ <bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig" >
+ <!--最小空闲数-->
+ <property name="minIdle" value="${redis.minIdle}" />
+ <!--最大空闲数-->
+ <property name="maxIdle" value="${redis.maxIdle}" />
+ <!--最大连接数-->
+ <property name="maxTotal" value="${redis.maxTotal}" />
+ <!--最大建立连接等待时间-->
+ <property name="maxWaitMillis" value="${redis.maxWaitMillis}" />
+ <!--是否在从池中取出连接前进行检验,如果检验失败,则从池中去除连接并尝试取出另一个-->
+ <property name="testOnBorrow" value="${redis.testOnBorrow}" />
+ </bean >
+ <!-- redisCluster配置 -->
+ <!-- <bean id="redisClusterConfiguration" class="org.springframework.data.redis.connection.RedisClusterConfiguration">
+ <constructor-arg name="propertySource">
+ <bean id="resourcePropertySource" class="org.springframework.core.io.support.ResourcePropertySource"
+ c:name="redis.properties" c:resource="classpath*:config/system.properties"/>
+ </bean>
+ </constructor-arg>
+ </bean>
+ <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
+ c:cluster-config-ref="redisClusterConfiguration" c:pool-config-ref="jedisPoolConfig" p:timeout="${redis.timeout}"
+ p:password="${redis.password}" /> -->
+ <!-- redis服务器中心 -->
+ <bean id="jedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
+ p:host-name="${redis.host}" p:port="${redis.port}" p:password="${redis.password}"
+ p:pool-config-ref="jedisPoolConfig" p:timeout="${redis.timeout}" />
+ <!-- 缓存序列化方式 -->
+ <bean id="keySerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer" />
+ <bean id="valueSerializer" class="org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer" />
+ <!-- 缓存 -->
+ <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
+ <property name="connectionFactory" ref="jedisConnectionFactory" />
+ <property name="enableTransactionSupport" value="true" />
+ <property name="keySerializer" ref="keySerializer" />
+ <property name="valueSerializer" ref="valueSerializer" />
+ <property name="hashKeySerializer" ref="keySerializer" />
+ <property name="hashValueSerializer" ref="valueSerializer" />
+ </bean>
+ <bean class="org.ibase4j.core.support.cache.RedisHelper" >
+ <property name="redisTemplate" ref="redisTemplate" />
+ </bean>
+ <!-- 缓存管理 -->
+ <bean id="redisCacheManager" class="org.springframework.data.redis.cache.RedisCacheManager">
+ <constructor-arg index="0" ref="redisTemplate" />
+ <property name="transactionAware" value="true" />
+ <property name="defaultExpiration" value="${redis.expiration}" />
+ </bean>
</beans>
\ No newline at end of file