Search in sources :

Example 1 with RpcException

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);
    }
}
Also used : FastClass(org.springframework.cglib.reflect.FastClass) RpcException(com.jim.framework.rpc.exception.RpcException) FastClass(org.springframework.cglib.reflect.FastClass) FastMethod(org.springframework.cglib.reflect.FastMethod) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with RpcException

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;
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) RpcException(com.jim.framework.rpc.exception.RpcException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RpcException(com.jim.framework.rpc.exception.RpcException) IOException(java.io.IOException)

Example 3 with RpcException

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();
    }
}
Also used : LinkedBuffer(io.protostuff.LinkedBuffer) RpcException(com.jim.framework.rpc.exception.RpcException) Schema(io.protostuff.Schema) RuntimeSchema(io.protostuff.runtime.RuntimeSchema) List(java.util.List) RpcException(com.jim.framework.rpc.exception.RpcException) IOException(java.io.IOException)

Example 4 with RpcException

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;
}
Also used : RpcException(com.jim.framework.rpc.exception.RpcException) RpcException(com.jim.framework.rpc.exception.RpcException) IOException(java.io.IOException)

Example 5 with RpcException

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();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ConsulRegistryService(com.jim.framework.rpc.registry.ConsulRegistryService) RpcException(com.jim.framework.rpc.exception.RpcException) RegistryService(com.jim.framework.rpc.registry.RegistryService) ConsulRegistryService(com.jim.framework.rpc.registry.ConsulRegistryService) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) RpcURL(com.jim.framework.rpc.common.RpcURL)

Aggregations

RpcException (com.jim.framework.rpc.exception.RpcException)6 IOException (java.io.IOException)3 LinkedBuffer (io.protostuff.LinkedBuffer)2 Schema (io.protostuff.Schema)2 RuntimeSchema (io.protostuff.runtime.RuntimeSchema)2 RpcURL (com.jim.framework.rpc.common.RpcURL)1 ConsulRegistryService (com.jim.framework.rpc.registry.ConsulRegistryService)1 RegistryService (com.jim.framework.rpc.registry.RegistryService)1 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)1 ChannelFuture (io.netty.channel.ChannelFuture)1 EventLoopGroup (io.netty.channel.EventLoopGroup)1 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 List (java.util.List)1 FastClass (org.springframework.cglib.reflect.FastClass)1 FastMethod (org.springframework.cglib.reflect.FastMethod)1