このブログでは、dockerコンテナ上で稼働させたwebMethods integration serverから、dockerコンテナ上のhttpbin(ローカルAPI)を呼び出すJava ServiceをService Designerを使って開発する手順を紹介します。
イメージとしては、下記のようになります。
sudo docker pull kennethreitz/httpbin sudo docker run -p 80:80 kennethreitz/httpbin
以下は、docker imageをpullした結果です。
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.ProtocolException; import java.net.URL; import java.net.URLEncoder;
public static final void callLocal_httpbin(IData pipeline) throws ServiceException { // pipelineから入力値を取得 IDataCursor pipelineCursor = pipeline.getCursor(); String arg1 = IDataUtil.getString(pipelineCursor, "arg1"); String arg2 = IDataUtil.getString(pipelineCursor, "arg2"); pipelineCursor.destroy(); try { // 入力値のバリデーション (必要に応じて) if (arg1 == null || arg1.isEmpty()) { throw new IllegalArgumentException("Input 'arg1' cannot be null or empty."); } if (arg2 == null || arg2.isEmpty()) { throw new IllegalArgumentException("Input 'arg2' cannot be null or empty."); } // URLの構築 // クエリパラメータをURLエンコード String encodedArg1 = URLEncoder.encode(arg1, "UTF-8"); String encodedArg2 = URLEncoder.encode(arg2, "UTF-8"); String urlString = String.format("http://host.docker.internal/get?arg1=%s&arg2=%s", encodedArg1,encodedArg2); // URLオブジェクトの生成と接続設定 URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Accept", "application/json"); // JSON形式での応答をリクエスト connection.setConnectTimeout(5000); // 接続タイムアウト (ミリ秒) connection.setReadTimeout(5000); // 読み取りタイムアウト (ミリ秒) int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream())); String inputLine; StringBuilder content = new StringBuilder(); while ((inputLine = in.readLine()) != null) { content.append(inputLine); } in.close(); // 応答データをpipelineに出力 // ここでは簡易的にJSON文字列をそのまま出力しています。 // 実際にはJSONパーサー (例: org.json, com.fasterxml.jackson.databind) を使用してパースし、 // 必要に応じてIData構造にマッピングすることが一般的です。 IDataCursor outputCursor = pipeline.getCursor(); IDataUtil.put(outputCursor, "httpbinResponse", content.toString()); outputCursor.destroy(); } else { // エラーハンドリング throw new Exception("HTTP GET request failed with response code: " + responseCode); } connection.disconnect(); }catch (Exception e) { IDataUtil.put(pipelineCursor, "error", e.toString()); } finally { pipelineCursor.destroy(); } }
{ "args": { "arg1": "foo", "arg2": "bar" }, "headers": { "Accept": "application/json", "Connection": "keep-alive", "Host": "host.docker.internal", "User-Agent": "Java/17.0.13" }, "origin": "172.17.0.1", "url": "http://host.docker.internal/get?arg1=foo&arg2=bar" }
IBM webMethods Service DesignerでJava Serviceを使って、ローカルのAPI呼び出しの連携を作成する手順は、以下で終了です。
Copy