mirror of https://github.com/longtai-cn/hippo4j
Rpc simplify (#1212)
* fix : Add annotations to classes of rpc module Add version: @since 1.5.1 (#1187) (#812) * fix : Simplify the rpc module, remove unnecessary classes and methods (#1187) (#812) * fix : Adjust unit tests against simplified code(#1187) (#812) * fix : Adjust unit tests against simplified code(#1187) (#812) * fix : Add new unit tests(#1187) (#812)pull/1219/head
parent
f1321052c1
commit
9a1089f640
@ -1,50 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.coder;
|
||||
|
||||
import cn.hippo4j.rpc.exception.CoderException;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import io.netty.handler.codec.serialization.ClassResolver;
|
||||
import io.netty.handler.codec.serialization.ObjectDecoder;
|
||||
|
||||
/**
|
||||
* According to the decoder for java objects implemented by ObjectDecoder,
|
||||
* it is necessary to ensure that the transmitted objects can be serialized
|
||||
*/
|
||||
public class NettyDecoder extends ObjectDecoder {
|
||||
|
||||
public NettyDecoder(ClassResolver classResolver) {
|
||||
super(classResolver);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Object decode(ChannelHandlerContext ctx, ByteBuf in) {
|
||||
ByteBuf byteBuf = in.retainedDuplicate();
|
||||
try {
|
||||
Object o = super.decode(ctx, in);
|
||||
if (o == null) {
|
||||
return byteBuf;
|
||||
} else {
|
||||
return o;
|
||||
}
|
||||
} catch (Exception e) {
|
||||
throw new CoderException("The encoding is abnormal, which may be caused by the failure of the transfer object to be deserialized");
|
||||
}
|
||||
}
|
||||
}
|
@ -1,60 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.process;
|
||||
|
||||
import cn.hippo4j.rpc.model.Request;
|
||||
import cn.hippo4j.rpc.model.Response;
|
||||
|
||||
/**
|
||||
* Callback while the connection is in progress
|
||||
*/
|
||||
public interface ActivePostProcess {
|
||||
|
||||
/**
|
||||
* Client: After establishing a connection and before passing parameters<br>
|
||||
* Server: Receives parameters and performs pre-call operations<br>
|
||||
*
|
||||
* @param request request
|
||||
* @return Whether to continue the execution. If it is a client, the returned value does not affect subsequent execution
|
||||
*/
|
||||
default boolean preHandler(Request request) {
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Client: Action after receiving a response<br>
|
||||
* Server: performs the operation after the call<br>
|
||||
*
|
||||
* @param request request
|
||||
* @param response response
|
||||
*/
|
||||
default void postHandler(Request request, Response response) {
|
||||
// NO SOMETHING
|
||||
}
|
||||
|
||||
/**
|
||||
* Called when an exception or resource is cleaned
|
||||
*
|
||||
* @param request request
|
||||
* @param response response
|
||||
* @param e Exception
|
||||
*/
|
||||
default void afterCompletion(Request request, Response response, Exception e) {
|
||||
// NO SOMETHING
|
||||
}
|
||||
}
|
@ -1,102 +0,0 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.process;
|
||||
|
||||
import cn.hippo4j.rpc.model.Request;
|
||||
import cn.hippo4j.rpc.model.Response;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Processor chain for easier processing of processors in different scenarios<br>
|
||||
* reference resources: spring HandlerExecutionChain
|
||||
*
|
||||
* @see ActivePostProcess
|
||||
*/
|
||||
@Slf4j
|
||||
public final class ActiveProcessChain {
|
||||
|
||||
/**
|
||||
* A collection of processors that will be applied to their assigned programs.
|
||||
* Processors will perform different actions on different occasions for both the server and the client,
|
||||
* but the execution period of that action must be the same
|
||||
*/
|
||||
List<ActivePostProcess> processes;
|
||||
|
||||
/**
|
||||
* index <br>
|
||||
* that identifies where the {@link ActivePostProcess#preHandler(Request)} processing is performed<br>
|
||||
* This allows for the fact that some processors will add shutable operations to the class<br>
|
||||
* eg: {@link java.io.Closeable}, The {@link ActivePostProcess#afterCompletion(Request, Response, Exception)}
|
||||
* operation is not performed after an exception if the preprocessor is not executed
|
||||
*/
|
||||
int index = -1;
|
||||
|
||||
public ActiveProcessChain(List<ActivePostProcess> processes) {
|
||||
this.processes = processes;
|
||||
}
|
||||
|
||||
public ActiveProcessChain(ActivePostProcess... processes) {
|
||||
this((processes != null ? Arrays.asList(processes) : Collections.emptyList()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply postHandle methods of registered processes.
|
||||
*/
|
||||
public boolean applyPreHandle(Request request) {
|
||||
for (int i = 0; i < this.processes.size(); i++) {
|
||||
ActivePostProcess handle = processes.get(i);
|
||||
if (!handle.preHandler(request)) {
|
||||
afterCompletion(request, null, null);
|
||||
return false;
|
||||
}
|
||||
this.index = i;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* Apply postHandle methods of registered processes.
|
||||
*/
|
||||
public void applyPostHandle(Request request, Response response) {
|
||||
for (int i = processes.size() - 1; i >= 0; i--) {
|
||||
ActivePostProcess handle = processes.get(i);
|
||||
handle.postHandler(request, response);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Trigger afterCompletion callbacks on the mapped ActivePostProcess.
|
||||
* Will just invoke afterCompletion for all interceptors whose preHandle invocation
|
||||
* has successfully completed and returned true.
|
||||
*/
|
||||
public void afterCompletion(Request request, Response response, Exception ex) {
|
||||
for (int i = this.index; i >= 0; i--) {
|
||||
ActivePostProcess handle = processes.get(i);
|
||||
try {
|
||||
handle.afterCompletion(request, response, ex);
|
||||
} catch (Throwable e) {
|
||||
log.error("HandlerInterceptor.afterCompletion threw exception", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,33 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.ServerSocket;
|
||||
|
||||
public class RandomPort {
|
||||
|
||||
public static int getSafeRandomPort() {
|
||||
try (ServerSocket socket = new ServerSocket(0)) {
|
||||
return socket.getLocalPort();
|
||||
} catch (IOException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,39 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.handler;
|
||||
|
||||
import io.netty.channel.ChannelHandler;
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
|
||||
public class TestFalseHandler implements ChannelHandler {
|
||||
|
||||
@Override
|
||||
public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
|
||||
|
||||
}
|
||||
}
|
@ -0,0 +1,72 @@
|
||||
/*
|
||||
* Licensed to the Apache Software Foundation (ASF) under one or more
|
||||
* contributor license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright ownership.
|
||||
* The ASF licenses this file to You under the Apache License, Version 2.0
|
||||
* (the "License"); you may not use this file except in compliance with
|
||||
* the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package cn.hippo4j.rpc.support;
|
||||
|
||||
import cn.hippo4j.common.toolkit.ThreadUtil;
|
||||
import cn.hippo4j.rpc.client.*;
|
||||
import cn.hippo4j.rpc.discovery.ClassRegistry;
|
||||
import cn.hippo4j.rpc.discovery.DefaultInstance;
|
||||
import cn.hippo4j.rpc.discovery.Instance;
|
||||
import cn.hippo4j.rpc.discovery.ServerPort;
|
||||
import cn.hippo4j.rpc.handler.NettyClientPoolHandler;
|
||||
import cn.hippo4j.rpc.handler.NettyClientTakeHandler;
|
||||
import cn.hippo4j.rpc.handler.NettyServerTakeHandler;
|
||||
import cn.hippo4j.rpc.handler.TestHandler;
|
||||
import cn.hippo4j.rpc.model.DefaultRequest;
|
||||
import cn.hippo4j.rpc.model.Request;
|
||||
import cn.hippo4j.rpc.model.Response;
|
||||
import cn.hippo4j.rpc.server.NettyServerConnection;
|
||||
import cn.hippo4j.rpc.server.RPCServer;
|
||||
import io.netty.channel.pool.ChannelPoolHandler;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
public class NettyClientSupportTest {
|
||||
|
||||
@Test
|
||||
public void closeTest() throws IOException {
|
||||
int port = RandomPort.getSafeRandomPort();
|
||||
ServerPort serverPort = () -> port;
|
||||
Class<CallManager> cls = CallManager.class;
|
||||
String className = cls.getName();
|
||||
ClassRegistry.put(className, cls);
|
||||
// The mode connection was denied when the server was started on the specified port
|
||||
Instance instance = new DefaultInstance();
|
||||
NettyServerTakeHandler handler = new NettyServerTakeHandler(instance);
|
||||
NettyServerConnection connection = new NettyServerConnection(handler);
|
||||
connection.addLast("Test", new TestHandler());
|
||||
RPCServer rpcServer = new RPCServer(connection, serverPort);
|
||||
rpcServer.bind();
|
||||
while (!rpcServer.isActive()) {
|
||||
ThreadUtil.sleep(100L);
|
||||
}
|
||||
InetSocketAddress address = InetSocketAddress.createUnresolved("localhost", port);
|
||||
ChannelPoolHandler channelPoolHandler = new NettyClientPoolHandler(new NettyClientTakeHandler());
|
||||
ClientConnection clientConnection = new NettyClientConnection(address, channelPoolHandler);
|
||||
RPCClient rpcClient = new RPCClient(clientConnection);
|
||||
|
||||
NettyClientSupport.closeClient(new InetSocketAddress("localhost", port));
|
||||
|
||||
rpcClient.close();
|
||||
rpcServer.close();
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue