ZooKeeper-3.5 任务队列化- 高飞网

3.5 任务队列化

2018-05-30 17:00:49.0

通过持久化节点,创建任务,当客户端下线后,任务仍然存在;

通过顺序节点,创建有序的任务队列;


public class Client {
    ZKClient zkClient = ZKClient.instance();
    static Logger logger = LoggerFactory.getLogger(Client.class);
    final String TASK_PATH = "/tasks/task-";

    String queueCommand(String command) throws KeeperException, InterruptedException {
        while (true) {
            try {
                String name = zkClient.create(TASK_PATH, command.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
                logger.info("task create successfully." + name);
                return name;
            } catch (KeeperException.NodeExistsException e) {
                logger.error("command already exists:" + command);
            } catch (KeeperException.ConnectionLossException e) {

            }
        }
    }

    public static void main(String[] args) throws Exception {
        Client client = new Client();
        String name = client.queueCommand("aaa");
        logger.info("Created " + name);
    }
}


上一篇:3.4 注册从节点
下一篇:3.6 管理客户端