我是Avro的新手,尝试模拟RPC,但是我不想使用Avro协议文件然后生成Java类,而是想使用现有的Java接口,但是遇到了一些{{1} }。这是我的源代码:
ClassCastException
AvroMsgInterface.java
package de.elcattivo.example.avro;
import org.apache.avro.reflect.ReflectData;
public interface AvroMsgInterface {
public static final org.apache.avro.Protocol PROTOCOL = ReflectData.get().getProtocol(AvroMsgInterface.class);
Person invoke(String ruleset, Person ao);
}
AvroMessageImpl.java
package de.elcattivo.example.avro;
public class AvroMessageImpl implements AvroMsgInterface {
@Override
public Person invoke(String ruleset, Person ao) {
Person result = ao;
result.setName("Jim");
return null;
}
}
AvroTest.java
最后是package de.elcattivo.example.avro;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Arrays;
import javax.xml.validation.SchemaFactory;
import org.apache.avro.Protocol;
import org.apache.avro.Schema;
import org.apache.avro.SchemaBuilder;
import org.apache.avro.ipc.NettyServer;
import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.Responder;
import org.apache.avro.ipc.Server;
import org.apache.avro.ipc.specific.SpecificRequestor;
import org.apache.avro.ipc.specific.SpecificResponder;
import org.apache.avro.reflect.ReflectData;
import example.proto.AOSender;
import example.proto.AvroPerson;
public class AvroTest {
private static Server server;
public static void main(String[] args) throws IOException {
Protocol prot = ReflectData.get().getProtocol(AvroMsgInterface.class);
Responder responder = new SpecificResponder(prot, new AvroMessageImpl());
server = new NettyServer(responder, new InetSocketAddress(InetAddress.getLocalHost(), 65125));
try(NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(InetAddress.getLocalHost(), 65125))) {
AvroMsgInterface proxy = (AvroMsgInterface) SpecificRequestor.getClient(AvroMsgInterface.class, client);
proxy.invoke("test", new Person("John", 42));
} catch (Exception e) {
e.printStackTrace();
}
server.close();
}
}
Person.java
关于如何进行的任何建议?我知道在avro自动生成的接口和类中,还有很多事情要做,但是正如我所说,我想避免这些事情。