com.alipay.remoting.InvokeContext Java Examples

The following examples show how to use com.alipay.remoting.InvokeContext. 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: SpecificClientUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    String threadName = Thread.currentThread().getName();
    Assert.assertTrue(threadName.contains("bolt-netty-client-worker"));

    logger.warn("Request received:" + request);
    Assert.assertEquals(RequestBody.class, request.getClass());

    long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    logger.warn("Client User processor process wait time [" + waittime + "].");

    invokeTimes.incrementAndGet();
    if (!delaySwitch) {
        return RequestBody.DEFAULT_CLIENT_RETURN_STR;
    }
    try {
        Thread.sleep(delayMs);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return RequestBody.DEFAULT_CLIENT_RETURN_STR;
}
 
Example #2
Source File: BoltChannelUtilTest.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetBoltCustomSerializer() {
    Assert.assertNull(BoltChannelUtil.getBoltCustomSerializer(new MockChannel()));
    BoltChannel boltChannel = new BoltChannel();
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, new Object());
    RemotingContext remotingContext = new RemotingContext(new MockChannelHandlerContext(),
        invokeContext, false, new ConcurrentHashMap<>());
    BizContext bizContext = new DefaultBizContext(remotingContext);
    boltChannel.setBizContext(bizContext);
    boolean isException = false;
    try {
        BoltChannelUtil.getBoltCustomSerializer(boltChannel);
    } catch (Throwable r) {
        isException = true;
    }
    Assert.assertTrue(isException);
    invokeContext.put(InvokeContext.BOLT_CUSTOM_SERIALIZER, new Byte("3"));
    Assert.assertEquals(new Byte("3"), BoltChannelUtil.getBoltCustomSerializer(boltChannel));
}
 
Example #3
Source File: BoltChannelUtil.java    From sofa-registry with Apache License 2.0 6 votes vote down vote up
public static Byte getBoltCustomSerializer(Channel channel) {
    if (channel instanceof BoltChannel) {
        BoltChannel boltChannel = (BoltChannel) channel;
        InvokeContext invokeContext = boltChannel.getBizContext().getInvokeContext();

        if (null != invokeContext) {
            // set client custom codec for request command if not null
            Object clientCustomCodec = invokeContext.get(InvokeContext.BOLT_CUSTOM_SERIALIZER);
            if (null != clientCustomCodec) {
                try {
                    return (Byte) clientCustomCodec;
                } catch (ClassCastException e) {
                    throw new IllegalArgumentException(
                        "Illegal custom codec [" + clientCustomCodec
                                + "], the type of value should be [byte], but now is ["
                                + clientCustomCodec.getClass().getName() + "].");
                }
            }
        }
    }
    return null;
}
 
Example #4
Source File: SpecificServerUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    String threadName = Thread.currentThread().getName();
    Assert.assertTrue(threadName.contains("Rpc-netty-server-worker"));

    logger.warn("Request received:" + request);
    this.remoteAddr = bizCtx.getRemoteAddress();

    long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    logger.warn("Server User processor process wait time [" + waittime + "].");

    latch.countDown();
    logger.warn("Server User processor say, remote address is [" + this.remoteAddr + "].");
    Assert.assertEquals(RequestBody.class, request.getClass());
    processTimes(request);
    if (!delaySwitch) {
        return RequestBody.DEFAULT_SERVER_RETURN_STR;
    }
    try {
        Thread.sleep(delayMs);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return RequestBody.DEFAULT_SERVER_RETURN_STR;
}
 
Example #5
Source File: ProtobufSerializer.java    From sofa-jraft with Apache License 2.0 6 votes vote down vote up
@Override
public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext)
                                                                                                 throws SerializationException {

    final RpcRequestCommand cmd = (RpcRequestCommand) request;
    final Message msg = (Message) cmd.getRequestObject();
    if (msg instanceof RpcRequests.AppendEntriesRequest) {
        final RpcRequests.AppendEntriesRequest req = (RpcRequests.AppendEntriesRequest) msg;
        final RpcRequests.AppendEntriesRequestHeader.Builder hb = RpcRequests.AppendEntriesRequestHeader
            .newBuilder() //
            .setGroupId(req.getGroupId()) //
            .setPeerId(req.getPeerId()) //
            .setServerId(req.getServerId());
        cmd.setHeader(hb.build().toByteArray());
        return true;
    }

    return false;
}
 
