package com.bluedavy.rpc.server;
/**
* nfs-rpc
* Apache License
*
* http://code.google.com/p/nfs-rpc (c) 2011
*/
import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.bluedavy.rpc.Coders;
import com.bluedavy.rpc.RequestWrapper;
import com.bluedavy.rpc.ResponseWrapper;
/**
* MethodHandle RPC Server Handler
*
* @author <a href="mailto:bluedavy@gmail.com">bluedavy</a>
*/
public class RPCJava7ServerHandler implements ServerHandler {
// Server Processors key: servicename value: service instance
private static Map<String, Object> processors = new HashMap<String, Object>();
// Cached Server Methods key: instanceName#methodname$argtype_argtype
private static Map<String, MethodHandle> cachedMethodHandles = new HashMap<String, MethodHandle>();
/* (non-Javadoc)
* @see com.bluedavy.rpc.ServerHandler#registerProcessor(java.lang.String, java.lang.Object)
*/
@Override
public void registerProcessor(String instanceName,Object instance){
processors.put(instanceName, instance);
Class<?> instanceClass = instance.getClass();
Method[] methods = instanceClass.getMethods();
for (Method method : methods) {
Class<?>[] argTypes = method.getParameterTypes();
StringBuilder methodKeyBuilder = new StringBuilder();
methodKeyBuilder.append(instanceName).append("#");
methodKeyBuilder.append(method.getName()).append("$");
for (Class<?> argClass : argTypes) {
methodKeyBuilder.append(argClass.getName()).append("_");
}
MethodType methodType = MethodType.methodType(method.getReturnType(), argTypes);
try{
MethodHandle methodHandle = MethodHandles.lookup().findVirtual(instance.getClass(), method.getName(), methodType);
cachedMethodHandles.put(methodKeyBuilder.toString(), methodHandle);
}
catch(Exception e){
e.printStackTrace();
}
}
}
/* (non-Javadoc)
* @see com.bluedavy.rpc.ServerHandler#handleRequest(com.bluedavy.rpc.RequestWrapper)
*/
@Override
public ResponseWrapper handleRequest(final RequestWrapper request){
ResponseWrapper responseWrapper = new ResponseWrapper();
responseWrapper.setRequestId(request.getId());
responseWrapper.setDataType(request.getDataType());
String targetInstanceName = request.getTargetInstanceName();
String methodName = request.getMethodName();
String[] argTypes = request.getArgTypes();
List<Object> requestObjects = null;
MethodHandle method = null;
try{
Object processor = processors.get(targetInstanceName);
requestObjects = new ArrayList<Object>(argTypes.length+1);
requestObjects.add(processor);
if(processor == null){
throw new Exception("no "+targetInstanceName+" instance exists on the server");
}
if (argTypes != null && argTypes.length > 0) {
StringBuilder methodKeyBuilder = new StringBuilder();
methodKeyBuilder.append(targetInstanceName).append("#");
methodKeyBuilder.append(methodName).append("$");
Class<?>[] argTypeClasses = new Class<?>[argTypes.length];
for (int i = 0; i < argTypes.length; i++) {
methodKeyBuilder.append(argTypes[i]).append("_");
argTypeClasses[i] = Class.forName(argTypes[i]);
}
String key = methodKeyBuilder.toString();
method = cachedMethodHandles.get(key);
Object[] tmprequestObjects = request.getRequestObjects();
for (int i = 0; i < tmprequestObjects.length; i++) {
requestObjects.add(Coders.getDecoder(String.valueOf(request.getDataType())).decode((byte[])tmprequestObjects[i]));
}
}
else {
MethodType methodType = MethodType.methodType(Object.class, new Class<?>[]{});
method = MethodHandles.lookup().findVirtual(processor.getClass(), methodName, methodType);
}
responseWrapper.setResponse(method.invokeWithArguments(requestObjects));
}
catch(Throwable e){
responseWrapper.setException(e);
}
return responseWrapper;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
基于java的开发源码-高性能RPC框架 nfs-rpc.zip
共235个文件
svn-base:116个
java:98个
sh:8个
1 下载量 15 浏览量
2024-02-25
10:17:30
上传
评论
收藏 363KB ZIP 举报
温馨提示
labview 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip 基于java的开发源码-高性能RPC框架 nfs-rpc.zip
资源推荐
资源详情
资源评论
收起资源包目录
基于java的开发源码-高性能RPC框架 nfs-rpc.zip (235个子文件)
wc.db 205KB
entries 3B
format 3B
PB.java 28KB
RPCProtocol.java 11KB
AbstractBenchmarkClient.java 10KB
SimpleProcessorBenchmarkClientRunnable.java 8KB
AbstractClient.java 7KB
RPCBenchmarkClientRunnable.java 7KB
SimpleProcessorProtocol.java 6KB
NettyServerHandler.java 5KB
GrizzlyServerHandler.java 5KB
MinaServerHandler.java 5KB
MinaServerHandler.java 5KB
AbstractClientFactory.java 4KB
RPCServerHandler.java 4KB
NettyProtocolDecoder.java 4KB
MinaClientFactory.java 3KB
AbstractBenchmarkServer.java 3KB
MinaClientFactory.java 3KB
MinaClient.java 3KB
GrizzlyProtocolFilter.java 3KB
MinaClient.java 3KB
NettyClientFactory.java 3KB
NettyClient.java 3KB
NettyServer.java 3KB
AbstractClientInvocationHandler.java 3KB
GrizzlyServer.java 3KB
NettyClientHandler.java 3KB
GrizzlyClientFactory.java 3KB
MinaServer.java 3KB
MinaServer.java 2KB
RequestWrapper.java 2KB
MinaClientProcessor.java 2KB
MinaClientProcessor.java 2KB
Codecs.java 2KB
Client.java 2KB
ProtocolFactory.java 2KB
GrizzlyClient.java 2KB
ProtocolUtils.java 2KB
AbstractRPCBenchmarkClient.java 2KB
MinaProtocolCodecFilter.java 2KB
ClientFactory.java 2KB
MinaProtocolCodecFilter.java 2KB
SimpleProcessorServerHandler.java 2KB
GrizzlyByteBufferWrapper.java 2KB
GrizzlyClientHandler.java 2KB
NettyByteBufferWrapper.java 2KB
GrizzlySimpleBenchmarkClient.java 1KB
ResponseWrapper.java 1KB
MinaByteBufferWrapper.java 1KB
MinaByteBufferWrapper.java 1KB
NamedThreadFactory.java 1KB
KryoUtils.java 1KB
GrizzlyRPCBenchmarkClient.java 1KB
NettyRPCBenchmarkClient.java 1KB
MinaRPCBenchmarkClient.java 1KB
MinaRPCBenchmarkClient.java 1KB
NettyClientPipelineFactory.java 1KB
ResponseObjectSerializer.java 1015B
RequestObjectSerializer.java 999B
GrizzlyClientInvocationHandler.java 994B
NettyClientInvocationHandler.java 989B
AbstractSimpleProcessorBenchmarkClient.java 982B
MinaClientInvocationHandler.java 976B
MinaClientInvocationHandler.java 975B
MinaProtocolDecoder.java 943B
MinaProtocolDecoder.java 936B
PBBenchmarkTestServiceImpl.java 934B
Protocol.java 827B
MinaProtocolEncoder.java 825B
MinaProtocolEncoder.java 818B
NettyProtocolEncoder.java 798B
PBDecoder.java 794B
GrizzlyBenchmarkServer.java 787B
BenchmarkTestServiceImpl.java 763B
ByteBufferWrapper.java 762B
NettySimpleBenchmarkClient.java 756B
MinaSimpleBenchmarkClient.java 742B
MinaSimpleBenchmarkClient.java 740B
Server.java 707B
AppTest.java 685B
HessianEncoder.java 685B
HessianDecoder.java 680B
PBEncoder.java 665B
NettyBenchmarkServer.java 659B
JavaEncoder.java 657B
ServerHandler.java 653B
MinaBenchmarkServer.java 652B
MinaBenchmarkServer.java 650B
JavaDecoder.java 614B
ResponseObject.java 563B
RequestObject.java 560B
KryoDecoder.java 547B
KryoEncoder.java 544B
ServerProcessor.java 479B
SendLimitPolicy.java 440B
BenchmarkTestService.java 398B
Decoder.java 384B
ClientRunnable.java 384B
共 235 条
- 1
- 2
- 3
资源评论
快乐无限出发
- 粉丝: 1200
- 资源: 7394
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功