com.alipay.remoting.rpc.ResponseCommand Java Examples

The following examples show how to use com.alipay.remoting.rpc.ResponseCommand. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ExceptionStringCustomSerializer.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/** 
 * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    deserialFlag.set(true);
    if (deserialRuntimeException) {
        throw new RuntimeException(
            "deserialRuntimeException in ExceptionStringCustomSerializer!");
    } else if (deserialException) {
        throw new DeserializationException(
            "deserialException in ExceptionStringCustomSerializer!");
    } else {
        return false;// use default codec 
    }
}
 
Example #2
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean serializeContent(Response response)
    throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializerCode = response.getSerializer();
        try {
            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializerCode);
            AbstractByteBuf byteBuf = rpcSerializer.encode(responseCommand.getResponseObject(), null);
            responseCommand.setContent(byteBuf.array());
            return true;
        } catch (Exception ex) {
            throw new SerializationException(ex.getMessage(), ex);
        } finally {
            recordSerializeResponse(responseCommand);
        }
    }
    return false;
}
 
Example #3
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean serializeHeader(Response response)
    throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();

        Object responseObject = ((RpcResponseCommand) response).getResponseObject();
        if (responseObject instanceof SofaResponse) {
            SofaResponse sofaResponse = (SofaResponse) responseObject;
            if (sofaResponse.isError() || sofaResponse.getAppResponse() instanceof Throwable) {
                sofaResponse.addResponseProp(RemotingConstants.HEAD_RESPONSE_ERROR, StringUtils.TRUE);
            }
            response.setHeader(mapSerializer.encode(sofaResponse.getResponseProps()));
        }
        return true;
    }
    return false;
}
 
Example #4
Source File: CustomHeaderSerializer.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeHeader(T response,
                                                             InvokeContext invokeContext)
                                                                                         throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte[] header = responseCommand.getHeader();
        try {
            responseCommand.setResponseHeader(new String(header, "UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.err.println("UnsupportedEncodingException");
        }
        return true;
    }
    return false;
}
 
Example #5
Source File: NormalStringCustomSerializer_InvokeContext.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    deserialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;

    if (StringUtils.equals(SERIALTYPE1_value, (String) invokeContext.get(SERIALTYPE_KEY))) {
        try {
            rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    } else {
        rpcResp.setResponseObject(UNIVERSAL_RESP);
    }
    return true;
}
 
Example #6
Source File: CustomHeaderSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.CustomSerializer#serializeHeader(com.alipay.remoting.rpc.ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeHeader(T response)
                                                                      throws SerializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        try {
            responseCommand.setHeader(EXECUTOR1.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.err.println("UnsupportedEncodingException");
        }
        return true;
    }
    return false;
}
 
Example #7
Source File: ExceptionStringCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/** 
 * @see CustomSerializer#serializeContent(ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeContent(T response)
                                                                       throws SerializationException {
    serialFlag.set(true);
    if (serialRuntimeException) {
        throw new RuntimeException("serialRuntimeException in ExceptionStringCustomSerializer!");
    } else if (serialException) {
        throw new SerializationException("serialException in ExceptionStringCustomSerializer!");
    } else {
        return false;// use default codec 
    }
}
 
Example #8
Source File: NormalStringCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see CustomSerializer#serializeContent(ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeContent(T response)
                                                                       throws SerializationException {
    serialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;
    String str = (String) rpcResp.getResponseObject();
    try {
        rpcResp.setContent(str.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    contentSerializer = response.getSerializer();
    return true;
}
 
Example #9
Source File: NormalStringCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see CustomSerializer#deserializeContent(ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    deserialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;
    try {
        rpcResp.setResponseObject(new String(rpcResp.getContent(), "UTF-8") + "RANDOM");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    contentDeserialier = response.getSerializer();
    return true;
}
 
Example #10
Source File: NormalStringCustomSerializer_InvokeContext.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see CustomSerializer#serializeContent(ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeContent(T response)
                                                                       throws SerializationException {
    serialFlag.set(true);
    RpcResponseCommand rpcResp = (RpcResponseCommand) response;
    String str = (String) rpcResp.getResponseObject();
    try {
        rpcResp.setContent(str.getBytes("UTF-8"));
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    return true;
}
 
Example #11
Source File: DefaultCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/** 
 * @see com.alipay.remoting.CustomSerializer#deserializeContent(com.alipay.remoting.rpc.ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response,
                                                              InvokeContext invokeContext)
                                                                                          throws DeserializationException {
    return false;
}
 
Example #12
Source File: DefaultCustomSerializer.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/** 
 * @see com.alipay.remoting.CustomSerializer#deserializeHeader(com.alipay.remoting.rpc.ResponseCommand, InvokeContext)
 */
