Java org.apache.thrift.transport.TIOStreamTransport 代码实例

・16 分钟阅读

以下是展示如何使用org.apache.thrift.transport.TIOStreamTransport的最佳示例。 我们使用了代码质量辨别算法从开源项目中提取出了最佳的优秀示例。

实例 1


@Override
public ByteBuffer serialize() throws Exception {
    if (mutationMap.isEmpty()) {
        throw new Exception("Mutation is empty");
    }
    ByteBufferOutputStream out       = new ByteBufferOutputStream();
    TIOStreamTransport     transport = new TIOStreamTransport(out);
    batch_mutate_args      args      = new batch_mutate_args();
    args.setMutation_map(mutationMap);
    try {
        args.write(new TBinaryProtocol(transport));
    }
    catch (TException e) {
        throw ThriftConverter.ToConnectionPoolException(e);
    }
    return out.getByteBuffer();
}
 

实例 2


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  TTransport inTransport = null;
  TTransport outTransport = null;
  try {
    response.setContentType("application/x-thrift");
    if (null != this.customHeaders) {
      for (Map.Entry<String, String> header : this.customHeaders) {
        response.addHeader(header.getKey(), header.getValue());
      }
    }
    InputStream in = request.getInputStream();
    OutputStream out = response.getOutputStream();
    TTransport transport = new TIOStreamTransport(in, out);
    inTransport = transport;
    outTransport = transport;
    TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
    TProtocol outProtocol = outProtocolFactory.getProtocol(outTransport);
    processor.process(inProtocol, outProtocol);
    out.flush();
  } catch (TException te) {
    throw new ServletException(te);
  }
}
 

实例 3


/**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   TTransport inTransport = null;
   TTransport outTransport = null;
   try {
     response.setContentType("application/x-thrift");
     if (null != this.customHeaders) {
for (Map.Entry<String, String> header : this.customHeaders) {
  response.addHeader(header.getKey(), header.getValue());
}
     }
     InputStream in = request.getInputStream();
     OutputStream out = response.getOutputStream();
     TTransport transport = new TIOStreamTransport(in, out);
     inTransport = transport;
     outTransport = transport;
     TProtocol inProtocol = inFactory.getProtocol(inTransport);
     TProtocol outProtocol = inFactory.getProtocol(outTransport);
     processor.process(inProtocol, outProtocol);
     out.flush();
   } catch (TException te) {
     throw new ServletException(te);
   }
 }
 

实例 4


@Override
protected void assertEqualsExcepted(List<org.apache.parquet.thrift.test.compat.StructWithUnionV2> expected, List<Object> found) throws Exception {
  List<StructWithUnionV2> scroogeExpected = new ArrayList<StructWithUnionV2>();
  for (org.apache.parquet.thrift.test.compat.StructWithUnionV2 tbase : expected) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TProtocol out = new Factory().getProtocol(new TIOStreamTransport(baos));
    tbase.write(out);
    TProtocol in = new Factory().getProtocol(new TIOStreamTransport(new ByteArrayInputStream(baos.toByteArray())));
    scroogeExpected.add(StructWithUnionV2$.MODULE$.decode(in));
  }
  assertEquals(scroogeExpected, found);
 }
 

实例 5


@Override
protected void doService(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    TTransport inTransport;
    TTransport outTransport;
    try {
        response.setContentType("application/x-thrift");
        if (null != this.customHeaders) {
            for (Map.Entry<String, String> header : this.customHeaders) {
                response.addHeader(header.getKey(), header.getValue());
            }
        }
        InputStream in = request.getInputStream();
        OutputStream out = response.getOutputStream();
        TTransport transport = new TIOStreamTransport(in, out);
        inTransport = transport;
        outTransport = transport;
        TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
        TProtocol outProtocol = outProtocolFactory.getProtocol(outTransport);
        getProcessor().process(inProtocol, outProtocol);
        out.flush();
    } catch (TException te) {
        throw new ServletException(te);
    }
}
 

实例 6


public void sendRequest() throws IOException, TException {
    TProtocol out = new TBinaryProtocol(new TIOStreamTransport(socketToServer.getOutputStream()));
    out.writeMessageBegin(new TMessage("dummy", TMessageType.CALL, 0));
    out.writeStructBegin(new TStruct("dummy_args"));
    out.writeFieldStop();
    out.writeStructEnd();
    out.writeMessageEnd();
    out.getTransport().flush();
}
 

实例 7


private void init(TProtocolFactory inFactory, TProtocolFactory outFactory)
    throws Exception {
  outTransport = new TIOStreamTransport(bos);
  inTransport = new TIOStreamTransport(bis);
  outProtocol = outFactory.getProtocol(outTransport);
  inProtocol = inFactory.getProtocol(inTransport);
}
 

实例 8


@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    if (!"POST".equals(request.getMethod())) {
        throw new HttpRequestMethodNotSupportedException(request.getMethod(),
                new String[]{"POST"}, "ThriftServiceExporter only supports POST requests");
    }
    InputStream in = request.getInputStream();
    OutputStream out = response.getOutputStream();
    try {
        ThriftContextHolder.init();
        response.setContentType("application/x-thrift");
        TTransport transport = new TIOStreamTransport(in, out);
        TProtocol protocol = getProtocolFactory().getProtocol(transport);
        TProcessor processor = ThriftUtil.buildProcessor(getServiceInterface(), getProxyForService());
        processor.process(protocol, protocol);
    } catch (Throwable e) {
        response.setContentType("text/plain; charset=UTF-8");
        response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
        e.printStackTrace(new PrintWriter(out, true));
        if (LOGGER.isErrorEnabled()) {
            LOGGER.error("Thrift server direct error", e);
        }
    } finally {
        ThriftContextHolder.reset();
    }
}
 

实例 9


/**
  * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
  *      response)
  */
 @Override
 protected void doPost(HttpServletRequest request, HttpServletResponse response)
     throws ServletException, IOException {
   TTransport inTransport = null;
   TTransport outTransport = null;
   try {
     response.setContentType("application/x-thrift");
     if (null != this.customHeaders) {
for (Map.Entry<String, String> header : this.customHeaders) {
  response.addHeader(header.getKey(), header.getValue());
}
     }
     InputStream in = request.getInputStream();
     OutputStream out = response.getOutputStream();
     TTransport transport = new TIOStreamTransport(in, out);
     inTransport = transport;
     outTransport = transport;
     TProtocol inProtocol = inFactory.getProtocol(inTransport);
     TProtocol outProtocol = inFactory.getProtocol(outTransport);
     processor.process(inProtocol, outProtocol);
     out.flush();
   } catch (TException te) {
     throw new ServletException(te);
   }
 }
 

