use of com.jim.framework.rpc.exception.RpcException in project jim-framework by jiangmin168168.
the class RpcServerInvoker method invoke.
@Override
public RpcResponse invoke(RpcInvocation invocation) {
String className = invocation.getClassName();
Object serviceBean = handlerMap.get(className);
Class<?> serviceClass = serviceBean.getClass();
String methodName = invocation.getMethodName();
Class<?>[] parameterTypes = invocation.getParameterTypes();
Object[] parameters = invocation.getParameters();
FastClass serviceFastClass = FastClass.create(serviceClass);
FastMethod serviceFastMethod = serviceFastClass.getMethod(methodName, parameterTypes);
try {
Object result = serviceFastMethod.invoke(serviceBean, parameters);
RpcResponse rpcResponse = new RpcResponse();
rpcResponse.setResult(result);
rpcResponse.setRequestId(invocation.getRequestId());
return rpcResponse;
} catch (InvocationTargetException e) {
throw new RpcException(e);
}
}
use of com.jim.framework.rpc.exception.RpcException in project jim-framework by jiangmin168168.
the class ProtoStuffSerializeUtil method serializeList.
private static <T> byte[] serializeList(List<T> objs) {
if (null == objs || objs.size() == 0) {
return null;
}
Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(objs.get(0).getClass());
LinkedBuffer buffer = LinkedBuffer.allocate();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
byte[] protostuff = null;
try {
ProtostuffIOUtil.writeListTo(byteArrayOutputStream, objs, schema, buffer);
protostuff = byteArrayOutputStream.toByteArray();
} catch (Exception e) {
throw new RpcException(e);
} finally {
buffer.clear();
try {
byteArrayOutputStream.close();
} catch (IOException e) {
logger.info("ByteArrayOutputStream close error:", e);
}
}
return protostuff;
}
use of com.jim.framework.rpc.exception.RpcException in project jim-framework by jiangmin168168.
the class ProtoStuffSerializeUtil method serialize.
public static <T> byte[] serialize(T obj) {
if (obj == null) {
throw new RpcException("ProtoStuffSerialize.serialize: obj is null");
}
if (obj instanceof List) {
return serializeList((List) obj);
}
Schema<T> schema = (Schema<T>) RuntimeSchema.getSchema(obj.getClass());
LinkedBuffer buffer = LinkedBuffer.allocate();
try {
return ProtostuffIOUtil.toByteArray(obj, schema, buffer);
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
buffer.clear();
}
}
use of com.jim.framework.rpc.exception.RpcException in project jim-framework by jiangmin168168.
the class ProtoStuffSerializeUtil method deserialize.
public static <T> T deserialize(byte[] bytes, Class<T> beanClass) {
if (bytes == null || bytes.length == 0) {
return null;
}
T instance = null;
try {
instance = beanClass.newInstance();
} catch (Exception e) {
throw new RpcException(e);
}
Schema<T> schema = RuntimeSchema.getSchema(beanClass);
ProtostuffIOUtil.mergeFrom(bytes, instance, schema);
return instance;
}
use of com.jim.framework.rpc.exception.RpcException in project jim-framework by jiangmin168168.
the class RpcServer method bind.
public void bind(ServiceConfig serviceConfig) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).childHandler(this.rpcServerInitializer).childOption(ChannelOption.SO_KEEPALIVE, true);
try {
ChannelFuture channelFuture = bootstrap.bind(serviceConfig.getHost(), serviceConfig.getPort()).sync();
RpcURL url = new RpcURL();
url.setHost(serviceConfig.getHost());
url.setPort(serviceConfig.getPort());
url.setRegistryHost(serviceConfig.getRegistryHost());
url.setRegistryPort(serviceConfig.getRegistryPort());
RegistryService registryService = new ConsulRegistryService();
registryService.register(url);
channelFuture.channel().closeFuture().sync();
} catch (InterruptedException e) {
throw new RpcException(e);
}
} finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
Aggregations