use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.
the class HeaderExchangeHandler method received.
public void received(Channel channel, Object message) throws RemotingException {
channel.setAttribute(KEY_READ_TIMESTAMP, System.currentTimeMillis());
ExchangeChannel exchangeChannel = HeaderExchangeChannel.getOrAddChannel(channel);
try {
if (message instanceof Request) {
// handle request.
Request request = (Request) message;
if (request.isEvent()) {
handlerEvent(channel, request);
} else {
if (request.isTwoWay()) {
Response response = handleRequest(exchangeChannel, request);
channel.send(response);
} else {
handler.received(exchangeChannel, request.getData());
}
}
} else if (message instanceof Response) {
handleResponse(channel, (Response) message);
} else if (message instanceof String) {
if (isClientSide(channel)) {
Exception e = new Exception("Dubbo client can not supported string message: " + message + " in channel: " + channel + ", url: " + channel.getUrl());
logger.error(e.getMessage(), e);
} else {
String echo = handler.telnet(channel, (String) message);
if (echo != null && echo.length() > 0) {
channel.send(echo);
}
}
} else {
handler.received(exchangeChannel, message);
}
} finally {
HeaderExchangeChannel.removeChannelIfDisconnected(channel);
}
}
use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.
the class Main method dataPackageTest.
static void dataPackageTest(int port) throws Exception {
ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
Random random = new Random();
for (int i = 5; i < 100; i++) {
StringBuilder sb = new StringBuilder();
for (int j = 0; j < i * 100; j++) sb.append("(" + random.nextLong() + ")");
Main.Data d = new Main.Data();
d.setData(sb.toString());
client.request(d).get();
}
System.out.println("send finished.");
}
use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.
the class Main method test.
private static void test(int port) throws Exception {
ExchangeChannel client = Exchangers.connect(URL.valueOf("dubbo://localhost:" + port));
MockResult result = (MockResult) client.request(new RpcMessage(DemoService.class.getName(), "plus", new Class<?>[] { int.class, int.class }, new Object[] { 55, 25 })).get();
System.out.println("55+25=" + result.getResult());
for (int i = 0; i < 100; i++) client.request(new RpcMessage(DemoService.class.getName(), "sayHello", new Class<?>[] { String.class }, new Object[] { "qianlei" + i }));
for (int i = 0; i < 100; i++) client.request(new Main.Data());
System.out.println("=====test invoke=====");
for (int i = 0; i < 100; i++) {
ResponseFuture future = client.request(new Main.Data());
System.out.println("invoke and get");
System.out.println("invoke result:" + future.get());
}
System.out.println("=====the end=====");
}
use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.
the class NettyClientTest method testClientClose.
@Test
public void testClientClose() throws Exception {
List<ExchangeChannel> clients = new ArrayList<ExchangeChannel>(100);
for (int i = 0; i < 100; i++) {
ExchangeChannel client = Exchangers.connect(URL.valueOf("exchange://localhost:10001?client=netty"));
Thread.sleep(5);
clients.add(client);
}
for (ExchangeChannel client : clients) {
client.close();
}
Thread.sleep(1000);
}
use of com.alibaba.dubbo.remoting.exchange.ExchangeChannel in project dubbo by alibaba.
the class ExchangeServerPeer method getExchangeChannel.
@Override
public ExchangeChannel getExchangeChannel(InetSocketAddress remoteAddress) {
String host = remoteAddress.getAddress() != null ? remoteAddress.getAddress().getHostAddress() : remoteAddress.getHostName();
int port = remoteAddress.getPort();
ExchangeChannel channel = super.getExchangeChannel(remoteAddress);
if (channel == null) {
for (Map.Entry<URL, ExchangeClient> entry : clients.entrySet()) {
URL url = entry.getKey();
if (url.getIp().equals(host) && url.getPort() == port) {
return entry.getValue();
}
}
}
return channel;
}
Aggregations