使用retorfit2創建flowable-rest-api的flowable-rest-client

前言

上一篇博客將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 項目。

不想看說明,只想看代碼的

https://github.com/ChineseLincoln/flowable-rest-java-client

代碼已經發布到了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方法中修改就可以了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章