@Override
public <T extends ResponseCommand> boolean deserializeHeader(T response,
                                                             InvokeContext invokeContext)
                                                                                         throws DeserializationException {
    return false;
}
 
Example #13
Source File: RpcCommandHandler.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
private void processExceptionForSingleCommand(RemotingContext ctx, Object msg, Throwable t) {
    final int id = ((RpcCommand) msg).getId();
    final String emsg = "Exception caught when processing "
                        + ((msg instanceof RequestCommand) ? "request, id=" : "response, id=");
    logger.warn(emsg + id, t);
    if (msg instanceof RequestCommand) {
        final RequestCommand cmd = (RequestCommand) msg;
        if (cmd.getType() != RpcCommandType.REQUEST_ONEWAY) {
            if (t instanceof RejectedExecutionException) {
                final ResponseCommand response = this.commandFactory.createExceptionResponse(
                    id, ResponseStatus.SERVER_THREADPOOL_BUSY);
                // RejectedExecutionException here assures no response has been sent back
                // Other exceptions should be processed where exception was caught, because here we don't known whether ack had been sent back.
                ctx.getChannelContext().writeAndFlush(response)
                    .addListener(new ChannelFutureListener() {
                        @Override
                        public void operationComplete(ChannelFuture future) throws Exception {
                            if (future.isSuccess()) {
                                if (logger.isInfoEnabled()) {
                                    logger
                                        .info(
                                            "Write back exception response done, requestId={}, status={}",
                                            id, response.getResponseStatus());
                                }
                            } else {
                                logger.error(
                                    "Write back exception response failed, requestId={}", id,
                                    future.cause());
                            }
                        }

                    });
            }
        }
    }
}
 
Example #14
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean deserializeHeader(Response response, InvokeContext invokeContext)
    throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();

        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte[] header = responseCommand.getHeader();
        responseCommand.setResponseHeader(mapSerializer.decode(header));
        return true;
    }
    return false;
}
 
Example #15
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext)
                                                                                                      throws DeserializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final String className = cmd.getResponseClass();

    cmd.setResponseObject(ProtobufMsgFactory.newMessageByJavaClassName(className, response.getContent()));
    return true;
}
 
Example #16
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 5 votes vote down vote up
@Override
public <T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException {
    final RpcResponseCommand cmd = (RpcResponseCommand) response;
    final Message msg = (Message) cmd.getResponseObject();
    cmd.setContent(msg.toByteArray());
    return true;
}
 
Example #17
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public <Response extends ResponseCommand> boolean deserializeContent(Response response, InvokeContext invokeContext)
    throws DeserializationException {
    if (response instanceof RpcResponseCommand) {
        RpcResponseCommand responseCommand = (RpcResponseCommand) response;
        byte serializer = response.getSerializer();
        byte[] content = responseCommand.getContent();
        if (content == null || content.length == 0) {
            return false;
        }
        try {
            Object sofaResponse = ClassUtils.forName(responseCommand.getResponseClass()).newInstance();

            Map<String, String> header = (Map<String, String>) responseCommand.getResponseHeader();
            if (header == null) {
                header = new HashMap<String, String>();
            }
            putKV(header, RemotingConstants.HEAD_TARGET_SERVICE,
                (String) invokeContext.get(RemotingConstants.HEAD_TARGET_SERVICE));
            putKV(header, RemotingConstants.HEAD_METHOD_NAME,
                (String) invokeContext.get(RemotingConstants.HEAD_METHOD_NAME));
            putKV(header, RemotingConstants.HEAD_GENERIC_TYPE,
                (String) invokeContext.get(RemotingConstants.HEAD_GENERIC_TYPE));

            Serializer rpcSerializer = com.alipay.sofa.rpc.codec.SerializerFactory.getSerializer(serializer);
            rpcSerializer.decode(new ByteArrayWrapperByteBuf(responseCommand.getContent()), sofaResponse, header);

            responseCommand.setResponseObject(sofaResponse);
            return true;
        } catch (Exception ex) {
            throw new DeserializationException(ex.getMessage(), ex);
        } finally {
            recordDeserializeResponse(responseCommand, invokeContext);
        }
    }

    return false;
}
 
