AVRO IPC HelloWorld

avro-ipc的HelloWorld例子
[b]pom.xml[/b]

<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>

[b]helloworld.avpr[/b]

{
"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"]
}
}
}

[b]生成java protocol code[/b]

mvn clean compile

[b]HelloWorldImpl.java[/b]

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");
}

}

[b]IpcServer.java[/b]

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");
}
}

[b]Client.java[/b]

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();
}
}

[b]运行结果[/b]
Client built, got proxy
Calling proxy.hello with message: {"message": "how are you"}
Result: {"message": "not too bad"}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值