Example #6
Source File: RpcRemoting.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * Synchronous rpc invocation.<br>
 * Notice! DO NOT modify the request object concurrently when this method is called.
 * 
 * @param conn
 * @param request
 * @param invokeContext 
 * @param timeoutMillis
 * @return
 * @throws RemotingException
 * @throws InterruptedException
 */
public Object invokeSync(final Connection conn, final Object request,
                         final InvokeContext invokeContext, final int timeoutMillis)
                                                                                    throws RemotingException,
                                                                                    InterruptedException {
    RemotingCommand requestCommand = toRemotingCommand(request, conn, invokeContext,
        timeoutMillis);
    preProcessInvokeContext(invokeContext, requestCommand, conn);
    ResponseCommand responseCommand = (ResponseCommand) super.invokeSync(conn, requestCommand,
        timeoutMillis);
    responseCommand.setInvokeContext(invokeContext);

    Object responseObject = RpcResponseResolver.resolveResponseObject(responseCommand,
        RemotingUtil.parseRemoteAddress(conn.getChannel()));
    return responseObject;
}
 
Example #7
Source File: SimpleServerMultiInterestUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
private Object handleRequest(BizContext bizCtx, RequestBodyC2 request) {

        Long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
        Assert.assertNotNull(waittime);
        if (logger.isInfoEnabled()) {
            logger.info("Client User processor process wait time {}", waittime);
        }

        processTimes(request);
        if (!delaySwitch) {
            return RequestBodyC2.DEFAULT_SERVER_RETURN_STR;
        }
        try {
            Thread.sleep(delayMs);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return RequestBodyC2.DEFAULT_SERVER_RETURN_STR;
    }
 
Example #8
Source File: SpecificServerUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    String threadName = Thread.currentThread().getName();
    Assert.assertTrue(threadName.contains("Rpc-specific0-executor"));

    logger.warn("Request received:" + request);
    this.remoteAddr = bizCtx.getRemoteAddress();

    long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    logger.warn("Server User processor process wait time [" + waittime + "].");

    latch.countDown();
    logger.warn("Server User processor say, remote address is [" + this.remoteAddr + "].");
    Assert.assertEquals(RequestBody.class, request.getClass());
    processTimes(request);
    if (!delaySwitch) {
        return RequestBody.DEFAULT_SERVER_RETURN_STR;
    }
    try {
        Thread.sleep(delayMs);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return RequestBody.DEFAULT_SERVER_RETURN_STR;
}
 
Example #9
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public SofaResponse syncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    SofaResponse response = null;
    SofaRpcException throwable = null;
    try {
        beforeSend(context, request);
        response = doInvokeSync(request, boltInvokeContext, timeout);
        return response;
    } catch (Exception e) { // 其它异常
        throwable = convertToRpcException(e);
        throw throwable;
    } finally {
        afterSend(context, boltInvokeContext, request);
        if (EventBus.isEnable(ClientSyncReceiveEvent.class)) {
            EventBus.post(new ClientSyncReceiveEvent(transportConfig.getConsumerConfig(),
                transportConfig.getProviderInfo(), request, response, throwable));
        }
    }
}
 
Example #10
Source File: NormalRequestBodyCustomSerializer.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/** 
 * @see CustomSerializer#serializeContent(RequestCommand, InvokeContext)
 */
