点石互动

导航

 

客户端接口

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);  
posted on 2013-02-16 19:46  点石互动  阅读(463)  评论(0编辑  收藏  举报