FEBS est une bibliothèque commune en API couramment. La plupart des API sont comme JavaScript.
Maven Config.
< 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 ( ) ;Il peut initial avec la configuration du pool de threads. Le pool de thread affectera les performances de la promesse.
// Initial with thread pool config.
Febs . init ( new Febs . ThreadPoolCfg (
2 ,
4 ,
20000 ,
new LinkedBlockingQueue < > ( ) ,
new ThreadPoolExecutor . AbortPolicy ( ) )
) ; Utilisez l'API getExecutorService pour obtenir un élément de travail asynchrone.
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 comme JavaScript Promise API, utilisez la liste des chaînes Way to Faire un travail asynchrone.
.then : Identique à JS-ES6 Promesse .then.fail : Identique à JS-ES6 Promise .catch Chaîne de capt..finish : Identique à JS Promise .finally Chaîne finalement..execute : il doit être appelé pour activer la promesse dans 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 chaîne then et fail peut renvoyer un objet à la chaîne suivante. Le type de données de la valeur de retour est unkonw, nous pouvons utiliser le modèle pour spacify un type de données.
par exemple
// 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. Un objet prometteur attrapera l'exception utiliser cette méthode, si elle n'a pas appelé .fail()
Promise . setUncaughtExceptionHandler ( e - > {
// handle error.
} ) ; Le transfert de réseau dans le style de récupération
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 ( ) ;IMPORTANT: Fermer BufferedReader après avoir lu 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 ) ; Utilisez une API sleep pour planifier les tâches.
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 ( ) ;