use of org.apache.dubbo.remoting.exchange.support.DefaultFuture in project dubbo by alibaba.
the class ExchangeCodec method getRequestData.
protected Object getRequestData(long id) {
DefaultFuture future = DefaultFuture.getFuture(id);
if (future == null) {
return null;
}
Request req = future.getRequest();
if (req == null) {
return null;
}
return req.getData();
}
use of org.apache.dubbo.remoting.exchange.support.DefaultFuture in project dubbo by alibaba.
the class HeaderExchangeChannel method request.
@Override
public CompletableFuture<Object> request(Object request, int timeout, ExecutorService executor) throws RemotingException {
if (closed) {
throw new RemotingException(this.getLocalAddress(), null, "Failed to send request " + request + ", cause: The channel " + this + " is closed!");
}
// create request.
Request req = new Request();
req.setVersion(Version.getProtocolVersion());
req.setTwoWay(true);
req.setData(request);
DefaultFuture future = DefaultFuture.newFuture(channel, req, timeout, executor);
try {
channel.send(req);
} catch (RemotingException e) {
future.cancel();
throw e;
}
return future;
}
use of org.apache.dubbo.remoting.exchange.support.DefaultFuture in project dubbo by alibaba.
the class ThriftCodecTest method testDecodeExceptionResponse.
@Test
public void testDecodeExceptionResponse() throws Exception {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.class.getName());
Channel channel = new MockedChannel(url);
RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
Request request = createRequest();
DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
TMessage message = new TMessage("echoString", TMessageType.EXCEPTION, ThriftCodec.getSeqId());
TTransport transport = new TIOStreamTransport(bos);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
TApplicationException exception = new TApplicationException();
int messageLength, headerLength;
// prepare
protocol.writeI16(ThriftCodec.MAGIC);
protocol.writeI32(Integer.MAX_VALUE);
protocol.writeI16(Short.MAX_VALUE);
protocol.writeByte(ThriftCodec.VERSION);
protocol.writeString(Demo.class.getName());
// path
protocol.writeString(Demo.class.getName());
protocol.writeI64(request.getId());
protocol.getTransport().flush();
headerLength = bos.size();
protocol.writeMessageBegin(message);
exception.write(protocol);
protocol.writeMessageEnd();
protocol.getTransport().flush();
int oldIndex = messageLength = bos.size();
try {
bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
protocol.writeI32(messageLength);
bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
protocol.writeI16((short) (0xffff & headerLength));
} finally {
bos.setWriteIndex(oldIndex);
}
// prepare
ChannelBuffer bis = ChannelBuffers.wrappedBuffer(encodeFrame(bos.toByteArray()));
Object obj = codec.decode((Channel) null, bis);
Assertions.assertNotNull(obj);
Assertions.assertTrue(obj instanceof Response);
Response response = (Response) obj;
Assertions.assertTrue(response.getResult() instanceof AppResponse);
AppResponse result = (AppResponse) response.getResult();
Assertions.assertTrue(result.hasException());
Assertions.assertTrue(result.getException() instanceof RpcException);
}
use of org.apache.dubbo.remoting.exchange.support.DefaultFuture in project dubbo by alibaba.
the class ThriftCodecTest method testDecodeReplyResponse.
@Test
public void testDecodeReplyResponse() throws Exception {
int port = NetUtils.getAvailablePort();
URL url = URL.valueOf(ThriftProtocol.NAME + "://127.0.0.1:" + port + "/" + Demo.Iface.class.getName());
Channel channel = new MockedChannel(url);
RandomAccessByteArrayOutputStream bos = new RandomAccessByteArrayOutputStream(128);
Request request = createRequest();
DefaultFuture future = DefaultFuture.newFuture(channel, request, 10, null);
TMessage message = new TMessage("echoString", TMessageType.REPLY, ThriftCodec.getSeqId());
Demo.echoString_result methodResult = new Demo.echoString_result();
methodResult.success = "Hello, World!";
TTransport transport = new TIOStreamTransport(bos);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
int messageLength, headerLength;
// prepare
protocol.writeI16(ThriftCodec.MAGIC);
protocol.writeI32(Integer.MAX_VALUE);
protocol.writeI16(Short.MAX_VALUE);
protocol.writeByte(ThriftCodec.VERSION);
protocol.writeString(Demo.Iface.class.getName());
// path
protocol.writeString(Demo.Iface.class.getName());
protocol.writeI64(request.getId());
protocol.getTransport().flush();
headerLength = bos.size();
protocol.writeMessageBegin(message);
methodResult.write(protocol);
protocol.writeMessageEnd();
protocol.getTransport().flush();
int oldIndex = messageLength = bos.size();
try {
bos.setWriteIndex(ThriftCodec.MESSAGE_LENGTH_INDEX);
protocol.writeI32(messageLength);
bos.setWriteIndex(ThriftCodec.MESSAGE_HEADER_LENGTH_INDEX);
protocol.writeI16((short) (0xffff & headerLength));
} finally {
bos.setWriteIndex(oldIndex);
}
// prepare
byte[] buf = new byte[4 + bos.size()];
System.arraycopy(bos.toByteArray(), 0, buf, 4, bos.size());
ChannelBuffer bis = ChannelBuffers.wrappedBuffer(buf);
Object obj = codec.decode((Channel) null, bis);
Assertions.assertNotNull(obj);
Assertions.assertTrue(obj instanceof Response);
Response response = (Response) obj;
Assertions.assertEquals(request.getId(), response.getId());
Assertions.assertTrue(response.getResult() instanceof AppResponse);
AppResponse result = (AppResponse) response.getResult();
Assertions.assertTrue(result.getValue() instanceof String);
Assertions.assertEquals(methodResult.success, result.getValue());
}
use of org.apache.dubbo.remoting.exchange.support.DefaultFuture in project dubbo by alibaba.
the class DeprecatedExchangeCodec method getRequestData.
protected Object getRequestData(long id) {
DefaultFuture future = DefaultFuture.getFuture(id);
if (future == null)
return null;
Request req = future.getRequest();
if (req == null)
return null;
return req.getData();
}
Aggregations