Example #18
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends ResponseCommand> boolean deserializeHeader(T response, InvokeContext invokeContext)
                                                                                                     throws DeserializationException {
    return false;
}
 
Example #19
Source File: DefaultCustomSerializer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
/** 
 * @see com.alipay.remoting.CustomSerializer#serializeContent(com.alipay.remoting.rpc.ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeContent(T response)
                                                                       throws SerializationException {
    return false;
}
 
Example #20
Source File: DefaultCustomSerializer.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
/** 
 * @see com.alipay.remoting.CustomSerializer#serializeHeader(com.alipay.remoting.rpc.ResponseCommand)
 */
@Override
public <T extends ResponseCommand> boolean serializeHeader(T response)
                                                                      throws SerializationException {
    return false;
}
 
Example #21
Source File: RpcCommandDecoder.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
private ResponseCommand createResponseCommand(short cmdCode) {
    ResponseCommand command = new RpcResponseCommand();
    command.setCmdCode(RpcCommandCode.valueOf(cmdCode));
    return command;
}
 
Example #22
Source File: RpcCommandEncoder.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
/**
 * @see com.alipay.remoting.CommandEncoder#encode(io.netty.channel.ChannelHandlerContext, java.io.Serializable, io.netty.buffer.ByteBuf)
 */
@Override
public void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    try {
        if (msg instanceof RpcCommand) {
            /*
             * ver: version for protocol
             * type: request/response/request oneway
             * cmdcode: code for remoting command
             * ver2:version for remoting command
             * requestId: id of request
             * codec: code for codec
             * (req)timeout: request timeout.
             * (resp)respStatus: response status
             * classLen: length of request or response class name
             * headerLen: length of header
             * cotentLen: length of content
             * className
             * header
             * content
             */
            RpcCommand cmd = (RpcCommand) msg;
            out.writeByte(RpcProtocol.PROTOCOL_CODE);
            out.writeByte(cmd.getType());
            out.writeShort(((RpcCommand) msg).getCmdCode().value());
            out.writeByte(cmd.getVersion());
            out.writeInt(cmd.getId());
            out.writeByte(cmd.getSerializer());
            if (cmd instanceof RequestCommand) {
                //timeout
                out.writeInt(((RequestCommand) cmd).getTimeout());
            }
            if (cmd instanceof ResponseCommand) {
                //response status
                ResponseCommand response = (ResponseCommand) cmd;
                out.writeShort(response.getResponseStatus().getValue());
            }
            out.writeShort(cmd.getClazzLength());
            out.writeShort(cmd.getHeaderLength());
            out.writeInt(cmd.getContentLength());
            if (cmd.getClazzLength() > 0) {
                out.writeBytes(cmd.getClazz());
            }
            if (cmd.getHeaderLength() > 0) {
                out.writeBytes(cmd.getHeader());
            }
            if (cmd.getContentLength() > 0) {
                out.writeBytes(cmd.getContent());
            }
        } else {
            String warnMsg = "msg type [" + msg.getClass() + "] is not subclass of RpcCommand";
            logger.warn(warnMsg);
        }
    } catch (Exception e) {
        logger.error("Exception caught!", e);
        throw e;
    }
}
 
Example #23
Source File: RpcCommandDecoderV2.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
private ResponseCommand createResponseCommand(short cmdCode) {
    ResponseCommand command = new RpcResponseCommand();
    command.setCmdCode(RpcCommandCode.valueOf(cmdCode));
    return command;
}
 
Example #24
Source File: RpcCommandEncoderV2.java    From sofa-bolt with Apache License 2.0 4 votes vote down vote up
/**
 * @see CommandEncoder#encode(ChannelHandlerContext, Serializable, ByteBuf)
 */
