Skip to content

Commit

Permalink
handle UserProcessor throw Exception (x-infra-lab#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
JoeCqupt authored Nov 16, 2024
1 parent 9f67f95 commit 7806e28
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 78 deletions.
4 changes: 0 additions & 4 deletions docs/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,6 @@ RpcClient rpcClient = new RpcClient();
rpcClient.startup();
```
### SyncCall
[link](https://github.com/x-infra-lab/x-remoting/blob/main/src/test/java/io/github/xinfra/lab/remoting/rpc/client/RpcClientTest.java#L50)
```java
@Test
public void testSyncCall() throws RemotingException, InterruptedException {
Expand All @@ -77,7 +76,6 @@ public void testSyncCall() throws RemotingException, InterruptedException {
}
```
### AsyncCall - Future
[link](https://github.com/x-infra-lab/x-remoting/blob/main/src/test/java/io/github/xinfra/lab/remoting/rpc/client/RpcClientTest.java#L59)
```java
@Test
public void testAsyncCall1() throws RemotingException, InterruptedException, TimeoutException {
Expand All @@ -90,7 +88,6 @@ public void testAsyncCall1() throws RemotingException, InterruptedException, Tim
}
```
### AsyncCall - Callback
[link](https://github.com/x-infra-lab/x-remoting/blob/main/src/test/java/io/github/xinfra/lab/remoting/rpc/client/RpcClientTest.java#L69)
```java
@Test
public void testAsyncCall2() throws RemotingException, InterruptedException, TimeoutException {
Expand All @@ -117,7 +114,6 @@ public void testAsyncCall2() throws RemotingException, InterruptedException, Tim
}
```
### OnewayCall
[link](https://github.com/x-infra-lab/x-remoting/blob/main/src/test/java/io/github/xinfra/lab/remoting/rpc/client/RpcClientTest.java#L93)
```java
@Test
public void testOnewayCall() throws RemotingException, InterruptedException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,18 +46,13 @@ public class DefaultConnectionFactory implements ConnectionFactory {
private static final Class<? extends SocketChannel> channelClass = Epoll.isAvailable() ? EpollSocketChannel.class
: NioSocketChannel.class;

/**
* Q: why use Supplier to get ChannelHandler? A: some ChannelHandler is
* not @ChannelHandler.Sharable. need create instance every time
*/
public DefaultConnectionFactory(Protocol protocol, List<Supplier<ChannelHandler>> channelHandlerSuppliers) {
this(protocol, channelHandlerSuppliers, new ConnectionConfig());
}

/**
* Q: why use Supplier to get ChannelHandler? A: some ChannelHandler is
* not @ChannelHandler.Sharable. need create instance every time
*/
// Q: why use Supplier to get ChannelHandler?
// A: some ChannelHandler is not @ChannelHandler.Sharable. need create instance every
// time
public DefaultConnectionFactory(Protocol protocol, List<Supplier<ChannelHandler>> channelHandlerSuppliers,
ConnectionConfig connectionConfig) {
Validate.notNull(protocol, "protocol can not be null");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
import lombok.ToString;

/**
* <p>
* request definition:
* <p>
* |protocol:bytes|message-type:byte|request-id:int|serialization-type:byte|content-type-length:short|header-length:short|content-length:int|content-type|header|content|
* <p>
*/

@Setter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ public static <R> R getResponseObject(RpcResponseMessage responseMessage) throws
if (responseMessage.getCause() != null) {
throw new RemotingException("rpc invoke fail. remote address:" + remoteAddress, responseMessage.getCause());
}
else if (responseMessage.getContent() instanceof Throwable) {
throw new RemotingException("rpc invoke fail. remote address:" + remoteAddress,
(Throwable) responseMessage.getContent());
}
else {
throw new RemotingException("rpc invoke fail. remote address:" + remoteAddress);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.github.xinfra.lab.remoting.rpc.client;

import io.github.xinfra.lab.remoting.processor.UserProcessor;

public class ExceptionProcessor implements UserProcessor<ExceptionRequest> {

@Override
public String interest() {
return ExceptionRequest.class.getName();
}

@Override
public Object handRequest(ExceptionRequest request) {
throw new RuntimeException(request.getErrorMsg());
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.github.xinfra.lab.remoting.rpc.client;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
@AllArgsConstructor
public class ExceptionRequest implements Serializable {

private String errorMsg;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.xinfra.lab.remoting.rpc.client;

import io.github.xinfra.lab.remoting.exception.RemotingException;
import io.github.xinfra.lab.remoting.rpc.exception.RpcServerException;
import io.github.xinfra.lab.remoting.rpc.server.RpcServer;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -26,6 +27,7 @@ public static void beforeAll() {
rpcServer.startup();

rpcServer.registerUserProcessor(new SimpleUserProcessor());
rpcServer.registerUserProcessor(new ExceptionProcessor());
}

@AfterAll
Expand Down Expand Up @@ -53,6 +55,20 @@ public void testSyncCall() throws RemotingException, InterruptedException {
Assertions.assertEquals(result, "echo:" + msg);
}

@Test
public void testSyncCallException() throws RemotingException, InterruptedException {
String msg = "test UserProcessor throw Exception";
ExceptionRequest request = new ExceptionRequest(msg);

RemotingException remotingException = Assertions.assertThrows(RemotingException.class, () -> {
rpcClient.syncCall(request, rpcServer.localAddress(), 1000);
});

Assertions.assertInstanceOf(RpcServerException.class, remotingException.getCause());

remotingException.printStackTrace();
}

@Test
public void testAsyncCall1() throws RemotingException, InterruptedException, TimeoutException {
String msg = "hello x-remoting";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

import static io.github.xinfra.lab.remoting.common.TestSocketUtils.findAvailableTcpPort;

public class HeartBeatTest {
public class RpcHeartBeatTest {

private RpcServer rpcServer;

Expand Down

This file was deleted.

0 comments on commit 7806e28

Please sign in to comment.