FEBS es una bibliotecas comunes en API fluida. La mayoría de la API es como JavaScript.
Configuración maven.
< dependency >
< groupId > cn.brainpoint </ groupId >
< artifactId > febs </ artifactId >
< version > 0.1.3 </ version >
</ dependency > import cn . brainpoint . febs ;
//
// It will execute asynchronously after call `execute()`
//
PromiseFuture future = Febs . Net . fetch ( "https://xxx" )
// get response status code.
. then ( res - > {
// code.
System . out . print ( res . statusCode ) ;
// message.
System . out . print ( res . statusMsg ) ;
// get text content.
return res . text ( ) ;
} )
. then ( content - > {
} )
. execute ( ) ;
//
// Can use `future.get()` to get result in synchronize.
//
String result = ( String ) future . get ( ) ;Puede inicialmente con la configuración del grupo de subprocesos. Thread Pool afectará el rendimiento de la promesa.
// Initial with thread pool config.
Febs . init ( new Febs . ThreadPoolCfg (
2 ,
4 ,
20000 ,
new LinkedBlockingQueue < > ( ) ,
new ThreadPoolExecutor . AbortPolicy ( ) )
) ; Use la API getExecutorService para obtener un elemento de trabajo asincrónico.
try {
Future < Object > future
= Febs . getExecutorService . submit ( ( ) - > {
// do anything in this thread.
return "any" ;
} ) ;
Object result = future . get ( ) ;
} catch ( ExecutionException e ) {
e . printStackTrace ( ) ;
} catch ( InterruptedException e ) {
e . printStackTrace ( ) ;
} catch ( Exception e ) {
e . printStackTrace ( ) ;
} FEBS Promise como JavaScript Promise API, use la forma de la lista de la cadena para hacer un trabajo asíncrono.
.then : igual que JS-ES6 Promise .then de la cadena..fail : igual que JS-ES6 Promise .catch Chain..finish : igual que JS Promise .finally Finalmente cadena..execute : debe ser llamado para activar Promise en Febs Promise. /**
* Make a promise object.
*/
Promise promise = new Promise ( ( IResolve resolve , IReject reject ) - > {
// call this set status to 'fulfilled'
resolve . execute ( retVal ) ;
// call this set status to 'rejected'
reject . execute ( new Exception ( "" ) ) ;
} ) ;
/**
* chain.
*/
PromiseFuture = promise . then ( res - > { } )
. then ( ( ) - > { return 1 ; } )
. then ( res1 - > { } )
. fail ( e - > { } ) // same as javascript catch()
. finish ( ( ) - > { } ) // same as javascript finally()
. execute ( ) ; // activate promise.
/**
* Block until promise finish, if you want to wait.
*/
PromiseFuture . get ( ) ; promise . then ( res - > {
// this nest promise cannot call execute().
return new Promise ( ( resolve , reject ) - > {
...
} ) ;
} )
. then ( res - > {
} )
. execute ( ) ; /**
* Promise object array.
* !Warning: All promise object cannot call execute() funciton.
*/
Promise [ ] promiseArr = { ... } ;
/**
* execute all promise object.
*/
Promise . all ( promiseArr )
. then ( res - > {
// all promise done.
} )
. fail ( e - > {
// if some promise rejected.
} )
. execute ( ) ; La cadena then y fail puede devolver un objeto a la siguiente cadena. El tipo de datos del valor de retorno es UNKONW, podemos usar la plantilla para espaciar un tipo de datos.
p.ej
// Spacify a data type.
Promise < Integer > promise = new Promise < Integer > ( ( IResolve < Integer > resolve , IReject reject ) - > {
resolve . execute ( 2 ) ;
} ) ;
// use the data type.
promise . then ( ( Integer res ) - > {
// ...
} )
. execute ( ) ; // execute promise. Algún objeto de promesa atrapará la excepción Use este método, si no ha llamado .fail()
Promise . setUncaughtExceptionHandler ( e - > {
// handle error.
} ) ; La transferencia de red en estilo de búsqueda
import cn . brainpoint . febs ;
Febs . Net . fetch ( "https://xxxx" )
// get text content.
. then ( res - > { return res . text ( ) ; } )
// print content.
. then ( res - > {
System . out . print ( res ) ;
} )
// If exception cause.
. fail ( ( e ) - > {
System . err . print ( e . getMessage ( ) ) ;
} )
. execute ( ) ; import cn . brainpoint . febs ;
Febs . Net . fetch ( "https://xxxx" )
// get blob content.
. then ( res - > { return res . blob ( ) ; } )
// print content.
. then ( ( res ) - > {
BufferedReader in = ( BufferedReader ) res ;
char buf [ ] = new char [ 1024 ] ;
while ( in . read ( buf , 0 , buf . length ) != - 1 ) {
System . out . printf ( "%s" , Arrays . toString ( buf ) ) ;
Arrays . fill ( buf , ' ' ) ;
}
// important to call close().
in . close ( ) ;
} )
// If exception cause.
. fail ( ( e ) - > {
System . err . print ( e . getMessage ( ) ) ;
} )
. execute ( ) ;IMPORTANTE: Cerrar BufferedReader después de leer Blob.
import cn . brainpoint . febs ;
Febs . Net . fetch ( "https://xxxx" )
// get response status code.
. then ( res - > {
// code.
System . out . print ( res . statusCode ) ;
// message.
System . out . print ( res . statusMsg ) ;
return res ;
} )
// get response headers.
. then ( res - > {
Set < String > keySet = res.headers.keySet();
Iterator < String > it1 = keySet.iterator();
while(it1.hasNext()) {
String Key = it1 . next ( ) ;
System . out . print ( "header: " + Key ) ;
List < String > values = res . headers . get ( Key ) ;
System . out . print ( values ) ;
}
} )
// If exception cause.
. fail ( ( e ) - > {
System . err . print ( e . getMessage ( ) ) ;
} )
.execute(); import cn . brainpoint . febs ;
Febs . Net . fetch ( new Requset (
url ,
body ,
method ,
headers ,
timeout ,
) )
// get blob content.
. then ( res - > { return res . blob ( ) ; } )
. execute ( ) ; import cn . brainpoint . febs ;
/**
* set the trust manager.<br>
* The default trust manager is trust all site.
*
* @param trustManger the trust manager object.
*/
Febs . Net . setDefaultTrustManger ( X509TrustManager trustManager ) ; Use la API sleep para programar tareas.
import cn . brainpoint . febs ;
Febs . Utils . sleep ( 1000 )
. then ( ( ) - > {
System . out . print ( "after 1000ms." ) ;
} )
. execute ( ) ;
Febs . Utils . sleep ( 1000 )
. then ( res - > {
System . out . print ( "after 1000ms." ) ;
return Febs . Utils . sleep ( 2000 ) ;
} )
. then ( res - > {
System . out . print ( "after 2000ms." ) ;
} )
. execute ( ) ;