use of org.apache.thrift.protocol.TBinaryProtocol in project dubbo by alibaba.
the class ThriftCodecTest method testEncodeExceptionResponse.
@Test
public void testEncodeExceptionResponse() throws Exception {
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:40880/" + Demo.Iface.class.getName());
Channel channel = new MockedChannel(url);
Request request = createRequest();
RpcResult rpcResult = new RpcResult();
String exceptionMessage = "failed";
rpcResult.setException(new RuntimeException(exceptionMessage));
Response response = new Response();
response.setResult(rpcResult);
response.setId(request.getId());
ChannelBuffer bos = ChannelBuffers.dynamicBuffer(1024);
ThriftCodec.RequestData rd = ThriftCodec.RequestData.create(ThriftCodec.getSeqId(), Demo.Iface.class.getName(), "echoString");
ThriftCodec.cachedRequest.put(request.getId(), rd);
codec.encode(channel, bos, response);
byte[] buf = new byte[bos.writerIndex() - 4];
System.arraycopy(bos.array(), 4, buf, 0, bos.writerIndex() - 4);
ByteArrayInputStream bis = new ByteArrayInputStream(buf);
if (bis.markSupported()) {
bis.mark(0);
}
TIOStreamTransport transport = new TIOStreamTransport(bis);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
Assert.assertEquals(ThriftCodec.MAGIC, protocol.readI16());
Assert.assertEquals(protocol.readI32() + 4, bos.writerIndex());
int headerLength = protocol.readI16();
Assert.assertEquals(ThriftCodec.VERSION, protocol.readByte());
Assert.assertEquals(Demo.Iface.class.getName(), protocol.readString());
Assert.assertEquals(request.getId(), protocol.readI64());
if (bis.markSupported()) {
bis.reset();
bis.skip(headerLength);
}
TMessage message = protocol.readMessageBegin();
Assert.assertEquals("echoString", message.name);
Assert.assertEquals(TMessageType.EXCEPTION, message.type);
Assert.assertEquals(ThriftCodec.getSeqId(), message.seqid);
TApplicationException exception = TApplicationException.read(protocol);
protocol.readMessageEnd();
Assert.assertEquals(exceptionMessage, exception.getMessage());
}
use of org.apache.thrift.protocol.TBinaryProtocol in project Honu by jboulon.
the class HTTPMessageSender method openConnection.
protected void openConnection() throws CommunicationException {
try {
if (httpClient != null) {
try {
httpClient.close();
} catch (Exception ignored) {
}
httpClient = null;
}
if (protocol != null) {
protocol = null;
}
if (collector != null) {
collector = null;
}
currentCollectorInfo = CollectorRegistry.getInstance().getCollector();
if (currentCollectorInfo == null) {
throw new CommunicationException("collector is null");
}
httpClient = new THttpClient(currentCollectorInfo.getHost() + ":" + currentCollectorInfo.getPort());
httpClient.setConnectTimeout(SENDER_TIMEOUT);
protocol = new TBinaryProtocol(httpClient);
collector = new HonuCollector.Client(protocol);
int status = collector.getStatus();
if (status != ServiceStatus.ALIVE) {
throw new RuntimeException("Collector is not alive! -- status:" + status);
}
} catch (Throwable e) {
collector = null;
throw new CommunicationException(e);
}
}
use of org.apache.thrift.protocol.TBinaryProtocol in project brisk by riptano.
the class Session method getClient.
/**
* Thrift client connection
* @param setKeyspace - should we set keyspace for client or not
* @return cassandra client connection
*/
public Cassandra.Client getClient(boolean setKeyspace) {
// random node selection for fake load balancing
String currentNode = nodes[Pricer.randomizer.nextInt(nodes.length)];
TSocket socket = new TSocket(currentNode, port);
TTransport transport = (isUnframed()) ? socket : new TFramedTransport(socket);
Cassandra.Client client = new Cassandra.Client(new TBinaryProtocol(transport));
try {
transport.open();
if (setKeyspace) {
client.set_keyspace("PortfolioDemo");
}
} catch (InvalidRequestException e) {
throw new RuntimeException(e.getWhy());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
return client;
}
use of org.apache.thrift.protocol.TBinaryProtocol in project brisk by riptano.
the class ThriftUtils method createNamenodeClient.
/**
* Creates a Thrift name node client.
*
* @param conf the HDFS instance
* @return a Thrift name node client.
*/
public static Namenode.Client createNamenodeClient(Configuration conf) throws Exception {
String s = conf.get(NamenodePlugin.THRIFT_ADDRESS_PROPERTY, NamenodePlugin.DEFAULT_THRIFT_ADDRESS);
// TODO(todd) use fs.default.name here if set to 0.0.0.0 - but share this with the code in
// SecondaryNameNode that does the same
InetSocketAddress addr = NetUtils.createSocketAddr(s);
// in the thrift config.
if (addr.getAddress().isAnyLocalAddress()) {
InetSocketAddress nnAddr = NameNode.getAddress(conf);
addr = new InetSocketAddress(nnAddr.getAddress(), addr.getPort());
}
TTransport t = new TSocket(addr.getHostName(), addr.getPort());
if (UserGroupInformation.isSecurityEnabled()) {
t = new HadoopThriftAuthBridge.Client().createClientTransport(conf.get(DFSConfigKeys.DFS_NAMENODE_USER_NAME_KEY), addr.getHostName(), "KERBEROS", t);
}
t.open();
TProtocol p = new TBinaryProtocol(t);
return new Namenode.Client(p);
}
use of org.apache.thrift.protocol.TBinaryProtocol in project jstorm by alibaba.
the class ThriftClient method reconnect.
public synchronized void reconnect() {
close();
try {
TSocket socket = new TSocket(host, port);
if (timeout != null) {
socket.setTimeout(timeout);
} else {
// @@@ Todo
// set the socket default Timeout as xxxx
}
// locate login configuration
Configuration login_conf = AuthUtils.GetConfiguration(conf);
// construct a transport plugin
ITransportPlugin transportPlugin = AuthUtils.GetTransportPlugin(type, conf, login_conf);
final TTransport underlyingTransport = socket;
// TODO get this from type instead of hardcoding to Nimbus.
// establish client-server transport via plugin
// do retries if the connect fails
TBackoffConnect connectionRetry = new TBackoffConnect(Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_TIMES)), Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_INTERVAL)), Utils.getInt(conf.get(Config.STORM_NIMBUS_RETRY_INTERVAL_CEILING)));
_transport = connectionRetry.doConnectWithRetry(transportPlugin, underlyingTransport, host, asUser);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
_protocol = null;
if (_transport != null) {
_protocol = new TBinaryProtocol(_transport);
}
}
Aggregations