@Override
public <T extends RequestCommand> boolean serializeContent(T req, InvokeContext invokeContext)
                                                                                              throws SerializationException {
    serialFlag.set(true);
    RpcRequestCommand rpcReq = (RpcRequestCommand) req;
    RequestBody bd = (RequestBody) rpcReq.getRequestObject();
    int id = bd.getId();
    byte[] msg;
    try {
        msg = bd.getMsg().getBytes("UTF-8");
        ByteBuffer bb = ByteBuffer.allocate(4 + msg.length);
        bb.putInt(id);
        bb.put(msg);
        rpcReq.setContent(bb.array());
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

    contentSerializer = rpcReq.getSerializer();
    return true;
}
 
Example #11
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
/**
 * 客户端记录响应反序列化大小和响应反序列化耗时
 *
 * @param responseCommand 响应体
 */
private void recordDeserializeResponse(RpcResponseCommand responseCommand, InvokeContext invokeContext) {
    if (!RpcInternalContext.isAttachmentEnable()) {
        return;
    }
    RpcInternalContext context = null;
    if (invokeContext != null) {
        // 客户端异步调用的情况下,上下文会放在InvokeContext中传递
        context = invokeContext.get(RemotingConstants.INVOKE_CTX_RPC_CTX);
    }
    if (context == null) {
        context = RpcInternalContext.getContext();
    }
    int cost = context.getStopWatch().tick().read();
    int respSize = RpcProtocol.getResponseHeaderLength()
        + responseCommand.getClazzLength()
        + responseCommand.getContentLength()
        + responseCommand.getHeaderLength();
    // 记录响应反序列化大小和响应反序列化耗时
    context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_SIZE, respSize);
    context.setAttachment(RpcConstants.INTERNAL_KEY_RESP_DESERIALIZE_TIME, cost);
}
 
Example #12
Source File: BasicUsage_ProtocolV1_Test.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Test
public void testOneway() throws InterruptedException {
    RequestBody req = new RequestBody(2, "hello world oneway");
    for (int i = 0; i < invokeTimes; i++) {
        try {
            String res = null;
            if (i % 2 == 0) {
                client.oneway(addr, req);
            } else {
                InvokeContext invokeContext = new InvokeContext();
                invokeContext.putIfAbsent(InvokeContext.BOLT_CRC_SWITCH, false);
                client.oneway(addr, req, invokeContext);
            }
            Thread.sleep(100);
        } catch (RemotingException e) {
            String errMsg = "RemotingException caught in oneway!";
            logger.error(errMsg, e);
            Assert.fail(errMsg);
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example #13
Source File: SofaRpcSerialization.java    From sofa-rpc with Apache License 2.0 6 votes vote down vote up
@Override
public <Request extends RequestCommand> boolean serializeHeader(Request request, InvokeContext invokeContext)
    throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcInternalContext.getContext().getStopWatch().tick();

        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        Object requestObject = requestCommand.getRequestObject();
        String service = getTargetServiceName(requestObject);
        if (StringUtils.isNotEmpty(service)) {
            Map<String, String> header = new HashMap<String, String>(16);
            header.put(RemotingConstants.HEAD_SERVICE, service);
            putRequestMetadataToHeader(requestObject, header);
            requestCommand.setHeader(mapSerializer.encode(header));
        }
        return true;
    }
    return false;
}
 
Example #14
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 #15
Source File: SpecificClientUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    String threadName = Thread.currentThread().getName();
    Assert.assertTrue(threadName.contains("Rpc-specific1-executor"));

    logger.warn("Request received:" + request);
    Assert.assertEquals(RequestBody.class, request.getClass());

    long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    logger.warn("Client User processor process wait time [" + waittime + "].");

    invokeTimes.incrementAndGet();
    if (!delaySwitch) {
        return RequestBody.DEFAULT_CLIENT_RETURN_STR;
    }
    try {
        Thread.sleep(delayMs);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return RequestBody.DEFAULT_CLIENT_RETURN_STR;
}
 
Example #16
Source File: ClassCustomSerializerTest.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
/**
 * test custom serializer using invoke contxt in sync
 *
 * @throws Exception
 */
@Test
public void testInvokeContextCustomSerializer_SYNC() throws Exception {
    NormalRequestBodyCustomSerializer_InvokeContext s1 = new NormalRequestBodyCustomSerializer_InvokeContext();
    NormalStringCustomSerializer_InvokeContext s2 = new NormalStringCustomSerializer_InvokeContext();
    CustomSerializerManager.registerCustomSerializer(RequestBody.class.getName(), s1);
    CustomSerializerManager.registerCustomSerializer(String.class.getName(), s2);

    RequestBody body = new RequestBody(1, "hello world!");
    InvokeContext invokeContext = new InvokeContext();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE1_value);
    String ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(RequestBody.DEFAULT_SERVER_RETURN_STR + "RANDOM", ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());

    invokeContext.clear();
    invokeContext.putIfAbsent(NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE_KEY,
        NormalRequestBodyCustomSerializer_InvokeContext.SERIALTYPE2_value);
    ret = (String) client.invokeSync(addr, body, invokeContext, 1000);
    Assert.assertEquals(NormalStringCustomSerializer_InvokeContext.UNIVERSAL_RESP, ret);
    Assert.assertTrue(s1.isSerialized());
    Assert.assertTrue(s1.isDeserialized());
}
 
