avro-ipc的HelloWorld例子
pom.xml
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro</artifactId>
</dependency>
<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro-ipc</artifactId>
</dependency>
...
<plugin>
<groupId>org.apache.avro</groupId>
<artifactId>avro-maven-plugin</artifactId>
<executions>
<execution>
<phase>generate-sources</phase>
<goals>
<goal>protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
<outputDirectory>${project.basedir}/src/main/java/</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>
helloworld.avpr
{
"namespace": "com.sanss.hadoop.demos.avro.proto",
"protocol": "HelloWorld",
"doc": "Protocol Greetings",
"types": [
{"name": "Greeting", "type": "record", "fields": [
{"name": "message", "type": "string"}]},
{"name": "Curse", "type": "error", "fields": [
{"name": "message", "type": "string"}]}
],
"messages": {
"hello": {
"doc": "Say hello.",
"request": [{"name": "greeting", "type": "Greeting" }],
"response": "Greeting",
"errors": ["Curse"]
}
}
}
生成java protocol code
mvn clean compile
HelloWorldImpl.java
public class HelloWorldImpl implements HelloWorld {
@Override
public Greeting hello(Greeting greeting) throws AvroRemoteException, Curse {
if(greeting.getMessage().toString().equalsIgnoreCase("how are you")){
greeting.setMessage("not too bad");
return greeting;
}
return new Greeting("hello");
}
}
IpcServer.java
public class IpcServer {
private Server server;
public void startServer() throws IOException, InterruptedException {
server = new NettyServer(new SpecificResponder(HelloWorld.class,
new HelloWorldImpl()), new InetSocketAddress(65000));
}
public void stopServer() {
server.close();
}
public static void main(String[] args) throws IOException, InterruptedException {
System.out.println("Starting server");
IpcServer ipcServer = new IpcServer();
ipcServer.startServer();
System.out.println("Server started");
}
}
Client.java
public class Client {
public static void main(String[] args) throws IOException {
NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65000));
HelloWorld proxy = (HelloWorld) SpecificRequestor.getClient(HelloWorld.class, client);
System.out.println("Client built, got proxy");
Greeting greeting = new Greeting("how are you");
System.out.println("Calling proxy.hello with message: " + greeting.toString());
System.out.println("Result: " +proxy.hello(greeting));
// cleanup
client.close();
}
}
运行结果
Client built, got proxy
Calling proxy.hello with message: {"message": "how are you"}
Result: {"message": "not too bad"}
分享到:
相关推荐
赠送jar包:avro-ipc-1.8.2.jar; 赠送原API文档:avro-ipc-1.8.2-javadoc.jar; 赠送源代码:avro-ipc-1.8.2-sources.jar; 赠送Maven依赖信息文件:avro-ipc-1.8.2.pom; 包含翻译后的API文档:avro-ipc-1.8.2-...
avro-ipc-1.7.7.jar
赠送jar包:avro-ipc-1.7.7.jar; 赠送原API文档:avro-ipc-1.7.7-javadoc.jar; 赠送源代码:avro-ipc-1.7.7-sources.jar; 赠送Maven依赖信息文件:avro-ipc-1.7.7.pom; 包含翻译后的API文档:avro-ipc-1.7.7-...
官方版本,亲测可用
赠送jar包:avro-ipc-1.7.7.jar; 赠送原API文档:avro-ipc-1.7.7-javadoc.jar; 赠送源代码:avro-ipc-1.7.7-sources.jar; 赠送Maven依赖信息文件:avro-ipc-1.7.7.pom; 包含翻译后的API文档:avro-ipc-1.7.7-...
官方版本,亲测可用
官方版本,亲测可用
Apache Avro是一个数据序列化系统。avro/avro-1.9.2/java
Apache Avro是一个数据序列化系统。avro/avro-1.10.1/java
Apache Avro是一个数据序列化系统。avro/avro-1.10.1/java
Apache Avro是一个数据序列化系统。avro/avro-1.9.2/java
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用
官方版本,亲测可用