IBM TechXchange Integration Group Japan

IBM TechXchange Integration Group Japan

 View Only

IBM webMethods Service DesignerでJava Serviceを使って、ローカルのAPI呼び出しの連携を作成する

By HIDEO SAITOH posted Fri July 25, 2025 06:13 AM

  

IBM webMethods Service DesignerでJava Serviceを使って、ローカルのAPI呼び出しの連携を作成する

このブログでは、dockerコンテナ上で稼働させたwebMethods integration serverから、dockerコンテナ上のhttpbin(ローカルAPI)を呼び出すJava ServiceをService Designerを使って開発する手順を紹介します。

イメージとしては、下記のようになります。

  • IBM webMethods Integration Serverをコンテナで稼働させる手順は、こちらにあります。
  • IBM webMethods Service Designerの導入とIBM webMethods Integration Serverの接続手順は、こちらにあります。

ローカルのAPI(モック)である、httpbinの導入手順

  • dockerコンテナに以下のコマンドを使って導入し、起動させます。
    sudo docker pull kennethreitz/httpbin           
    sudo docker run -p 80:80 kennethreitz/httpbin

    以下は、docker imageをpullした結果です。


    以下は、httpbinを開始した結果になります。
  • httpbinをためしてみる。
    ブラウザーから、http://localhost/get?arg1=foo&arg2=barを入力してみる。
    以下のように、引数とかHeader情報が応答として帰ってきます。
  • httpbinの導入は以上です。

webMethods Service DesignerでJava Serviceを作成する

  • 最初にパッケージを作成します。
    Defaultを右クリック > New > Package
  • パッケージ名を入力します。
    パッケージ
    myJavaService

  • フォルダーを作成します。
    myJavaServiceを右クリック > New > Folder
  • フォルダー名をmyFolderと入力します。
    フォルダー名
    myFolder



  • Java Serviceを作成
    myFolderを右クリック > New > Java Service
  • Element nameには、callLocal_httpbinを入力します。
    Element name
    callLocal_httpbin

  • Javaコードのテンプレートが作成されます。
    このテンプレートを修正して、コードを完成させます。
  • InputとOutputの定義を行います。
    Input/Outputタブをクリックします。
  • 左側のエリアがInputになるので、ここにarg1とarg2を定義します。
    右クリック > Insert > String
  • 以下の二つを定義します。
    Name Type
    arg1 String
    arg2 String
  • 続いて、同様の手順で、Outputを定義します。
    Name Type
    httpbinResponse String
  • Javaコードを完成させる。
    import文を追加します。
    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;
    import com.wm.util.JournalLogger;

    callLocal_httpbin関数の中身を完成させます。
    以下は完成したコードです。
    	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());
    		    JournalLogger.log(4,90,3,"[callLocal_httpbin]", 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();
    		}
    	}
    

  • コードの追加が完了したら、CTRL+Sで保存します。

動作確認

  • Service Designerの中から、Runを実行して動作を確認します。
    callLocal_httpbinを選んだ状態で、実行ボタンをクリックします。
  • 二つの引数のInput(arg1とarg2)に任意の値を入力します。
  • 以下のようにレスポンスが返ってくれば、ローカルのhttpbinの呼び出しが成功したことになります。
  • 以下がレスポンスとして帰ってきていれば成功です。
    引数に指定したものと、URLとして、host.docker.internalが指定されて結果が返ってきています。
    {
    	"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呼び出しの連携を作成する手順は、以下で終了です。

0 comments
17 views

Permalink