@Override
public void encode(ChannelHandlerContext ctx, Serializable msg, ByteBuf out) throws Exception {
    try {
        if (msg instanceof RpcCommand) {
            /*
             * proto: magic code for protocol
             * ver: version for protocol
             * type: request/response/request oneway
             * cmdcode: code for remoting command
             * ver2:version for remoting command
             * requestId: id of request
             * codec: code for codec
             * switch: function switch
             * (req)timeout: request timeout.
             * (resp)respStatus: response status
             * classLen: length of request or response class name
             * headerLen: length of header
             * cotentLen: length of content
             * className
             * header
             * content
             * crc (optional)
             */
            int index = out.writerIndex();
            RpcCommand cmd = (RpcCommand) msg;
            out.writeByte(RpcProtocolV2.PROTOCOL_CODE);
            Attribute<Byte> version = ctx.channel().attr(Connection.VERSION);
            byte ver = RpcProtocolV2.PROTOCOL_VERSION_1;
            if (version != null && version.get() != null) {
                ver = version.get();
            }
            out.writeByte(ver);
            out.writeByte(cmd.getType());
            out.writeShort(((RpcCommand) msg).getCmdCode().value());
            out.writeByte(cmd.getVersion());
            out.writeInt(cmd.getId());
            out.writeByte(cmd.getSerializer());
            out.writeByte(cmd.getProtocolSwitch().toByte());
            if (cmd instanceof RequestCommand) {
                //timeout
                out.writeInt(((RequestCommand) cmd).getTimeout());
            }
            if (cmd instanceof ResponseCommand) {
                //response status
                ResponseCommand response = (ResponseCommand) cmd;
                out.writeShort(response.getResponseStatus().getValue());
            }
            out.writeShort(cmd.getClazzLength());
            out.writeShort(cmd.getHeaderLength());
            out.writeInt(cmd.getContentLength());
            if (cmd.getClazzLength() > 0) {
                out.writeBytes(cmd.getClazz());
            }
            if (cmd.getHeaderLength() > 0) {
                out.writeBytes(cmd.getHeader());
            }
            if (cmd.getContentLength() > 0) {
                out.writeBytes(cmd.getContent());
            }
            if (ver == RpcProtocolV2.PROTOCOL_VERSION_2
                && cmd.getProtocolSwitch().isOn(ProtocolSwitch.CRC_SWITCH_INDEX)) {
                // compute the crc32 and write to out
                byte[] frame = new byte[out.readableBytes()];
                out.getBytes(index, frame);
                out.writeInt(CrcUtil.crc32(frame));
            }
        } else {
            String warnMsg = "msg type [" + msg.getClass() + "] is not subclass of RpcCommand";
            logger.warn(warnMsg);
        }
    } catch (Exception e) {
        logger.error("Exception caught!", e);
        throw e;
    }
}
 
Example #25
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends ResponseCommand> boolean serializeHeader(T response) throws SerializationException {
    return false;
}
 
Example #26
Source File: CustomSerializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Deserialize the content of ResponseCommand.
 * 
 * @param response
 * @param invokeContext
 * @return
 * @throws CodecException
 */
<T extends ResponseCommand> boolean deserializeContent(T response, InvokeContext invokeContext)
                                                                                               throws DeserializationException;
 
Example #27
Source File: CustomSerializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Serialize the content of ResponseCommand.
 * 
 * @param response
 * @return
 * @throws CodecException
 */
<T extends ResponseCommand> boolean serializeContent(T response) throws SerializationException;
 
Example #28
Source File: CustomSerializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Deserialize the header of ResponseCommand.
 * 
 * @param response
 * @param invokeContext
 * @return
 * @throws CodecException
 */
<T extends ResponseCommand> boolean deserializeHeader(T response, InvokeContext invokeContext)
                                                                                              throws DeserializationException;
 
Example #29
Source File: CustomSerializer.java    From sofa-bolt with Apache License 2.0 2 votes vote down vote up
/**
 * Serialize the header of ResponseCommand.
 *
 * @param response
 * @return
 * @throws CodecException
 */
<T extends ResponseCommand> boolean serializeHeader(T response) throws SerializationException;