use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.
the class TraceFilter method invoke.
@Override
public Result invoke(Invoker<?> invoker, Invocation invocation) throws RpcException {
long start = System.currentTimeMillis();
Result result = invoker.invoke(invocation);
long end = System.currentTimeMillis();
if (TRACERS.size() > 0) {
String key = invoker.getInterface().getName() + "." + invocation.getMethodName();
Set<Channel> channels = TRACERS.get(key);
if (channels == null || channels.isEmpty()) {
key = invoker.getInterface().getName();
channels = TRACERS.get(key);
}
if (CollectionUtils.isNotEmpty(channels)) {
for (Channel channel : new ArrayList<>(channels)) {
if (channel.isConnected()) {
try {
int max = 1;
Integer m = (Integer) channel.getAttribute(TRACE_MAX);
if (m != null) {
max = m;
}
int count = 0;
AtomicInteger c = (AtomicInteger) channel.getAttribute(TRACE_COUNT);
if (c == null) {
c = new AtomicInteger();
channel.setAttribute(TRACE_COUNT, c);
}
count = c.getAndIncrement();
if (count < max) {
String prompt = channel.getUrl().getParameter(Constants.PROMPT_KEY, Constants.DEFAULT_PROMPT);
channel.send("\r\n" + RpcContext.getContext().getRemoteAddress() + " -> " + invoker.getInterface().getName() + "." + invocation.getMethodName() + "(" + JSON.toJSONString(invocation.getArguments()) + ")" + " -> " + JSON.toJSONString(result.getValue()) + "\r\nelapsed: " + (end - start) + " ms." + "\r\n\r\n" + prompt);
}
if (count >= max - 1) {
channels.remove(channel);
}
} catch (Throwable e) {
channels.remove(channel);
logger.warn(e.getMessage(), e);
}
} else {
channels.remove(channel);
}
}
}
}
return result;
}
use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.
the class TraceFilterTest method testAddAndRemoveTracer.
@Test
public void testAddAndRemoveTracer() throws Exception {
String method = "sayHello";
Class<?> type = DemoService.class;
String key = type.getName() + "." + method;
// add tracer
TraceFilter.addTracer(type, method, mockChannel, 100);
Assertions.assertEquals(100, mockChannel.getAttribute(TRACE_MAX));
Assertions.assertTrue(mockChannel.getAttribute(TRACE_COUNT) instanceof AtomicInteger);
Field tracers = TraceFilter.class.getDeclaredField(TRACERS_FIELD_NAME);
tracers.setAccessible(true);
ConcurrentHashMap<String, Set<Channel>> o = (ConcurrentHashMap<String, Set<Channel>>) tracers.get(new ConcurrentHashMap<String, Set<Channel>>());
Assertions.assertTrue(o.containsKey(key));
Set<Channel> channels = o.get(key);
Assertions.assertNotNull(channels);
Assertions.assertTrue(channels.contains(mockChannel));
// remove tracer
TraceFilter.removeTracer(type, method, mockChannel);
Assertions.assertNull(mockChannel.getAttribute(TRACE_MAX));
Assertions.assertNull(mockChannel.getAttribute(TRACE_COUNT));
Assertions.assertFalse(channels.contains(mockChannel));
}
use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.
the class DefaultFuture method closeChannel.
/**
* close a channel when a channel is inactive
* directly return the unfinished requests.
*
* @param channel channel to close
*/
public static void closeChannel(Channel channel) {
for (Map.Entry<Long, Channel> entry : CHANNELS.entrySet()) {
if (channel.equals(entry.getValue())) {
DefaultFuture future = getFuture(entry.getKey());
if (future != null && !future.isDone()) {
Response disconnectResponse = new Response(future.getId());
disconnectResponse.setStatus(Response.CHANNEL_INACTIVE);
disconnectResponse.setErrorMessage("Channel " + channel + " is inactive. Directly return the unFinished request : " + (logger.isDebugEnabled() ? future.getRequest() : future.getRequest().copyWithoutData()));
DefaultFuture.received(channel, disconnectResponse);
}
}
}
}
use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.
the class HeaderExchangeServer method getExchangeChannels.
@Override
public Collection<ExchangeChannel> getExchangeChannels() {
Collection<ExchangeChannel> exchangeChannels = new ArrayList<ExchangeChannel>();
Collection<Channel> channels = server.getChannels();
if (CollectionUtils.isNotEmpty(channels)) {
for (Channel channel : channels) {
exchangeChannels.add(HeaderExchangeChannel.getOrAddChannel(channel));
}
}
return exchangeChannels;
}
use of org.apache.dubbo.remoting.Channel in project dubbo by alibaba.
the class AbstractClient method setAttribute.
@Override
public void setAttribute(String key, Object value) {
Channel channel = getChannel();
if (channel == null) {
return;
}
channel.setAttribute(key, value);
}
Aggregations