# BPEL & Real Time processing

- The problem
- A Solution
- Download
- Patch
- Discussion

## The problem | Top

BPEL is certainly not a real time engine. The BPEL specification does not say anything about the implementation of the wait, or onAlarm activities.
In order to preserve some SLA, it could be very tempting to use the onAlarm activity to trap or work around the too long execution of an activity, like invoke or receive.
The only thing we can predict when using a wait or onAlarm in BPEL, is that it will take at least the required amount of time. How long will it take beyond this amount of time, that cannot be predicted.

## A Solution | Top

### Presentation

The problem we are facing can be addressed from several programming languages, including Java.
The idea would be to use some Java class to:

1. Start the Asynchronous BPEL Process
2. Poll the BPEL Server, waiting for the callback from the Asynchronous process (watchdog)

At the same time, another thread would be waiting, for the amount of time defined by the timeout.
If the result is found in time, the watch dog notifies the main thread.
If the timeout expires, it stops the watch dog.
As a result, this service would return the reponse from the Asynchronous process, or a timeout message.
This Java component can be exposed as a Web Service. This Web Service can be invoked synchronously from BPEL, through SOAP, or better, through WSIF.

### Implementation

We tried to keep it simple and clear, we have 4 Java classes.

- AsyncTwoWaysSimpleInvocation.java
- AsyncCallbackImpl.java
- ProcessService.java
- TimeoutException

AsyncTwoWaysSimpleInvocation.java is the class that takes care of instantiating the asynchronous process, it uses the AsyncCallbackImpl.java to poll the server in order to get the response from the process. The AsyncCallbackImpl.java is actually used by the watchdog, spawned as a new thread. This thread notifies AsyncTwoWaysSimpleInvocation.java in case the response comes back in time. It is stopped otherwise.
ProcessService.java is a wrapper on top of AsyncTwoWaysSimpleInvocation.java, to be eventually exposed as a Web Service. This one also contains a main method, for tests, and illustration of the way to use it.

#### Parameters of ProcessService.invokeService

Name | Description | Example
--- | --- | ---
processName | Name of the BPEL process to start | "BPELSayHiAndWait"
domainName | Domain of the Process | "default"
pswd | Password of the domain | "welcome1"
methodToExecute | Name of the method to start the process with | "initiate"
contextFileName | Context file, containing all the conncetion properties | "context.properties"
pollingLoopTime | Time between th eloops of the watchdog, in milliseconds | 100L
outputVariable | Name of the variable containing the response of the asynchronous process | "outputVariable"
payload | Payload to start the process with, as a String |
    <?xml version='1.0' encoding='windows-1252'?>
    <BPELSayHiAndWaitProcessRequest xmlns="http://xmlns.oracle.com/BPELSayHiAndWait">
      <input>Async Test</input>
    </BPELSayHiAndWaitProcessRequest>
parentId | Id of the instance of the calling process, to preserve the TreeFinder structure. | 60091, obtained from the BPEL parent, using the ora:getInstanceId() XPath function
rootId | Id of the root instance, to preserve the TreeFinder structure. | 60090. obtained from the BPEL parent, using getRootId() (to expose)
timeout | Timeout, in milliseconds. If this value is set to -1, there is no timeout. In that case, the interest of the watchdog is to get the response from the dehydration store. This method would also be a way to make a synchronous call to an asynchronous process. | 20000L
returns | The xml response, as a String. |
    <BPELSayHiAndWaitProcessResponse xmlns="http://xmlns.oracle.com/BPELSayHiAndWait">
       <result>Hello Async Test!</result>
    </BPELSayHiAndWaitProcessResponse>
Throws | TimeoutException, if the timeout has expired |

Note: The required configuration for the connection is stored is a file named context.properties. See about that the lines 46 & 47 of AsyncTwoWaysSimpleInvocation.java. Depending on the location of that file, comment or uncomment those lines.
Also, line 116 of AsyncTwoWaysSimpleInvocation.java, we use the stopWatching method, instead of just stop (deprecated). To be able to do so, we need the patch, downloadable below.

## Download | Top

The Java sources

## Patch | Top

The Patch

## Discussion | Top

As you can see above, the inbound and outbound payloads are represented as Strings, in order to be as generic as possible. This means that as long as you have the schema associated with the asynchronous service to take care of, you can use the following XPath functions:

- On the way out: ora:getContentAsString()
- On the way back in: ora:parseEscapedXML()

In 10.1.3.3, there is a new configuration property set as the process level, keepGlobalVariables, that must be set to true, its default value is false. Otherwise, the variable (outputVariable) will not be written into the dehydration store.