本文整理汇总了Java中org.apache.avro.Protocol类的典型用法代码示例。如果您正苦于以下问题:Java Protocol类的具体用法?Java Protocol怎么用?Java Protocol使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
Protocol类属于org.apache.avro包,在下文中一共展示了Protocol类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: bussinessDeal
import org.apache.avro.Protocol; //导入依赖的package包/类
/**
* 进行必要的业务处理
*
* @param transceiver
* @throws IOException
*/
private void bussinessDeal(Transceiver transceiver) throws IOException {
// 2.获取协议
Protocol protocol = Protocol.parse(this.getClass().getResourceAsStream("/Members.avpr"));
// 3.根据协议和通讯构造请求对象
GenericRequestor requestor = new GenericRequestor(protocol, transceiver);
// 4.根据schema获取messages主节点内容
GenericRecord loginGr = new GenericData.Record(protocol.getMessages().get("login").getRequest());
// 5.在根据协议里面获取request中的schema
GenericRecord mGr = new GenericData.Record(protocol.getType("Members"));
// 6.设置request中的请求数据
mGr.put("userName", "rita");
mGr.put("userPwd", "123456");
// 7、把二级内容加入到一级message的主节点中
loginGr.put("m", mGr);
// 8.设置完毕后,请求方法,正式发送访问请求信息,并得到响应内容
Object retObj = requestor.request("login", loginGr);
// 9.进行解析操作
GenericRecord upGr = (GenericRecord) retObj;
System.out.println(upGr.get("msg"));
}
示例2: extractParams
import org.apache.avro.Protocol; //导入依赖的package包/类
/**
* Extracts parameters from RPC call to List or converts to object of appropriate type
* if only one parameter set.
*
* @param message Avro message
* @param request Avro request
* @param singleParameter Indicates that called method has single parameter
* @param dataResolver Extracts type of parameters in call
* @return Parameters of RPC method invocation
*/
private static Object extractParams(Protocol.Message message, Object request, boolean singleParameter, SpecificData dataResolver) {
if (singleParameter) {
Schema.Field field = message.getRequest().getFields().get(0);
return dataResolver.getField(request, field.name(), field.pos());
} else {
int i = 0;
Object[] params = new Object[message.getRequest().getFields().size()];
for (Schema.Field param : message.getRequest().getFields()) {
params[i] = dataResolver.getField(request, param.name(), param.pos());
i++;
}
return params;
}
}
示例3: compileInterface
import org.apache.avro.Protocol; //导入依赖的package包/类
private void compileInterface(Protocol protocol) throws IOException {
startFile(protocol.getName(), protocol.getNamespace());
try {
line(0, "public interface "+protocol.getName()+" {");
out.append("\n");
for (Map.Entry<String,Message> e : protocol.getMessages().entrySet()) {
String name = e.getKey();
Message message = e.getValue();
Schema request = message.getRequest();
Schema response = message.getResponse();
line(1, unbox(response)+" "+name+"("+params(request)+")");
line(2,"throws AvroRemoteException"+errors(message.getErrors())+";");
}
line(0, "}");
} finally {
out.close();
}
}
示例4: startServers
import org.apache.avro.Protocol; //导入依赖的package包/类
public void startServers() {
servers = new ArrayList<>(protocolPaths.size());
for (Entry<Class<?>, String> entry : protocolPaths.entrySet()) {
Protocol protocol = AvroHelper.getProtocol(entry.getKey());
String path = entry.getValue();
if (!path.startsWith("/")) {
path = "/" + path;
}
URL url;
try {
url = new URL("http", serverHost, serverPort, path);
} catch (Exception e) {
throw new RuntimeException("Unable to initialize server", e);
}
AvroD2Server server = new AvroD2Server(protocol, url);
server.register();
servers.add(server);
}
}
示例5: respond
import org.apache.avro.Protocol; //导入依赖的package包/类
public Object respond(Protocol.Message message, Object request, SpecificData data) throws Exception {
AvroConsumer consumer = this.defaultConsumer;
if (this.consumerRegistry.containsKey(message.getName())) {
consumer = this.consumerRegistry.get(message.getName());
}
if (consumer == null) {
throw new AvroComponentException("No consumer defined for message: " + message.getName());
}
Object params = extractParams(message, request, consumer.getEndpoint().getConfiguration().isSingleParameter(), data);
return processExchange(consumer, message, params);
}
示例6: processExchange
import org.apache.avro.Protocol; //导入依赖的package包/类
/**
* Creates exchange and processes it.
*
* @param consumer Holds processor and exception handler
* @param message Message on which exchange is created
* @param params Params of exchange
* @return Response of exchange processing
* @throws Exception
*/
private static Object processExchange(AvroConsumer consumer, Protocol.Message message, Object params) throws Exception {
Object response;
Exchange exchange = consumer.getEndpoint().createExchange(message, params);
try {
consumer.getProcessor().process(exchange);
} catch (Throwable e) {
consumer.getExceptionHandler().handleException(e);
}
if (ExchangeHelper.isOutCapable(exchange)) {
response = exchange.getOut().getBody();
} else {
response = null;
}
boolean failed = exchange.isFailed();
if (failed) {
if (exchange.getException() != null) {
throw exchange.getException();
} else {
// failed and no exception, must be a fault
throw new AvroComponentException("Camel processing error.");
}
}
return response;
}
示例7: createExchange
import org.apache.avro.Protocol; //导入依赖的package包/类
public Exchange createExchange(Protocol.Message message, Object request) {
ExchangePattern pattern = ExchangePattern.InOut;
if (message.getResponse().equals(Schema.Type.NULL)) {
pattern = ExchangePattern.InOnly;
}
Exchange exchange = createExchange(pattern);
exchange.getIn().setBody(request);
exchange.getIn().setHeader(AvroConstants.AVRO_MESSAGE_NAME, message.getName());
return exchange;
}
示例8: compileProtocol
import org.apache.avro.Protocol; //导入依赖的package包/类
/** Generates Java interface and classes for a protocol.
* @param src the source Avro protocol file
* @param dest the directory to place generated files in
*/
public static void compileProtocol(File src, File dest) throws IOException {
log.info("Compiling Protocol: " + src + " to: " + dest);
if(licenseHeader != null) {
log.info("The generated file will be " + licenseHeader.getLicenseName() + " licensed.");
}
GoraCompiler compiler = new GoraCompiler(dest);
Protocol protocol = Protocol.parse(src);
for (Schema s : protocol.getTypes()) // enqueue types
compiler.enqueue(s);
compiler.compileInterface(protocol); // generate interface
compiler.compile(); // generate classes for types
}
示例9: createVersionNode
import org.apache.avro.Protocol; //导入依赖的package包/类
public static void createVersionNode(ZooKeeper zk, Protocol protocol)
throws KeeperException, InterruptedException {
String path = getVersionsZkPath(protocol);
String md5 = DatatypeConverter.printHexBinary(protocol.getMD5());
ensurePath(zk, path);
try {
zk.create(path + "/" + md5, protocol.toString().getBytes(Constants.UTF8), Ids.READ_ACL_UNSAFE,
CreateMode.PERSISTENT);
} catch (NodeExistsException e) {
// Ignore.
}
}
示例10: getUri
import org.apache.avro.Protocol; //导入依赖的package包/类
public static URI getUri(Protocol protocol) {
try {
return new URI(SCHEME + ":/" + getVersionsZkPath(protocol));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
示例11: readProtocolFromZk
import org.apache.avro.Protocol; //导入依赖的package包/类
public static Protocol readProtocolFromZk(ZooKeeper zk, String namespace, String name,
String md5) throws KeeperException, InterruptedException {
String versionPath = getProtocolZkPath(namespace, name) + "/versions/" + md5;
byte[] data = zk.getData(versionPath, false, null);
String schema = new String(data, Constants.UTF8);
return Protocol.parse(schema);
}
示例12: processProtoFile
import org.apache.avro.Protocol; //导入依赖的package包/类
private void processProtoFile(File sourceFile) {
getLogger().info("Processing {}", sourceFile);
try {
compile(Protocol.parse(sourceFile), sourceFile);
} catch (IOException ex) {
throw new GradleException(String.format("Failed to compile protocol definition file %s", sourceFile), ex);
}
}
示例13: bussinessDeal
import org.apache.avro.Protocol; //导入依赖的package包/类
/**
* 主要进行业务处理 服务端逻辑处理 采用动态生成代码处理方式,客户端和服务端只需要有protocol文件即可,不需要手工生成代码
*
* @return
* @throws IOException
*/
private GenericResponder bussinessDeal() throws IOException {
// 1.构建协议
final Protocol protocol = Protocol.parse(this.getClass().getResourceAsStream("/Members.avpr"));
// 2.构建业务逻辑及响应客户端
GenericResponder gr = new GenericResponder(protocol) {
@Override
public Object respond(Message message, Object request) throws Exception {
System.err.println("request:" + request);
// 3.获取请求信息
GenericRecord record = (GenericRecord) request;
GenericRecord retGr = null;
// 4.判断请求的方法
if (message.getName().equals("login")) {
// 5.获取到传输的参数
Object obj = record.get("m");
GenericRecord mGr = (GenericRecord) obj;
String userName = mGr.get("userName").toString();
String userPwd = mGr.get("userPwd").toString();
// 6.进行相应的业务逻辑处理
System.out.println("Members:" + ",userName:" + userName + mGr + ",userPwd:" + userPwd);
String retMsg;
if (userName.equalsIgnoreCase("rita") && userPwd.equals("123456")) {
retMsg = "哈哈,恭喜你,成功登录。";
System.out.println(retMsg);
} else {
retMsg = "登录失败。";
System.out.println(retMsg);
}
// 7.获取返回值类型
retGr = new GenericData.Record(protocol.getMessages().get("login").getResponse());
// 8.构造回复消息
retGr.put("msg", retMsg);
}
System.err.println("DEAL SUCCESS!");
return retGr;
}
};
return gr;
}
示例14: AvroReflectRequestor
import org.apache.avro.Protocol; //导入依赖的package包/类
public AvroReflectRequestor(Protocol protocol, Transceiver transceiver) throws IOException {
super(protocol, transceiver);
}
示例15: applyToConfiguration
import org.apache.avro.Protocol; //导入依赖的package包/类
/**
* Applies endpoint parameters to configuration & resolves protocol and other required configuration properties.
*/
private void applyToConfiguration(AvroConfiguration config, URI endpointUri, Map<String, Object> parameters) throws Exception {
config.parseURI(endpointUri, parameters, this);
setProperties(config, parameters);
if (config.getProtocol() == null && config.getProtocolClassName() != null) {
Class<?> protocolClass = getCamelContext().getClassResolver().resolveClass(config.getProtocolClassName());
if (protocolClass != null) {
try {
Field f = protocolClass.getField("PROTOCOL");
if (f != null) {
Protocol protocol = (Protocol)f.get(null);
config.setProtocol(protocol);
}
} catch (NoSuchFieldException e) {
ReflectData reflectData = ReflectData.get();
config.setProtocol(reflectData.getProtocol(protocolClass));
config.setReflectionProtocol(true);
}
}
}
if (config.getProtocol() == null) {
throw new IllegalArgumentException("Avro configuration does not contain protocol");
}
if (config.getMessageName() != null && !config.getProtocol().getMessages().containsKey(config.getMessageName())) {
throw new IllegalArgumentException("Message " + config.getMessageName() + " is not defined in protocol");
}
if (config.isSingleParameter()) {
Map<String, Protocol.Message> messageMap = config.getProtocol().getMessages();
Iterable<Protocol.Message> messagesToCheck = config.getMessageName() == null
? messageMap.values()
: Collections.singleton(messageMap.get(config.getMessageName()));
for (Protocol.Message message : messagesToCheck) {
if (message.getRequest().getFields().size() != 1) {
throw new IllegalArgumentException("Single parameter option can't be used with message "
+ message.getName() + " because it has " + message.getRequest().getFields().size()
+ " parameters defined"
);
}
}
}
}