前言
上一篇博客将flowable rest api通过spring boot 发布成为了工作流引擎的微服务。但是使用rest-api 就还得自己完成rest-api-client。
当然用spring boot 提供的 restTemplate,或者 feign Client 完成 rest-api调用。
如果不使用Spring boot 或者 Spring 框架呢?
那就需要一个轻量级的rest-client。
所以就使用Retofit2 创建了 flowable-rest-java-client 项目。
目录
- 前言
- 不想看说明,只想看代码的
- maven管理
- Client 使用
- 调用方法
- 链式调用
- Retofit2 配置
不想看说明,只想看代码的
代码已经发布到了github,需要的亲们直接去clone使用就好了。
maven管理
代码使用maven 管理,可以打包成jar包,或者直接把项目发布到私服,然后引用私服的依赖就可以了。
Client 使用
调用方法
// 获取Client实例
FlowableClient client = FlowableClient.getInstance();
// 调用connect方法配置连接地址、用户名、密码
client.connect(Constants.EndPoint, "username", "password")
// 通过FlowableClient实例获取对应的API对象
EngineAPI engineAPI = client.getEngineAPI();
// 使用具体的API方法获取数据
EngineInfo engineInfo = engineAPI.getEngineInfo();
调用rest-api时,必须先调用一次connect方法,不然不抛出异常。
链式调用
EngineAPI engineAPI = FlowableClient.getInstance().connect(Constants.EndPoint, Constants.USERNAME, Constants.PASSWORD).getEngineAPI();
Retofit2 配置
public FlowableClient connect(String endpoint, String username, String password) {
if (endpoint == null || endpoint.isEmpty()) {
throw new IllegalArgumentException("Invalid url");
}
if (httpClient.get() == null) {
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.addInterceptor(new AuthorizationInterceptor(username, password));
builder.readTimeout(60, TimeUnit.SECONDS);
builder.writeTimeout(60,TimeUnit.SECONDS);
httpClient.compareAndSet(null, builder.build());
}
if (retrofit.get() == null) {
retrofit.compareAndSet(null, new Retrofit.Builder()
.baseUrl(endpoint)
.addCallAdapterFactory(new SyncCallAdapterFactory())
.addConverterFactory(ConverterFactory.create())
.client(httpClient.get())
.build());
}
isConnect.compareAndSet(false, true);
return this;
}
需要修改okhttp client 或者 retorfit2 的配置,在FlowableClient.java类中的connect方法中修改就可以了。