use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.
the class NettyTransporterTest method shouldConnectToNetty4Server.
@Test
public void shouldConnectToNetty4Server() throws Exception {
final CountDownLatch lock = new CountDownLatch(1);
int port = NetUtils.getAvailablePort();
URL url = new URL("telnet", "localhost", port, new String[] { Constants.BIND_PORT_KEY, String.valueOf(port) });
new NettyTransporter().bind(url, new ChannelHandlerAdapter() {
@Override
public void connected(Channel channel) {
lock.countDown();
}
});
new NettyTransporter().connect(url, new ChannelHandlerAdapter() {
@Override
public void sent(Channel channel, Object message) throws RemotingException {
channel.send(message);
channel.close();
}
});
lock.await();
}
use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.
the class HeaderExchangeChannelTest method requestTest03.
@Test
public void requestTest03() throws RemotingException {
Assertions.assertThrows(RemotingException.class, () -> {
channel = new MockChannel() {
@Override
public void send(Object req) throws RemotingException {
throw new RemotingException(channel.getLocalAddress(), channel.getRemoteAddress(), "throw error");
}
};
header = new HeaderExchangeChannel(channel);
Object requestob = new Object();
header.request(requestob, 1000);
});
}
use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.
the class HeaderExchangeHandlerTest method test_received_request_event_other_discard.
@Test
public void test_received_request_event_other_discard() throws RemotingException {
final Request request = new Request();
request.setTwoWay(true);
request.setEvent("my event");
final Channel mchannel = new MockedChannel() {
@Override
public void send(Object message) throws RemotingException {
Assertions.fail();
}
};
HeaderExchangeHandler hexhandler = new HeaderExchangeHandler(new MockedExchangeHandler() {
@Override
public CompletableFuture<Object> reply(ExchangeChannel channel, Object request) throws RemotingException {
Assertions.fail();
throw new RemotingException(channel, "");
}
@Override
public void received(Channel channel, Object message) throws RemotingException {
Assertions.fail();
throw new RemotingException(channel, "");
}
});
hexhandler.received(mchannel, request);
}
use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.
the class NettyChannel method send.
@Override
public void send(Object message, boolean sent) throws RemotingException {
super.send(message, sent);
boolean success = true;
int timeout = 0;
try {
ChannelFuture future = channel.write(message);
if (sent) {
timeout = getUrl().getPositiveParameter(TIMEOUT_KEY, DEFAULT_TIMEOUT);
success = future.await(timeout);
}
Throwable cause = future.getCause();
if (cause != null) {
throw cause;
}
} catch (Throwable e) {
throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + ", cause: " + e.getMessage(), e);
}
if (!success) {
throw new RemotingException(this, "Failed to send message " + PayloadDropper.getRequestWithoutData(message) + " to " + getRemoteAddress() + "in timeout(" + timeout + "ms) limit");
}
}
use of org.apache.dubbo.remoting.RemotingException in project dubbo by alibaba.
the class DeprecatedExchangeCodec method encodeResponse.
protected void encodeResponse(Channel channel, OutputStream os, Response res) throws IOException {
try {
Serialization serialization = CodecSupport.getSerialization(channel.getUrl());
// header.
byte[] header = new byte[HEADER_LENGTH];
// set magic number.
Bytes.short2bytes(MAGIC, header);
// set request and serialization flag.
header[2] = serialization.getContentTypeId();
if (res.isHeartbeat())
header[2] |= FLAG_EVENT;
// set response status.
byte status = res.getStatus();
header[3] = status;
// set request id.
Bytes.long2bytes(res.getId(), header, 4);
UnsafeByteArrayOutputStream bos = new UnsafeByteArrayOutputStream(1024);
ObjectOutput out = serialization.serialize(channel.getUrl(), bos);
// encode response data or error message.
if (status == Response.OK) {
if (res.isHeartbeat()) {
encodeHeartbeatData(channel, out, res.getResult());
} else {
encodeResponseData(channel, out, res.getResult());
}
} else
out.writeUTF(res.getErrorMessage());
out.flushBuffer();
bos.flush();
bos.close();
byte[] data = bos.toByteArray();
checkPayload(channel, data.length);
Bytes.int2bytes(data.length, header, 12);
// write
// write header.
os.write(header);
// write data.
os.write(data);
} catch (Throwable t) {
// send error message to Consumer, otherwise, Consumer will wait until timeout.
if (!res.isEvent() && res.getStatus() != Response.BAD_RESPONSE) {
try {
// FIXME log error info in Codec and put all error handle logic in IoHanndler?
logger.warn("Fail to encode response: " + res + ", send bad_response info instead, cause: " + t.getMessage(), t);
Response r = new Response(res.getId(), res.getVersion());
r.setStatus(Response.BAD_RESPONSE);
r.setErrorMessage("Failed to send response: " + res + ", cause: " + StringUtils.toString(t));
channel.send(r);
return;
} catch (RemotingException e) {
logger.warn("Failed to send bad_response info back: " + res + ", cause: " + e.getMessage(), e);
}
}
// Rethrow exception
if (t instanceof IOException) {
throw (IOException) t;
} else if (t instanceof RuntimeException) {
throw (RuntimeException) t;
} else if (t instanceof Error) {
throw (Error) t;
} else {
throw new RuntimeException(t.getMessage(), t);
}
}
}
Aggregations