Monday, December 19, 2011

SOA/BPM APIs - Manipulating Composites and purging instances

With the release of the BPM 11g Feature Pack, I've started revisiting the whole API space. There are many blog entries out there dealing with this area - however it is usually only one particular use case that's covered e.g. how to purge instances etc.

However, there are exceptions - Rommel Pino's series of posts on the API are well worth reading, as well as anything from Mark Nelson http://redstack.wordpress.com/author/markxnelson/


Over the next couple of posts I want to look at the different API levels -

- composite / composite instance
- component/ component instance --> BPM process
- human task

My starting point is the following simple BPM process.





My XSD-



My test input -




Notice I have created a composite sensor on the FileAdapter (Read)

Composite API


I going to go thru the composite API - querying composites and their components.
I will also show a couple of approaches to purging instances.

1. Purge all instances of a composite based on instance state e.g. STALE.
This we can do at composite level.

2. Purge all instances of a composite based on instance state e.g. STALE. but doing
individual auditing for each instance. This we can do at composite instance level.

3. Purge a composite instance based on a sensor value. e.g. purge instance for orderID = 3.

The following code - will query all composites and list their components -

****************************************************************************

public void getAllComposites() throws Exception {
System.out.println("getAllComposites()");
Locator locator = null;
int purgedInstanceCount = 0;

try {
locator = this.getLocator();
CompositeFilter compositeFilter = new CompositeFilter();
CompositeInstanceFilter instanceFilter = new CompositeInstanceFilter();

//Get all composites from the domain based on the filter given
List composites =
locator.getComposites(compositeFilter);

System.out.println("*** Found " + composites.size() + " Composites.");

//Iterate through the composites.
Iterator compositeIter = composites.iterator();
while (compositeIter.hasNext()) {
Composite composite = (Composite)compositeIter.next();
System.out.println("Composite DN = " +composite.getCompositeDN());
System.out.println("Composite State = " + composite.getState());
System.out.println("Composite Instance count = " + composite.getInstanceCount());
List components = composite.getComponents();

// List Components

Iterator componentsIter = components.iterator();
while (componentsIter.hasNext()) {
Component component = (Component)componentsIter.next();
System.out.println("*** Component DN = " +component.getDN());
System.out.println("*** Component Name = " + component.getName());
System.out.println("*** Component Type = " + component.getImplementationType());
System.out.println("*** Component Total Instances = " + component.getNumberOfInstances());
System.out.println("*** Component Active Instances = " + component.getNumberOfActiveInstances());

}
//
}

} catch (Exception e) {
e.printStackTrace();
}
}
********************************************************************

My test output is as follows -



The following code will query the composite instances and output their composite sensor values -

*******************************************************************

public void getInstances(String compositeName) throws Exception {
System.out.println("getInstances() for composite " + compositeName);
Locator locator = null;

try {
locator = this.getLocator();
CompositeFilter compositeFilter = new CompositeFilter();
CompositeInstanceFilter instanceFilter = new CompositeInstanceFilter();

instanceFilter.setCompositeDN(compositeName);

//Get composite from the domain based on the filter given
List compositeInstances =
locator.getCompositeInstances(instanceFilter);

System.out.println("*** Found " + compositeInstances.size() + " Composite Instances.");

//Iterate through the composites.
Iterator compositeIter = compositeInstances.iterator();
while (compositeIter.hasNext()) {
CompositeInstance compositeInstance = (CompositeInstance)compositeIter.next();
System.out.println("Composite Instance DN = " +compositeInstance.getCompositeDN());
System.out.println("Composite Instance State = " + compositeInstance.getState());
System.out.println("Looking for Composite Sensors...");

List l_sensorData = compositeInstance.getSensorData();
for (int i =0; i < l_sensorData.size(); i++){
SensorData sensorData = l_sensorData.get(i);
System.out.println("*** Sensor name " + sensorData.getSensor().getName());
System.out.println("*** Sensor value " + sensorData.getData().toString());
}
}

}
catch (Exception e){
e.printStackTrace();
}
}

************************************************************************

My test output -



------------------------------------------------------------------------

