Sunday, January 22, 2012

SOA11G: Scheduling SOA BPEL Process using DBMS_scheduler

Scheduling a BPEL Process is always a requirement which many projects have. SOA11G still doesn't provide Out-of-Box, ready to use functionality for that. So we are left we a choice to use an external scheduler and from that call the SOA Process. One approach is using Quartz API, which has included lot many new features now. Another approach is to use Database based scheduler.

I personally carry this thought that - One product which Oracle has released to IT world, which is efficient, easily manageable and yet reliable is - Oracle DB. So i'll prefer Database based JOBs to try scheduling a SOA BPEL.

Here in 3 simple steps, we will create a DB JOB to schedule a BPEL which takes a STRING as input. 
The input is qualified with namespace - 'http://xmlns.oracle.com/Application1/Project1/BPELProcess1', in this example.

1) Create a PLSQL procedure which will inturn call a BPEL process using SOAP request.
CREATE OR REPLACE PROCEDURE ScheduleBPELProcess
(p_Payload       IN VARCHAR2)

IS
soap_request VARCHAR2(30000);
soap_respond VARCHAR2(30000);
http_req     UTL_HTTP.REQ;
http_resp    UTL_HTTP.RESP;
resp         XMLTYPE;
response     VARCHAR2(30000) := '';
endpoint  VARCHAR2(128) :=   'http://usmtnz-dinfap19.dev.emrsn.org:8120/soa-infra/services/ETH_Temp/Project1/bpelprocess1_client_ep';
-- Service Endpoint URL of the BPEL process

BEGIN
   
--Create a SOAP request to invoke the BPEL process
   
   soap_request:= ' <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'
      ||'<soap:Body xmlns:ns1="http://xmlns.oracle.com/Application1/Project1/BPELProcess1">'
            ||'   <ns1:process><ns1:input>inderpal singh</ns1:input></ns1:process>'
    ||'</soap:Body>'
     ||'</soap:Envelope>';   
    http_req:= utl_http.begin_request
    (
     endpoint,
      'POST',
      'HTTP/1.1'
    );
    utl_http.set_header
    (
      http_req  ,
     'Content-Type'  ,
     'text/xml'
    ); -- dealing with plain text in XML documents
   
    utl_http.set_header(http_req  , 'Content-Length'  , lengthb(soap_request));
    utl_http.set_header(http_req  , 'SOAPAction'  , 'process'); -- required to specify a SOAP communication
    utl_http.write_text(http_req, soap_request);--
    http_resp := utl_http.get_response(http_req);
    utl_http.read_text(http_resp, soap_respond);
    utl_http.end_response(http_resp);--
    resp := XMLType.createXML(soap_respond);
   
    IF (instr(resp.getStringVal(), 'ERROR:') > 0)
    THEN 
    raise_application_error (     -20999,     'ScheduleBPELProcess: Failed! '||p_Payload);
    END IF;
   
    resp := resp.extract(    '/soap:Envelope/soap:Body/child::node()'   , 'xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"'   );

   END;

2) Next Step is to create a job in Oracle DB using DBMS_SCHEDULER package.
BEGIN
  -- Job defined entirely by the CREATE JOB procedure.
  DBMS_SCHEDULER.create_job (
    job_name        => 'schedule_bpel_process',--Name the job
    job_type        => 'PLSQL_BLOCK',
    job_action      => 'BEGIN ScheduleBPELProcess(''ABC''); END;',--Enclose ---the procedure name here which will be calling the webservice
    start_date      => SYSTIMESTAMP,
    repeat_interval => 'freq=minutely; interval=5',--The repeat interval
 --attribute value can be set according to the scheduled time.These are some --predefined values which can be set.
    end_date        => NULL,
    enabled         => TRUE,
    comments        => 'Job defined entirely by the CREATE JOB procedure.');
 END;

3)We can use DBMS_SCHEDULER.setAttribute procedure to set the values of the attributes of the job created in step 2.
For ex: Here we can change the interval time from 5 mins to 2 mins.
BEGIN
DBMS_SCHEDULER.set_attribute (
    name      => 'schedule_bpel_process',
    attribute => 'repeat_interval',
    value     => 'freq=minutely; interval=2');
 END;



TO disable the job


BEGIN
DBMS_SCHEDULER.set_attribute (
    name      => 'schedule_bpel_process',
    attribute => ’enabled’,
    value     =>FALSE’);
 END;



To drop the Job

BEGIN

DBMS_SCHEDULER.drop_job (job_name => 'schedule_bpel_process');
END;

SOA11G: Automatic Recovery of BPEL Instances


Recently we faced a situation in our STAGE when a lot of instances were generated over –night, without any test being performed. When we dig into it, we found that it’s because of the Automatic Recovery initiation of BPEL Instances by the system itself.

11G gives us options to configure Automatic recovery of BPEL Instances. By default the automatic recovery window set is from 00:00 hrs to 4:00 hrs and the subsequent reties are done at 5mins frequency. You can set it up by setting the BPEL property under soa-infra -> SOA Administartion -> BPEL Properties -> More Advanced BPEL Properties -> recoveryConfig







To cancel Recoverable instances which have failed permanently and are just hogging system resources due to automatic recovery being happened. Go to BPEL Console - soa-infra -> Service Engines -> BPEL. Go to Recovery Tab.


Click to select the instance to recover or cancel. Press “Recover” to Recover OR Press “Mark Cancelled” to stop resubmission of these instances.