Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of dubbo's WrappedChannelHandler?

2025-01-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces the relevant knowledge of "what is the role of WrappedChannelHandler of dubbo". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Order

This paper mainly studies the WrappedChannelHandler of dubbo.

WrappedChannelHandler

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/WrappedChannelHandler.java

Public class WrappedChannelHandler implements ChannelHandlerDelegate {protected static final Logger logger = LoggerFactory.getLogger (WrappedChannelHandler.class); protected static final ExecutorService SHARED_EXECUTOR = Executors.newCachedThreadPool (new NamedThreadFactory ("DubboSharedHandler", true)); protected final ExecutorService executor; protected final ChannelHandler handler; protected final URL url; public WrappedChannelHandler (ChannelHandler handler, URL url) {this.handler = handler; this.url = url; executor = (ExecutorService) ExtensionLoader.getExtensionLoader (ThreadPool.class). GetAdaptiveExtension (). GetExecutor (url) String componentKey = Constants.EXECUTOR_SERVICE_COMPONENT_KEY; if (CONSUMER_SIDE.equalsIgnoreCase (url.getParameter (SIDE_KEY) {componentKey = CONSUMER_SIDE;} DataStore dataStore = ExtensionLoader.getExtensionLoader (DataStore.class). GetDefaultExtension (); dataStore.put (componentKey, Integer.toString (url.getPort ()), executor) } public void close () {try {if (executor! = null) {executor.shutdown ();}} catch (Throwable t) {logger.warn ("fail to destroy thread pool of server:" + t.getMessage (), t);} @ Override public void connected (Channel channel) throws RemotingException {handler.connected (channel) @ Override public void disconnected (Channel channel) throws RemotingException {handler.disconnected (channel);} @ Override public void sent (Channel channel, Object message) throws RemotingException {handler.sent (channel, message);} @ Override public void received (Channel channel, Object message) throws RemotingException {handler.received (channel, message);} @ Override public void caught (Channel channel, Throwable exception) throws RemotingException {handler.caught (channel, exception) } public ExecutorService getExecutor () {return executor;} @ Override public ChannelHandler getHandler () {if (handler instanceof ChannelHandlerDelegate) {return ((ChannelHandlerDelegate) handler) .getHandler ();} else {return handler;}} public URL getUrl () {return url;} public ExecutorService getExecutorService () {ExecutorService cexecutor = executor If (cexecutor = = null | | cexecutor.isShutdown ()) {cexecutor = SHARED_EXECUTOR;} return cexecutor;}}

The construction of WrappedChannelHandler is based on ExtensionLoader.getExtensionLoader (ThreadPool.class). GetAdaptiveExtension (). GetExecutor (url) to get the ExecutorService and put it in the dataStore

ExecutionChannelHandler

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/execution/ExecutionChannelHandler.java

Public class ExecutionChannelHandler extends WrappedChannelHandler {public ExecutionChannelHandler (ChannelHandler handler, URL url) {super (handler, url);} @ Override public void received (Channel channel, Object message) throws RemotingException {ExecutorService executor = getExecutorService (); if (message instanceof Request) {try {executor.execute (new ChannelEventRunnable (channel, handler, ChannelState.RECEIVED, message)) } catch (Throwable t) {/ / FIXME: when the thread pool is full, SERVER_THREADPOOL_EXHAUSTED_ERROR cannot return properly, / / therefore the consumer side has to wait until gets timeout. This is a temporary solution to prevent / / this scenario from happening, but a better solution should be considered later. If (t instanceof RejectedExecutionException) {Request request = (Request) message; if (request.isTwoWay ()) {String msg = "Server side (" + url.getIp () + "," + url.getPort () + ") thread pool is exhausted, detail msg:" + t.getMessage () Response response = new Response (request.getId (), request.getVersion ()); response.setStatus (Response.SERVER_THREADPOOL_EXHAUSTED_ERROR); response.setErrorMessage (msg); channel.send (response); return } throw new ExecutionException (message, channel, getClass () + "error when process received event.", t);} else {handler.received (channel, message);}

ExecutionChannelHandler inherits WrappedChannelHandler, and its received creates a ChannelEventRunnable and then puts it into executor to execute

ChannelEventRunnable

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/dispatcher/ChannelEventRunnable.java

Public class ChannelEventRunnable implements Runnable {private static final Logger logger = LoggerFactory.getLogger (ChannelEventRunnable.class); private final ChannelHandler handler; private final Channel channel; private final ChannelState state; private final Throwable exception; private final Object message; public ChannelEventRunnable (Channel channel, ChannelHandler handler, ChannelState state) {this (channel, handler, state, null);} public ChannelEventRunnable (Channel channel, ChannelHandler handler, ChannelState state, Object message) {this (channel, handler, state, message, null) } public ChannelEventRunnable (Channel channel, ChannelHandler handler, ChannelState state, Throwable t) {this (channel, handler, state, null, t);} public ChannelEventRunnable (Channel channel, ChannelHandler handler, ChannelState state, Object message, Throwable exception) {this.channel = channel; this.handler = handler; this.state = state; this.message = message; this.exception = exception } @ Override public void run () {if (state = = ChannelState.RECEIVED) {try {handler.received (channel, message);} catch (Exception e) {logger.warn ("ChannelEventRunnable handle" + state + "operation error, channel is" + channel + ", message is" + message, e) } else {switch (state) {case CONNECTED: try {handler.connected (channel);} catch (Exception e) {logger.warn ("ChannelEventRunnable handle" + state + "operation error, channel is" + channel, e);} break Case DISCONNECTED: try {handler.disconnected (channel);} catch (Exception e) {logger.warn ("ChannelEventRunnable handle" + state + "operation error, channel is" + channel, e);} break Case SENT: try {handler.sent (channel, message);} catch (Exception e) {logger.warn ("ChannelEventRunnable handle" + state + "operation error, channel is" + channel + ", message is" + message, e);} break Case CAUGHT: try {handler.caught (channel, exception);} catch (Exception e) {logger.warn ("ChannelEventRunnable handle" + state + "operation error, channel is" + channel + ", message is:" + message + ", exception is" + exception, e) } break; default: logger.warn ("unknown state:" + state + ", message is" + message) } / * ChannelState * / public enum ChannelState {/ * CONNECTED * / CONNECTED, / * DISCONNECTED * / DISCONNECTED, / * SENT * / SENT / * * RECEIVED * / RECEIVED, / * CAUGHT * / CAUGHT}}

ChannelEventRunnable implements the Runnable interface, and its run method is handled differently according to different ChannelState.

Summary

The construction of WrappedChannelHandler is based on ExtensionLoader.getExtensionLoader (ThreadPool.class). GetAdaptiveExtension (). GetExecutor (url) to get the ExecutorService and put it in the dataStore

ExecutionChannelHandler inherits WrappedChannelHandler, and its received creates a ChannelEventRunnable and then puts it into executor to execute

ChannelEventRunnable implements the Runnable interface, and its run method is handled differently according to different ChannelState.

This is the end of the content of "what is the use of dubbo's WrappedChannelHandler"? thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 286

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report