org.apache.avro.tool.RpcReceiveTool Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of avro-tools Show documentation
Show all versions of avro-tools Show documentation
Avro command line tools and utilities
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.avro.tool;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.io.File;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.net.URI;
import joptsimple.OptionParser;
import joptsimple.OptionSet;
import joptsimple.OptionSpec;
import org.apache.avro.AvroRemoteException;
import org.apache.avro.Protocol;
import org.apache.avro.Protocol.Message;
import org.apache.avro.generic.GenericDatumWriter;
import org.apache.avro.io.EncoderFactory;
import org.apache.avro.io.JsonEncoder;
import org.apache.avro.ipc.Ipc;
import org.apache.avro.ipc.Server;
import org.apache.avro.ipc.generic.GenericResponder;
/**
* Receives one RPC call and responds. (The moral equivalent of "netcat".)
*/
public class RpcReceiveTool implements Tool {
private PrintStream out;
private Object response;
/** Used to communicate between server thread (responder) and run() */
private CountDownLatch latch;
private Message expectedMessage;
Server server;
@Override
public String getName() {
return "rpcreceive";
}
@Override
public String getShortDescription() {
return "Opens an RPC Server and listens for one message.";
}
private class SinkResponder extends GenericResponder {
public SinkResponder(Protocol local) {
super(local);
}
@Override
public Object respond(Message message, Object request) throws AvroRemoteException {
if (!message.equals(expectedMessage)) {
out.println(
String.format("Expected message '%s' but received '%s'.", expectedMessage.getName(), message.getName()));
latch.countDown();
throw new IllegalArgumentException("Unexpected message.");
}
out.print(message.getName());
out.print("\t");
try {
JsonEncoder jsonEncoder = EncoderFactory.get().jsonEncoder(message.getRequest(), out);
GenericDatumWriter