实例 10


@Override
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    try {
        if (null != mediaType) {
            response.setContentType(mediaType.toString());
        }
        if (null != this.customHeaders) {
            for (Map.Entry<String, String> header : this.customHeaders) {
                response.addHeader(header.getKey(), header.getValue());
            }
        }
        InputStream in = request.getInputStream();
        OutputStream out = response.getOutputStream();
        // this is not pluggable from this end
        TTransport transport = new TIOStreamTransport(in, out);
        TProtocol protocol = protocolFactory.getProtocol(transport);
        processor.process(protocol, protocol);
        out.flush();
    } catch (TException te) {
        throw new ServletException(te);
    }
}
 

实例 11


@Override
public ResultCode Log(final List<LogEntry> messages) throws TException {
    try {
        for (final LogEntry logEntry : messages) {
            final byte[] decodedSpan = Base64.getDecoder().decode(logEntry.getMessage());
            final ByteArrayInputStream buf = new ByteArrayInputStream(decodedSpan);
            final TProtocolFactory factory = new TBinaryProtocol.Factory();
            final TProtocol proto = factory.getProtocol(new TIOStreamTransport(buf));
            final Span span = new Span();
            span.read(proto);
            spans.add(span);
        }
        if (delayMs > 0) {
            try {
                Thread.sleep(delayMs);
            } catch (final InterruptedException e) {
                LOGGER.log(Level.SEVERE, "Interrupted.", e);
            }
        }
    } catch (final TException e) {
        LOGGER.log(Level.SEVERE, "TException when getting result.", e);
        return ResultCode.TRY_LATER;
    }
    return ResultCode.OK;
}
 

实例 12


@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  response.setContentType("application/x-thrift");
  InputStream in = request.getInputStream();
  OutputStream out = response.getOutputStream();
  TTransport client = new TIOStreamTransport(in, out);
  TProcessor processor = null;
  TTransport inputTransport = null;
  TTransport outputTransport = null;
  TProtocol inputProtocol = null;
  TProtocol outputProtocol = null;
  try {
    processor = processor_;
    inputTransport = inputTransportFactory_.getTransport(client);
    outputTransport = outputTransportFactory_.getTransport(client);
    inputProtocol = inputProtocolFactory_.getProtocol(inputTransport);
    outputProtocol = outputProtocolFactory_.getProtocol(outputTransport);
    while (processor.process(inputProtocol, outputProtocol)) {}
  } catch (TTransportException ttx) {
    // Client died, just move on
  } catch (TException tx) {
    tx.printStackTrace();
  } catch (Exception x) {
    x.printStackTrace();
  }
  if (inputTransport != null) {
    inputTransport.close();
  }
  if (outputTransport != null) {
    outputTransport.close();
  }
}
 

实例 13


/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  TTransport inTransport = null;
  TTransport outTransport = null;
  try {
    response.setContentType("application/x-thrift");
    if (null != this.customHeaders) {
      for (Map.Entry<String, String> header : this.customHeaders) {
        response.addHeader(header.getKey(), header.getValue());
      }
    }
    InputStream in = request.getInputStream();
    OutputStream out = response.getOutputStream();
    TTransport transport = new TIOStreamTransport(in, out);
    inTransport = transport;
    outTransport = transport;
    TProtocol inProtocol = inProtocolFactory.getProtocol(inTransport);
    TProtocol outProtocol = outProtocolFactory.getProtocol(outTransport);
    processor.process(inProtocol, outProtocol);
    out.flush();
  } catch (TException te) {
    throw new ServletException(te);
  }
}
 

实例 14


@Override
protected Object decode(ChannelHandlerContext ctx,
                        Channel channel,
                        ChannelBuffer buffer) throws Exception {
    List<SyncMessage> ms = null;
    ChannelBuffer frame = null;
    while (null != (frame = (ChannelBuffer) super.decode(ctx, channel, 
                                                         buffer))) {
        if (ms == null) ms = new ArrayList<SyncMessage>();
        ChannelBufferInputStream is = new ChannelBufferInputStream(frame);
        TCompactProtocol thriftProtocol =
                new TCompactProtocol(new TIOStreamTransport(is));
        SyncMessage bsm = new SyncMessage();
        bsm.read(thriftProtocol);
        ms.add(bsm);
    }
    return ms;
}
 

实例 15


/**
 * Deserialize the Thrift object from a byte array.
 *
 * @param base The object to read into
 * @param bytes The array to read from
 */
public void deserialize(TBase base, byte[] bytes) throws TException {
  base.read(
      protocolFactory_.getProtocol(
        new TIOStreamTransport(
          new ByteArrayInputStream(bytes))));
}
 
讨论
淘淘あ西西 profile image