RabbitMQ Java客户端:为什么JsonRpcServer不允许大多数方法参数类型?

时间:2017-11-09 11:57:33

标签: java rabbitmq rpc json-rpc

我想为JsonRpcServer客户端公开一些方法:

    Map getById(Long o);

每当我尝试使用此方法时,都会抛出运行时异常:

Caused by: com.rabbitmq.tools.jsonrpc.JsonRpcException: {
"code": 500,
"name": "JSONRPCError",
"message": "Internal Server Error",
"error": {
    "localizedMessage": "argument type mismatch",
    "cause": null,
    "stackTrace": [{
        "fileName": "NativeMethodAccessorImpl.java",
        "nativeMethod": true,
        "methodName": "invoke0",
        "className": "sun.reflect.NativeMethodAccessorImpl",
        "lineNumber": -2
    }, {
        "fileName": "NativeMethodAccessorImpl.java",
        "nativeMethod": false,
        "methodName": "invoke",
        "className": "sun.reflect.NativeMethodAccessorImpl",
        "lineNumber": 62
    }, {
        "fileName": "DelegatingMethodAccessorImpl.java",
        "nativeMethod": false,
        "methodName": "invoke",
        "className": "sun.reflect.DelegatingMethodAccessorImpl",
        "lineNumber": 43
    }, {
        "fileName": "Method.java",
        "nativeMethod": false,
        "methodName": "invoke",
        "className": "java.lang.reflect.Method",
        "lineNumber": 483
    }, {
        "fileName": "JsonRpcServer.java",
        "nativeMethod": false,
        "methodName": "doCall",
        "className": "com.rabbitmq.tools.jsonrpc.JsonRpcServer",
        "lineNumber": 143
    }, {
        "fileName": "JsonRpcServer.java",
        "nativeMethod": false,
        "methodName": "handleStringCall",
        "className": "com.rabbitmq.tools.jsonrpc.JsonRpcServer",
        "lineNumber": 103
    }, {
        "fileName": "StringRpcServer.java",
        "nativeMethod": false,
        "methodName": "handleCall",
        "className": "com.rabbitmq.client.StringRpcServer",
        "lineNumber": 48
    }, {
        "fileName": "RpcServer.java",
        "nativeMethod": false,
        "methodName": "handleCall",
        "className": "com.rabbitmq.client.RpcServer",
        "lineNumber": 176
    }, {
        "fileName": "RpcServer.java",
        "nativeMethod": false,
        "methodName": "handleCall",
        "className": "com.rabbitmq.client.RpcServer",
        "lineNumber": 163
    }, {
        "fileName": "RpcServer.java",
        "nativeMethod": false,
        "methodName": "processRequest",
        "className": "com.rabbitmq.client.RpcServer",
        "lineNumber": 149
    }, {
        "fileName": "RpcServer.java",
        "nativeMethod": false,
        "methodName": "mainloop",
        "className": "com.rabbitmq.client.RpcServer",
        "lineNumber": 115
    }, {
        "fileName": "RabbitConfiguration.java",
        "nativeMethod": false,
        "methodName": "lambda$jsonRpcServer$0",
        "className": "com.ofaly.comments.RabbitConfiguration",
        "lineNumber": 56
    }, {
        "fileName": null,
        "nativeMethod": false,
        "methodName": "run",
        "className": "com.ofaly.comments.RabbitConfiguration$$Lambda$7\/82172068",
        "lineNumber": -1
    }, {
        "fileName": "Thread.java",
        "nativeMethod": false,
        "methodName": "run",
        "className": "java.lang.Thread",
        "lineNumber": 745
    }],
    "suppressed": [],
    "message": "argument type mismatch"
}

}

当我将参数的类型更改为String时,它可以工作......并且它似乎不接受String和map旁边的任何其他类型。甚至对象也不会作为论据。

为什么JsonRpcClient不接受对象?

配置:

我正在使用Rabbit Mq JsonRPC客户端和Spring启动。 我的配置如下:

@Value("${spring.rabbitmq.host}")
private String rabbitmqHost;

@Value("${spring.rabbitmq.queue}")
private String rabbitmqQueue;

private final IRPCService rpcService;

public RabbitConfiguration(final IRPCService rpcService) {
    this.rpcService = rpcService;
}

@Bean
public Connection rabbitmqConnection() throws IOException, TimeoutException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost(rabbitmqHost);
    return factory.newConnection();
}

@Bean
public Channel rpcChanel() throws IOException, TimeoutException {
    Connection connection = rabbitmqConnection();
    Channel channel = connection.createChannel();
    channel.queueDeclare(rabbitmqQueue, false, false, false, null);
    return channel;
}

@Bean
@Lazy(false)
public JsonRpcServer jsonRpcServer() throws IOException, TimeoutException {
    JsonRpcServer jsonRpcServer = new JsonRpcServer(rpcChanel(), rabbitmqQueue, IRPCService.class, rpcService);
    new Thread(() -> {
        try {
            jsonRpcServer.mainloop();
        } catch (Exception e) {
            System.out.println(e);
        }
    }).start();
    return jsonRpcServer;
}

我这样使用它:

@Autowired
private ProxyRpcBuilder rpcBuilder;
....
rpcBuilder.create(IRPCService.class).getById(commentId);

我的代理看起来像这样:

@Component
public class ProxyRpcBuilder {

@Value("${spring.rabbitmq.queue}")
private String rabbitmqQueue;

private final Channel channel;
private JsonRpcClient jsonRpcClient;

public ProxyRpcBuilder(Channel channel) {
    this.channel = channel;
}

public <T> T create(Class<T> t) {
    try {
        if (jsonRpcClient == null) {
            jsonRpcClient = new JsonRpcClient(channel, "", rabbitmqQueue, 20000);
        }
        return (T) jsonRpcClient.createProxy(t);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
} ...

0 个答案:

没有答案