Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/brainpoint/febs-java

The common libraries in fluent api. Most api is like javascript.
https://github.com/brainpoint/febs-java

febs fetch java-js net promise promise-java stage thread threadpool

Last synced: 8 days ago
JSON representation

The common libraries in fluent api. Most api is like javascript.

Awesome Lists containing this project

README

        

# Febs

[![Maven Central](https://maven-badges.herokuapp.com/maven-central/cn.brainpoint/febs/badge.svg)](https://maven-badges.herokuapp.com/maven-central/cn.brainpoint/febs/)
[![License](https://img.shields.io/github/license/brainpoint/febs-java)](https://opensource.org/licenses/MIT)

Febs is a common libraries in fluent API. Most api is like javascript.

- [How to use](#how-to-use)
- [Asynchronous in ThreadPool](#Asynchronous-in-ThreadPool)
- [Asynchronous in Promise](#Asynchronous-in-Promise)
- [Network transfer in Fetch](#Network-transfer-in-Fetch)
- [Utilities](#Utilities)

## How to use

maven config.

```html

cn.brainpoint
febs
0.1.3

```

```js
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();

```

### config

It can initial with thread pool config. Thread pool will affect performance of promise.

```js
// Initial with thread pool config.
Febs.init(new Febs.ThreadPoolCfg(
2,
4,
20000,
new LinkedBlockingQueue<>(),
new ThreadPoolExecutor.AbortPolicy())
);
```

## Asynchronous in ThreadPool

Use `getExecutorService` api to get a asynchronous work item.

```js
try {
Future 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();
}
```

## Asynchronous in Promise

Febs promise like javascript promise api, use chain list way to do asynchronous work.

- `.then`: same as js-es6 promise`.then` chain.
- `.fail`: same as js-es6 promise`.catch` chain.
- `.finish`: same as js promise`.finally` chain.
- `.execute`: It must be call to activate promise in Febs promise.

### Base scene

```js
/**
* 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();
```

### return another promise object in chain.

```js
promise.then(res->{
// this nest promise cannot call execute().
return new Promise((resolve, reject)->{
...
});
})
.then(res->{
})
.execute();
```

### all

```js
/**
* 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();
```

### template

The `then` and `fail` chain can return a object to next chain. The data type of return value is unkonw, we can use template to spacify a data type.

e.g.

```js
// Spacify a data type.
Promise promise = new Promise((IResolve resolve, IReject reject)-> {
resolve.execute(2);
});

// use the data type.
promise.then((Integer res)->{
// ...
})
.execute(); // execute promise.
```

### Uncaught Exception Handler

Some promise object will catch exception use this method, if it have't call `.fail()`

```js
Promise.setUncaughtExceptionHandler(e->{
// handle error.
});
```

## Network transfer in Fetch

The network transfer in fetch style

### Get text content.

```js
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();
```

### Get binary content.

```js
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, '\0');
}

// important to call close().
in.close();
})
// If exception cause.
.fail((e)->{
System.err.print(e.getMessage());
})
.execute();
```

> IMPORTANT: close BufferedReader after read blob.

### Get response headers.

```js
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 keySet = res.headers.keySet();
Iterator it1 = keySet.iterator();
while(it1.hasNext()){
String Key = it1.next();
System.out.print("header: " + Key);
List values = res.headers.get(Key);
System.out.print(values);
}
})
// If exception cause.
.fail((e)->{
System.err.print(e.getMessage());
})
.execute();
```

### Set request parameter

```js
import cn.brainpoint.febs;

Febs.Net.fetch(new Requset(
url,
body,
method,
headers,
timeout,
))
// get blob content.
.then(res->{ return res.blob(); })
.execute();
```

### SSL trust manager

```js
import cn.brainpoint.febs;

/**
* set the trust manager.

* The default trust manager is trust all site.
*
* @param trustManger the trust manager object.
*/
Febs.Net.setDefaultTrustManger(X509TrustManager trustManager);
```

## Utilities

### sleep

Use `sleep` API to schedule tasks.

```js
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();
```