@@ -41,10 +41,10 @@ public abstract class BaseRemoting {
4141 .getLogger ("CommonDefault" );
4242 private final static long ABANDONING_REQUEST_THRESHOLD = 0L ;
4343
44- protected CommandFactory commandFactory ;
44+ private CommandFactory defalutCommandFactory ;
4545
4646 public BaseRemoting (CommandFactory commandFactory ) {
47- this .commandFactory = commandFactory ;
47+ this .defalutCommandFactory = commandFactory ;
4848 }
4949
5050 /**
@@ -69,7 +69,7 @@ protected RemotingCommand invokeSync(final Connection conn, final RemotingComman
6969 request .getId (),
7070 conn .getUrl () != null ? conn .getUrl () : RemotingUtil .parseRemoteAddress (conn
7171 .getChannel ()));
72- return this .commandFactory .createTimeoutResponse (conn .getRemoteAddress ());
72+ return this .getCommandFactory ( conn ) .createTimeoutResponse (conn .getRemoteAddress ());
7373 }
7474
7575 final InvokeFuture future = createInvokeFuture (request , request .getInvokeContext ());
@@ -86,7 +86,7 @@ protected RemotingCommand invokeSync(final Connection conn, final RemotingComman
8686 public void operationComplete (ChannelFuture f ) throws Exception {
8787 if (!f .isSuccess ()) {
8888 conn .removeInvokeFuture (requestId );
89- future .putResponse (commandFactory .createSendFailedResponse (
89+ future .putResponse (getCommandFactory ( conn ) .createSendFailedResponse (
9090 conn .getRemoteAddress (), f .cause ()));
9191 LOGGER .error ("Invoke send failed, id={}" , requestId , f .cause ());
9292 }
@@ -98,7 +98,8 @@ public void operationComplete(ChannelFuture f) throws Exception {
9898 }
9999 } catch (Exception e ) {
100100 conn .removeInvokeFuture (requestId );
101- future .putResponse (commandFactory .createSendFailedResponse (conn .getRemoteAddress (), e ));
101+ future .putResponse (getCommandFactory (conn ).createSendFailedResponse (
102+ conn .getRemoteAddress (), e ));
102103 LOGGER .error ("Exception caught when sending invocation, id={}" , requestId , e );
103104 }
104105 RemotingCommand response = future .waitResponse (remainingTime );
@@ -109,7 +110,7 @@ public void operationComplete(ChannelFuture f) throws Exception {
109110
110111 if (response == null ) {
111112 conn .removeInvokeFuture (requestId );
112- response = this .commandFactory .createTimeoutResponse (conn .getRemoteAddress ());
113+ response = this .getCommandFactory ( conn ) .createTimeoutResponse (conn .getRemoteAddress ());
113114 LOGGER .warn ("Wait response, request id={} timeout!" , requestId );
114115 }
115116
@@ -137,7 +138,8 @@ protected void invokeWithCallback(final Connection conn, final RemotingCommand r
137138 request .getId (),
138139 conn .getUrl () != null ? conn .getUrl () : RemotingUtil .parseRemoteAddress (conn
139140 .getChannel ()));
140- future .putResponse (commandFactory .createTimeoutResponse (conn .getRemoteAddress ()));
141+ future .putResponse (getCommandFactory (conn ).createTimeoutResponse (
142+ conn .getRemoteAddress ()));
141143 future .tryAsyncExecuteInvokeCallbackAbnormally ();
142144 return ;
143145 }
@@ -149,8 +151,8 @@ protected void invokeWithCallback(final Connection conn, final RemotingCommand r
149151 public void run (Timeout timeout ) throws Exception {
150152 InvokeFuture future = conn .removeInvokeFuture (requestId );
151153 if (future != null ) {
152- future .putResponse (commandFactory .createTimeoutResponse (conn
153- .getRemoteAddress ()));
154+ future .putResponse (getCommandFactory ( conn ) .createTimeoutResponse (
155+ conn .getRemoteAddress ()));
154156 future .tryAsyncExecuteInvokeCallbackAbnormally ();
155157 }
156158 }
@@ -165,7 +167,7 @@ public void operationComplete(ChannelFuture cf) throws Exception {
165167 InvokeFuture f = conn .removeInvokeFuture (requestId );
166168 if (f != null ) {
167169 f .cancelTimeout ();
168- f .putResponse (commandFactory .createSendFailedResponse (
170+ f .putResponse (getCommandFactory ( conn ) .createSendFailedResponse (
169171 conn .getRemoteAddress (), cf .cause ()));
170172 f .tryAsyncExecuteInvokeCallbackAbnormally ();
171173 }
@@ -179,7 +181,8 @@ public void operationComplete(ChannelFuture cf) throws Exception {
179181 InvokeFuture f = conn .removeInvokeFuture (requestId );
180182 if (f != null ) {
181183 f .cancelTimeout ();
182- f .putResponse (commandFactory .createSendFailedResponse (conn .getRemoteAddress (), e ));
184+ f .putResponse (getCommandFactory (conn ).createSendFailedResponse (
185+ conn .getRemoteAddress (), e ));
183186 f .tryAsyncExecuteInvokeCallbackAbnormally ();
184187 }
185188 LOGGER .error ("Exception caught when sending invocation. The address is {}" ,
@@ -208,7 +211,8 @@ protected InvokeFuture invokeWithFuture(final Connection conn, final RemotingCom
208211 request .getId (),
209212 conn .getUrl () != null ? conn .getUrl () : RemotingUtil .parseRemoteAddress (conn
210213 .getChannel ()));
211- future .putResponse (commandFactory .createTimeoutResponse (conn .getRemoteAddress ()));
214+ future .putResponse (getCommandFactory (conn ).createTimeoutResponse (
215+ conn .getRemoteAddress ()));
212216 return future ;
213217 }
214218
@@ -219,8 +223,8 @@ protected InvokeFuture invokeWithFuture(final Connection conn, final RemotingCom
219223 public void run (Timeout timeout ) throws Exception {
220224 InvokeFuture future = conn .removeInvokeFuture (requestId );
221225 if (future != null ) {
222- future .putResponse (commandFactory .createTimeoutResponse (conn
223- .getRemoteAddress ()));
226+ future .putResponse (getCommandFactory ( conn ) .createTimeoutResponse (
227+ conn .getRemoteAddress ()));
224228 }
225229 }
226230
@@ -235,7 +239,7 @@ public void operationComplete(ChannelFuture cf) throws Exception {
235239 InvokeFuture f = conn .removeInvokeFuture (requestId );
236240 if (f != null ) {
237241 f .cancelTimeout ();
238- f .putResponse (commandFactory .createSendFailedResponse (
242+ f .putResponse (getCommandFactory ( conn ) .createSendFailedResponse (
239243 conn .getRemoteAddress (), cf .cause ()));
240244 }
241245 LOGGER .error ("Invoke send failed. The address is {}" ,
@@ -248,7 +252,8 @@ public void operationComplete(ChannelFuture cf) throws Exception {
248252 InvokeFuture f = conn .removeInvokeFuture (requestId );
249253 if (f != null ) {
250254 f .cancelTimeout ();
251- f .putResponse (commandFactory .createSendFailedResponse (conn .getRemoteAddress (), e ));
255+ f .putResponse (getCommandFactory (conn ).createSendFailedResponse (
256+ conn .getRemoteAddress (), e ));
252257 }
253258 LOGGER .error ("Exception caught when sending invocation. The address is {}" ,
254259 RemotingUtil .parseRemoteAddress (conn .getChannel ()), e );
@@ -323,8 +328,27 @@ protected abstract InvokeFuture createInvokeFuture(final Connection conn,
323328 final InvokeContext invokeContext ,
324329 final InvokeCallback invokeCallback );
325330
331+ @ Deprecated
326332 protected CommandFactory getCommandFactory () {
327- return commandFactory ;
333+ LOGGER
334+ .warn ("The method getCommandFactory() is deprecated. Please use getCommandFactory(ProtocolCode/Connection) instead." );
335+ return defalutCommandFactory ;
336+ }
337+
338+ protected CommandFactory getCommandFactory (Connection conn ) {
339+ ProtocolCode protocolCode = conn .getChannel ().attr (Connection .PROTOCOL ).get ();
340+ return getCommandFactory (protocolCode );
341+ }
342+
343+ protected CommandFactory getCommandFactory (ProtocolCode protocolCode ) {
344+ if (protocolCode == null ) {
345+ return getCommandFactory ();
346+ }
347+ Protocol protocol = ProtocolManager .getProtocol (protocolCode );
348+ if (protocol == null ) {
349+ return getCommandFactory ();
350+ }
351+ return protocol .getCommandFactory ();
328352 }
329353
330354 private int remainingTime (RemotingCommand request , int timeout ) {
0 commit comments