JSON

请求路由到业务方法设计(2)-我们到底能走多远系列(45)(2)

字号+ 作者:H5之家 来源:H5之家 2015-11-12 18:31 我要评论( )

OpenServiceContext用来实际启动时将那些注解的类全部解析成路由Map,也算核心代码,上面提到的ROP框架也是一样的做法,值得借鉴,以后自己想写个小框架可以用用。 public class OpenServiceContext{ private MapSt

OpenServiceContext用来实际启动时将那些注解的类全部解析成路由Map,也算核心代码,上面提到的ROP框架也是一样的做法,值得借鉴,以后自己想写个小框架可以用用。

public class OpenServiceContext{ private Map<String, ServiceMethodHandler> handlerMap = new HashMap<String, ServiceMethodHandler>(); private Set<String> methodNameSet = new HashSet<String>(); private ApplicationContext applicationContext; public OpenServiceContext(){ initContext( this. applicationContext); } public OpenServiceContext( final ApplicationContext context){ initContext(context); } public void addServiceMethodHandler(String methodName, ServiceMethodHandler serviceMethodHandler){ methodNameSet.add(methodName); handlerMap.put(methodName, serviceMethodHandler); } public boolean isValidMethod(String methodName){ return methodNameSet.contains(methodName); } public Map<String, ServiceMethodHandler> getHandlerMap(){ return handlerMap; } initContext( final ApplicationContext context){ String[] beanNames = context.getBeanNamesForType(Object.class ); if(beanNames == null){ return; } for (final String beanName : beanNames) { Class<?> handlerType = context.getType(beanName); // 本方法是最后一个参数根据注解直接过滤出来的method,放入Map ReflectionUtils. doWithMethods(handlerType, new ReflectionUtils.MethodCallback() { public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException { ReflectionUtils.makeAccessible(method); OpenServiceMethod serviceMethod = method.getAnnotation(OpenServiceMethod .class); ServiceMethodHandler serviceMethodHandler = new ServiceMethodHandler(); //1.set handler serviceMethodHandler.setHandler(context.getBean(beanName)); //handler serviceMethodHandler.setHandlerMethod(method); (!ClassUtils. isAssignable(OpenResponse.class, method.getReturnType())){ throw new OpenServiceException(method.getDeclaringClass().getName() + "." + method.getName() + "的返回参数必须是" + OpenResponse.class.getName()); } OpenServiceException(method.getDeclaringClass().getName() + "." + method.getName() + "的入参只能是" + OpenRequest.class.getName() + "或无入参。" ); } else if (method.getParameterTypes().length == 1) { Class<?> paramType = method.getParameterTypes()[0]; if (!ClassUtils.isAssignable(OpenRequest. class, paramType)) { throw new OpenServiceException(method.getDeclaringClass().getName() + "." + method.getName() + "的入参必须是" + OpenRequest.class.getName()); } serviceMethodHandler.setRequestType((Class<? extends OpenRequest>)paramType); } else { throw new OpenServiceException(method.getDeclaringClass().getName() + "." + method.getName() + "无入参" ); } addServiceMethodHandler(serviceMethod.method(), serviceMethodHandler); } }, new ReflectionUtils.MethodFilter() { @Override public boolean matches(Method method) { return AnnotationUtils.findAnnotation(method, OpenServiceMethod. class) != null; } } ); } } }

 

接下来就是执行器来执行指定逻辑代码了,因为Map中放的是Method,执行需要进行反射:

public class AbstractBaseServiceAdapter { /** * json ==> OpenRequest * @param json * @param handler * OpenRequest decode(JSON json, ServiceMethodHandler handler){ Class<? extends OpenRequest> requestClass = handler.getRequestType(); ObjectMapper mapper = new ObjectMapper(); OpenRequest request = null; try { request = mapper.readValue(json.toJSONString(), requestClass); } catch (Exception e) { throw new OpenServiceException( "open decode had a exp json==>"+json , e); } return request; } /** * OpenResponse ==> json * @param response * JSON encode(OpenResponse response){ ObjectMapper mapper = new ObjectMapper(); // Convert object to JSON string JSON json = null; try { String j = mapper.writeValueAsString(response); json = JSON. parseObject(j); } catch (Exception e) { throw new OpenServiceException( "open encode had a exp response==>"+response.getClass() , e); } return json; } public final JSON execute(JSON json, ServiceMethodHandler handler) { OpenRequest request = this.decode(json, handler); handler.getHandler(); handler.getHandlerMethod(); OpenResponse response = null; try { // 执行器执行对应方法 response = (OpenResponse) handler.getHandlerMethod().invoke( handler.getHandler(), request); } catch (Exception e) { throw new OpenServiceException( "open invoke had a exp json"+json , e); } JSON retrunJson = this.encode(response); return retrunJson; } }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评