Create: 2013/04/04
LastUpdate: 2013/04/10
下図の環境を使用します。
環境の詳細を知りたい場合は、メニューに戻って構築手順を参照してください。
JobScheduler では、同期応答と非同期応答の2種類のWebServiceをSOAPで提供しています。
WebServiceを利用すると、ジョブの実行などの操作を自分のアプリケーションから行うことができます。
WebServiceはSOAPを使用しますが、WSDLは用意されていません。
SOAPメッセージをXSLTで変換してJobSchedulerと通信しないといけないので、SOAPとXSLTの知識がないと使うのは難しいです。
SOAPやXSLTを知らない場合は、XMLコマンドを使うほうが簡単です。
WebServiceの使用手順については、「Tutorial Web Service Implementation(PDF)」を参考にしました。
1.WebService(非同期応答)
1.1.WebServiceの設定
新しいWebServiceを定義します。
Managerサーバに jobs ユーザでログインして、以下の設定ファイルを変更します。
- /home/jobs/sos-berlin.com/jobscheduler/scheduler/config/scheduler.xml
<http_server> <http_directory url_path = "/scheduler_home/" path = "${SCHEDULER_HOME}/"/> <http_directory url_path = "/scheduler_data/" path = "${SCHEDULER_DATA}/"/> <web_service debug = "no" request_xslt_stylesheet = "config/scheduler_soap_request.xslt" response_xslt_stylesheet = "config/scheduler_soap_response.xslt" name = "scheduler" url_path = "/scheduler" /> </http_server>JobScheduler を再起動します。
$ sudo service jobscdl stop $ sudo service jobscdl startこれで、以下のURLにSOAPメッセージを送信することができます。192.168.1.61 はManagerサーバのIPアドレスです。
- http://192.168.1.61/scheduler
- addOrder ・・・ Orderの登録
- startJob ・・・ ジョブの実行
- modifyJob ・・・ ジョブの登録
- showOrder ・・・ Orderの参照
1.2.コマンドラインから利用
curl コマンドでSOAPメッセージを送信して、ジョブを実行してみます。
送信するXML形式のSOAPメッセージは以下のとおり。これを "aaa.txt"ファイルに保存します。
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <startJob xmlns="http://www.sos-berlin.com/scheduler"> <job>/test/shell_job</job> <at>now</at> </startJob> </soapenv:Body> </soapenv:Envelope>このSOAPメッセージは、ジョブの "/test/shell_job" を計画実行(即時)します。
curl コマンドを以下のように実行して、"aaa.txt"ファイルのSOAPメッセージを送信します。
$ curl -X POST -H 'Content-Type: application/soap+xml;charset=UTF-8' -d @aaa.txt http://192.168.1.61:4444/scheduler非同期のWebServiceなのでジョブの実行終了を待たずに、ジョブの実行登録だけ受け付けて、以下のように TaskID を応答します。
<?xml version="1.0" encoding="ISO-8859-1"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"><soapenv:Body><sos:taskId xmlns:sos="http://www.sos-berlin.com/scheduler">6799</sos:taskId></soapenv:Body></soapenv:Envelope>あとは、指定した時間になればジョブが実行されます。
今回の例では "now" で即時実行を設定したので、ジョブはすぐに実行されます。
JOCを開けば、ジョブの実行結果を見ることができます。
1.3.phpから利用
phpのプログラムで SOAPメッセージを送信する場合も、CURL を使用すると簡単です。
上記と同じSOAPメッセージを送信するプログラムは以下のとおり。
<?php // 送信データ $post=<<<EOF <?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"> <soapenv:Body> <startJob xmlns="http://www.sos-berlin.com/scheduler"> <job>/test/shell_job</job> <at>now</at> </startJob> </soapenv:Body> </soapenv:Envelope> EOF; // SOAPメッセージ送信(POST) $ch=curl_init(); curl_setopt ($ch,CURLOPT_URL,"http://192.168.1.61:4444/scheduler"); curl_setopt ($ch,CURLOPT_POST,true); curl_setopt ($ch,CURLOPT_POSTFIELDS,$post); curl_setopt ($ch,CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/soap+xml;charset=UTF-8')); $res = curl_exec($ch); curl_close ($ch); // 応答データ echo $res; ?>
2.WebService(同期応答)
2.1.WebServiceの設定
JobChainの実行結果を応答するWebService(同期応答)を作ることができます。
以下のように赤字部分の設定を追加します。
<http_server> <http_directory url_path = "/scheduler_home/" path = "${SCHEDULER_HOME}/"/> <http_directory url_path = "/scheduler_data/" path = "${SCHEDULER_DATA}/"/> <web_service debug = "no" request_xslt_stylesheet = "config/scheduler_soap_request.xslt" response_xslt_stylesheet = "config/scheduler_soap_response.xslt" name = "scheduler" url_path = "/scheduler" /> <web_service name="test1" job_chain="/test/ws/ws_test1" url_path="/test1" /> </http_server>これで、JobScheduler を再起動すると、設定が有効になります。
以下のURLにアクセスすると、"/test/ws/ws_test1" が実行されて実行結果が表示されます。
ちなみに、URLにクエリーは使用できません。クエリーはPOSTで送信します。
- http://192.168.1.61:4444/test1
2.2.JobChainとJobの定義
試しに、JobChain と Job を用意してURLにアクセスしてみます。
Managerサーバにjobsユーザでログインし、以下のようにディレクトリとファイルを作成します。
$ ls /home/jobs/sos-berlin.com/jobscheduler/scheduler/config/live/test/ws ws_test1.job_chain.xml ws_test_job.job.xmlJobChain を以下のように定義します。
ジョブを1つだけ実行します。
<job_chain > <job_chain_node state="1" job="ws_test_job" next_state="100" error_state="999" /> <job_chain_node state="100"/> <job_chain_node state="999"/> </job_chain>ジョブを以下のように定義します。
リクエストデータを実行結果ログに出力します。
また、赤字部分でレスポンス(応答)を編集しています。
<job order="yes">
<script language="javascript">
<![CDATA[
function spooler_process()
{
var order = spooler_task.order;
var operation;
try{
operation = order.web_service_operation;
} catch (e){
spooler_log.info("There is no Web Service operation attached to this order");
return true;
}
// read request
var request = operation.request;
spooler_log.info("Content of Webservice Request:\n"+request.string_content);
// send reply
var response = operation.response;
response.string_content = "Task id: " + spooler_task.id;
response.send();
return true;
}
]]>
</script>
<run_time />
</job>
2.3.コマンドラインから利用
curl コマンドでWebService にアクセスしてみると、結果は以下のとおり。
ジョブがレスポンスに編集したTaskID が表示されます。
また、JOCを開けば、Jobの実行結果でPOSTリクエストの内容を見ることができます。
$ curl -X "POST" -d "p=123" http://192.168.1.61:4444/test1 Task id: 6823
ちなみに、以下のように charset に "UTF-8" を指定するとエラーになり、応答が無いのでCtrl+Cで終了するしかありません。
$ curl -X "POST" -H "Content-Type: text/html; charset=utf-8" -d "p=123" http://192.168.1.61:4444/test1
エラーメッセージは、以下のとおり。2013-04-08 10:34:31.843 [ERROR] (Task test/ws/ws_test_job:8073) COM-80020009 DISP_E_EXCEPTION [step] [] [SOS-1425 Fehler im Skript: COM-80020009 DISP_E_EXCEPTION [IDispatch::Invoke] [string_content] [Z-CHARSET-001 Unknown character encoding: utf-8, in sos::scheduler::http::C_request::get_String_content] in line 17, column 1, in Remote_module_instance_server::step], method=callrequest.content_string をログに出力しているところで異常終了しています。
Windowsは、"UTF-8" をサポートしていますが、Linuxは "ISO-8859-1" しかサポートしていないので原因です。
charset を指定しなければ "ISO-8859-1" になります。
WebService(同期応答)のPOSTリクエストにUTF-8を使用したい場合は、WindowsサーバでJobSchedulerを使うしかないようです。