zoukankan      html  css  js  c++  java
  • 深入浅出Zookeeper之四Create请求和处理

    客户端接口

    Java代码 复制代码 收藏代码
    1. public String create(final String path, byte data[], List<ACL> acl,
    2. CreateMode createMode)
    3. throws KeeperException, InterruptedException
    4. {
    5. final String clientPath = path;
    6. PathUtils.validatePath(clientPath, createMode.isSequential());
    7. final String serverPath = prependChroot(clientPath);
    8. //请求头
    9. RequestHeader h = new RequestHeader();
    10. h.setType(ZooDefs.OpCode.create);
    11. //请求体
    12. CreateRequest request = new CreateRequest();
    13. //CREATE请求需要server端响应
    14. CreateResponse response = new CreateResponse();
    15. request.setData(data);
    16. //node类型
    17. request.setFlags(createMode.toFlag());
    18. request.setPath(serverPath);
    19. if (acl != null && acl.size() == 0) {
    20. throw new KeeperException.InvalidACLException();
    21. }
    22. request.setAcl(acl);
    23. //同步提交
    24. ReplyHeader r = cnxn.submitRequest(h, request, response, null);
    25. //异常情况
    26. if (r.getErr() != 0) {
    27. throw KeeperException.create(KeeperException.Code.get(r.getErr()),
    28. clientPath);
    29. }
    30. //真实路径,对于SEQUENTIAL NODE后面会加上序号
    31. if (cnxn.chrootPath == null) {
    32. return response.getPath();
    33. } else {
    34. return response.getPath().substring(cnxn.chrootPath.length());
    35. }
    36. }

    请求提交过程和之前的exists一样,都是通过sendthread写出去,都会进入pengding队列等待server端返回。

    server端处理也是一样,变化的只是RequestProcessor的业务逻辑。也就是说transport层是通用的,变化的是上层的业务层。

    server端执行处理链,PrepRequestProcessor

    Java代码 复制代码 收藏代码
    1. switch (request.type) {
    2. case OpCode.create:
    3. //反序列化的对象
    4. CreateRequest createRequest = new CreateRequest();
    5. //zxid递增
    6. pRequest2Txn(request.type, zks.getNextZxid(), request, createRequest, true);
    7. break;
    8. ......
    9. request.zxid = zks.getZxid();
    10. nextProcessor.processRequest(request);  
  • 相关阅读:
    常用的android弹出对话框
    AutoCompleteTextView与TextWatcher的结合
    As of ADT 14, resource fields cannot be used as switch cases
    Linux中tshark(wireshark)抓包工具使用方法详解
    php模板引擎
    php中实现精确设置session过期时间的方法
    什么情况下会调用到session_destroy()
    PHPSession-完全PHP5之session篇
    彻底理解PHP的SESSION机制
    闪迪U3利用工具U3-Pwn
  • 原文地址:https://www.cnblogs.com/bjanzhuo/p/3575998.html
Copyright © 2011-2022 走看看