Skip to content

Commit

Permalink
Add Avro RPC
Browse files Browse the repository at this point in the history
  • Loading branch information
politrons committed Jul 30, 2018
1 parent b69d7fb commit eb2f419
Show file tree
Hide file tree
Showing 8 changed files with 631 additions and 2 deletions.
124 changes: 124 additions & 0 deletions .idea/uiDesigner.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions pom.xml
Expand Up @@ -29,6 +29,8 @@
<phase>generate-sources</phase>
<goals>
<goal>schema</goal>
<goal>protocol</goal>
<goal>idl-protocol</goal>
</goals>
<configuration>
<sourceDirectory>${project.basedir}/src/main/avro/</sourceDirectory>
Expand Down Expand Up @@ -277,6 +279,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.avro</groupId>
<artifactId>avro-ipc</artifactId>
<version>1.8.2</version>
</dependency>


</dependencies>

Expand Down
20 changes: 20 additions & 0 deletions src/main/avro/avro_rpc.avpr
@@ -0,0 +1,20 @@
{"namespace": "com.politrons.avro.rpc",
"protocol": "CustomAvroRPC",

"types": [
{"name": "Message", "type": "record",
"fields": [
{"name": "to", "type": "string"},
{"name": "from", "type": "string"},
{"name": "body", "type": "string"}
]
}
],

"messages": {
"send": {
"request": [{"name": "message", "type": "Message"}],
"response": "string"
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/com/politrons/avro/DeserializeAvro.java
Expand Up @@ -29,9 +29,9 @@ public static void fromFile() {
public static AvroPerson fromByteArray(byte[] avroPersonData) {
AvroPerson avroPerson = null;
try {
DatumReader<AvroPerson> employeeReader = new SpecificDatumReader<>(AvroPerson.class);
DatumReader<AvroPerson> personReader = new SpecificDatumReader<>(AvroPerson.class);
Decoder binaryDecoder = DecoderFactory.get().binaryDecoder(avroPersonData, null);
avroPerson = employeeReader.read(null, binaryDecoder);
avroPerson = personReader.read(null, binaryDecoder);
} catch (IOException e) {
e.printStackTrace();
}
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/politrons/avro/rpc/ClientAvroRPC.java
@@ -0,0 +1,38 @@
package com.politrons.avro.rpc;

import org.apache.avro.ipc.NettyTransceiver;
import org.apache.avro.ipc.specific.SpecificRequestor;

import java.io.IOException;
import java.net.InetSocketAddress;

import static com.politrons.avro.rpc.ServerAvroRPC.startServer;


/**
* Start a server, attach a client, and send a message.
*/
public class ClientAvroRPC {

public static void main(String[] args) throws IOException {
// usually this would be another app, but for simplicity
startServer();

NettyTransceiver client = new NettyTransceiver(new InetSocketAddress(65111));
// client code - attach to the server and send a message
CustomAvroRPC proxy = SpecificRequestor.getClient(CustomAvroRPC.class, client);
System.out.println("Client built, got proxy");

// fill in the Message record and send it
Message message = new Message();
message.setTo("Receiver");
message.setFrom("Sender");
message.setBody("Body message");
System.out.println("Calling proxy.send with message: " + message.toString());
System.out.println("Result: " + proxy.send(message));

// cleanup
client.close();
ServerAvroRPC.server.close();
}
}
24 changes: 24 additions & 0 deletions src/main/java/com/politrons/avro/rpc/CustomAvroRPC.java
@@ -0,0 +1,24 @@
/**
* Autogenerated by Avro
*
* DO NOT EDIT DIRECTLY
*/
package com.politrons.avro.rpc;

@SuppressWarnings("all")
@org.apache.avro.specific.AvroGenerated
public interface CustomAvroRPC {
public static final org.apache.avro.Protocol PROTOCOL = org.apache.avro.Protocol.parse("{\"protocol\":\"CustomAvroRPC\",\"namespace\":\"com.politrons.avro.rpc\",\"types\":[{\"type\":\"record\",\"name\":\"Message\",\"fields\":[{\"name\":\"to\",\"type\":\"string\"},{\"name\":\"from\",\"type\":\"string\"},{\"name\":\"body\",\"type\":\"string\"}]}],\"messages\":{\"send\":{\"request\":[{\"name\":\"message\",\"type\":\"Message\"}],\"response\":\"string\"}}}");
/**
*/
java.lang.CharSequence send(com.politrons.avro.rpc.Message message) throws org.apache.avro.AvroRemoteException;

@SuppressWarnings("all")
public interface Callback extends CustomAvroRPC {
public static final org.apache.avro.Protocol PROTOCOL = com.politrons.avro.rpc.CustomAvroRPC.PROTOCOL;
/**
* @throws java.io.IOException The async call could not be completed.
*/
void send(com.politrons.avro.rpc.Message message, org.apache.avro.ipc.Callback<java.lang.CharSequence> callback) throws java.io.IOException;
}
}

0 comments on commit eb2f419

Please sign in to comment.