Example #17
Source File: BasicUsage_InvokeContext_Test.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
@Test
public void testOneway() throws InterruptedException {
    RequestBody req = new RequestBody(2, "hello world oneway");
    for (int i = 0; i < invokeTimes; i++) {
        try {
            InvokeContext invokeContext = new InvokeContext();
            client.oneway(addr, req, invokeContext);
            Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.CLIENT_LOCAL_IP));
            Assert.assertEquals("127.0.0.1", invokeContext.get(InvokeContext.CLIENT_REMOTE_IP));
            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_LOCAL_PORT));
            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_REMOTE_PORT));
            Assert.assertNotNull(invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
            logger.warn("CLIENT_CONN_CREATETIME:"
                        + invokeContext.get(InvokeContext.CLIENT_CONN_CREATETIME));
            Thread.sleep(100);
        } catch (RemotingException e) {
            String errMsg = "RemotingException caught in oneway!";
            logger.error(errMsg, e);
            Assert.fail(errMsg);
        }
    }

    Assert.assertTrue(serverConnectProcessor.isConnected());
    Assert.assertEquals(1, serverConnectProcessor.getConnectTimes());
    Assert.assertEquals(invokeTimes, serverUserProcessor.getInvokeTimes());
}
 
Example #18
Source File: SimpleClientMultiInterestUserProcessor.java    From sofa-bolt with Apache License 2.0 6 votes vote down vote up
private Object handleRequest(BizContext bizCtx, RequestBodyC2 request) {

        Long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
        Assert.assertNotNull(waittime);
        if (logger.isInfoEnabled()) {
            logger.info("Client User processor process wait time {}", waittime);
        }

        processTimes(request);
        if (!delaySwitch) {
            return RequestBodyC2.DEFAULT_CLIENT_RETURN_STR;
        }
        try {
            Thread.sleep(delayMs);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return RequestBodyC2.DEFAULT_CLIENT_RETURN_STR;
    }
 
Example #19
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponseFuture invokeWithFuture(final Url url, final Object request,
                                          final InvokeContext invokeContext,
                                          final int timeoutMillis) throws RemotingException,
                                                                  InterruptedException {
    return this.rpcRemoting.invokeWithFuture(url, request, invokeContext, timeoutMillis);
}
 
Example #20
Source File: BoltClientTransport.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
@Override
public ResponseFuture asyncSend(SofaRequest request, int timeout) throws SofaRpcException {
    checkConnection();
    RpcInternalContext context = RpcInternalContext.getContext();
    InvokeContext boltInvokeContext = createInvokeContext(request);
    try {
        beforeSend(context, request);
        boltInvokeContext.put(RemotingConstants.INVOKE_CTX_RPC_CTX, context);
        return doInvokeAsync(request, context, boltInvokeContext, timeout);
    } catch (Exception e) {
        throw convertToRpcException(e);
    } finally {
        afterSend(context, boltInvokeContext, request);
    }
}
 
Example #21
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public RpcResponseFuture invokeWithFuture(final String address, final Object request,
                                          final InvokeContext invokeContext,
                                          final int timeoutMillis) throws RemotingException,
                                                                  InterruptedException {
    return this.rpcRemoting.invokeWithFuture(address, request, invokeContext, timeoutMillis);
}
 
Example #22
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeWithCallback(final String address, final Object request,
                               final InvokeContext invokeContext,
                               final InvokeCallback invokeCallback, final int timeoutMillis)
                                                                                            throws RemotingException,
                                                                                            InterruptedException {
    this.rpcRemoting.invokeWithCallback(address, request, invokeContext, invokeCallback,
        timeoutMillis);
}
 
Example #23
Source File: RpcServerRemoting.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
/**
 * @see com.alipay.remoting.rpc.RpcRemoting#invokeSync(com.alipay.remoting.Url, java.lang.Object, InvokeContext, int)
 */
@Override
public Object invokeSync(Url url, Object request, InvokeContext invokeContext, int timeoutMillis)
                                                                                                 throws RemotingException,
                                                                                                 InterruptedException {
    Connection conn = this.connectionManager.get(url.getUniqueKey());
    if (null == conn) {
        throw new RemotingException("Client address [" + url.getUniqueKey()
                                    + "] not connected yet!");
    }
    this.connectionManager.check(conn);
    return this.invokeSync(conn, request, invokeContext, timeoutMillis);
}
 
Example #24
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public void invokeWithCallback(final Url url, final Object request,
                               final InvokeContext invokeContext,
                               final InvokeCallback invokeCallback, final int timeoutMillis)
                                                                                            throws RemotingException,
                                                                                            InterruptedException {
    this.rpcRemoting.invokeWithCallback(url, request, invokeContext, invokeCallback,
        timeoutMillis);
}
 
Example #25
Source File: SimpleClientUserProcessor.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    logger.warn("Request received:" + request);
    if (bizCtx.isRequestTimeout()) {
        String errMsg = "Stop process in client biz thread, already timeout!";
        logger.warn(errMsg);
        processTimes(request);
        throw new Exception(errMsg);
    }
    Assert.assertEquals(RequestBody.class, request.getClass());

    Long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    Assert.assertNotNull(waittime);
    if (logger.isInfoEnabled()) {
        logger.info("Client User processor process wait time {}", waittime);
    }

    processTimes(request);
    if (!delaySwitch) {
        return RequestBody.DEFAULT_CLIENT_RETURN_STR;
    }
    try {
        Thread.sleep(delayMs);
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
    return RequestBody.DEFAULT_CLIENT_RETURN_STR;
}
 
Example #26
Source File: BoltServerProcessor.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
private void putToContextIfNotNull(InvokeContext invokeContext, String oldKey,
                                   RpcInternalContext context, String key) {
    Object value = invokeContext.get(oldKey);
    if (value != null) {
        context.setAttachment(key, value);
    }
}
 
Example #27
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 #28
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.RequestCommand, InvokeContext)
 */
