Two, Curator use: asynchronous call

Code

    ExecutorService es = Executors.newFixedThreadPool(2);
    //带线程池的异步接口
    cc.create().inBackground((client,event)->{
    //pool-3-thread-1,CuratorEventImpl{type=CREATE, resultCode=0, path='/abc', name='/abc', children=null, context=null,stat=114,114,1573797468244,1573797468244,0,0,0,0,13,0,114, data=null, watchedEvent=null, aclList=null, opResults=null}  
        System.out.println(Thread.currentThread().getName()+","+event);

    },es).forPath("/abc");

    //不带线程池的异步接口
    cc.delete().inBackground((client,event)->{

    //main-EventThread,CuratorEventImpl{type=DELETE, resultCode=0, path='/abc', name='null', children=null, context=null, stat=null, data=null, watchedEvent=null, aclList=null, opResults=null}        
        System.out.println(Thread.currentThread().getName()+","+event);

    }).forPath("/abc");

    Thread.sleep(Integer.MAX_VALUE);

concept

1.inBackground () This method is to add an asynchronous callback method, parameters are BackgroundCallback interface is a function interface.
Client interface parameters for 2.BackgroundCallback (current client instance) and event (event server)
3. The event type, CuratorEventType,
contains the following information {type = DELETE, resultCode = 0 , path = '/ abc', name = ' null ', Children = null, context = null, STAT = null, Data = null, watchedEvent = null, aclList = null, opResults = null}
type corresponding to that type of operation, such as delete the corresponding delete (), Create corresponding to create ( ), etc., resultCode a response code 0 for success
4. role of the thread pool es, you can see by name, by default is to use the main-EventThread serial execution threads, if takes a long time have an impact, can to alleviate this situation by custom thread pool.

Guess you like

Origin www.cnblogs.com/june777/p/11865701.html