Let's now do something with the instances e.g.
purge Stale instances.



Instance state can have the following values -






Here I have a couple of stale instances -








I use the following method -

***************************************************************************+

public void deleteCompositeInstances(String compositeName, int state) throws Exception {
System.out.println("deleteCompositeInstances() for composite " + compositeName + " with state " + state );
Locator locator = null;
String orderID = null;
try {
locator = this.getLocator();
CompositeInstanceFilter instanceFilter = new CompositeInstanceFilter();

instanceFilter.setCompositeDN(compositeName);
instanceFilter.setState(state);

//Get composite from the domain based on the filter given
List compositeInstances =
locator.getCompositeInstances(instanceFilter);

System.out.println("*** Found " + compositeInstances.size() + " stale composite instances.");

//Iterate through the composites.
Iterator compositeIter = compositeInstances.iterator();
while (compositeIter.hasNext()) {
CompositeInstance compositeInstance = (CompositeInstance)compositeIter.next();

List l_sensorData = compositeInstance.getSensorData();
for (int i =0; i < l_sensorData.size(); i++){
SensorData sensorData = l_sensorData.get(i);
orderID = sensorData.getData().toString();
}
System.out.println("About to delete instance for orderID = " + orderID);
compositeInstance.delete();

}

}
catch (Exception e){
e.printStackTrace();
}
}


***************************************************************************






We can also purge at the composite, as opposed to the compositeInstance level

************************************************************************

public void deleteCompositeInstancesNoAudit(String compositeName, int state) throws Exception {
System.out.println("deleteCompositeInstancesNoAudit() for composite " + compositeName + " with state " + state );
Locator locator = null;
try {
locator = this.getLocator();
CompositeFilter compositeFilter = new CompositeFilter();
compositeFilter.setCompositeDN(compositeName);

CompositeInstanceFilter instanceFilter = new CompositeInstanceFilter();
instanceFilter.setState(state);

//Get composite from the domain based on the filter given
List l_composites =
locator.getComposites(compositeFilter);

//Iterate through the composites.
Iterator compositeIter = l_composites.iterator();
while (compositeIter.hasNext()) {
Composite composite = (Composite)compositeIter.next();
System.out.println("About to purge "+ composite.getInstances(instanceFilter).size() + " instances... ");
composite.purgeInstances(instanceFilter);

}

}
catch (Exception e){
e.printStackTrace();
}
}


******************************************************************************

The composite sensor can be used in em to find instances based on orderID.



We can also purge instances based on the composite sensor value.
My composite sensor is called compSensor_orderID -

Here is the code -

**********************************************************************
public void deleteCompositeInstance4OrderId(String compositeName, String orderID) throws Exception {
System.out.println("deleteCompositeInstance4OrderId() for composite " + compositeName + " with orderID " + orderID );
Locator locator = null;

try {
locator = this.getLocator();
CompositeInstanceFilter instanceFilter = new CompositeInstanceFilter();

instanceFilter.setCompositeDN(compositeName);

List l_sensorFilter = new ArrayList ();
SensorFilter sensorFilter = new SensorFilter("compSensor_orderID",Sensor.SensorDataType.STRING,Operator.EQUALS,orderID);
l_sensorFilter.add(sensorFilter);

instanceFilter.setSensorFilter(l_sensorFilter);

//Get composite from the domain based on the filter given
List compositeInstances =
locator.getCompositeInstances(instanceFilter);

System.out.println("*** Found " + compositeInstances.size() + " composite instance(s) for orderID " + orderID);

//Iterate through the composites.
Iterator compositeIter = compositeInstances.iterator();
while (compositeIter.hasNext()) {
CompositeInstance compositeInstance = (CompositeInstance)compositeIter.next();

System.out.println("About to delete instance for orderID = " + orderID);
compositeInstance.delete();

}

}
catch (Exception e){
e.printStackTrace();
}
}

**********************************************************************

2 comments:

  1. Where do you run this code? on a client app?
    would you mind sharing it?

    ReplyDelete
  2. Hi René,
    Client app, just cut and paste the code, I have other blog posts that show the required libraries.

    ReplyDelete