@Override
public <T extends RequestCommand> boolean serializeHeader(T request, InvokeContext invokeContext)
                                                                                                 throws SerializationException {
    if (request instanceof RpcRequestCommand) {
        RpcRequestCommand requestCommand = (RpcRequestCommand) request;
        try {
            requestCommand.setHeader(EXECUTOR1.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            System.err.println("UnsupportedEncodingException");
        }
        return true;
    }
    return false;
}
 
Example #29
Source File: RpcClient.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public Object invokeSync(final Url url, final Object request,
                         final InvokeContext invokeContext, final int timeoutMillis)
                                                                                    throws RemotingException,
                                                                                    InterruptedException {
    return this.rpcRemoting.invokeSync(url, request, invokeContext, timeoutMillis);
}
 
Example #30
Source File: PreHandleUserProcessor.java    From sofa-bolt with Apache License 2.0 5 votes vote down vote up
@Override
public Object handleRequest(BizContext bizCtx, RequestBody request) throws Exception {
    logger.warn("Request received:" + request);
    invokeTimes.incrementAndGet();

    long waittime = (Long) bizCtx.getInvokeContext().get(InvokeContext.BOLT_PROCESS_WAIT_TIME);
    logger.warn("PreHandleUserProcessor User processor process wait time [" + waittime + "].");

    Assert.assertEquals(RequestBody.class, request.getClass());
    Assert.assertEquals("127.0.0.1", bizCtx.getRemoteHost());
    Assert.assertTrue(bizCtx.getRemotePort() != -1);
    return bizCtx.get("test");
}