Introduction : Process Server selectors let you determine dynamically which implementation to invoke at runtime based on a specified set of criteria, which is currently limited to dates. Although the standard selector implementation has the date limitation, with a little knowledge (and a little programming experience) you can create a custom selector that can use more varied selection criteria.
A selector can use any Process Server Components as its outcome choices, including those defined in other modules. With a custom selector, you can write code that makes selections based on the values of some interface parameter passed into the selector. Othe components can then use selectors that need to select behavior based on the criteria expressed in the selector.
Scenario :
1. Create New Module : CustomSelectorModule
2. Create New BO(Business Object) in this module : "CustomSelectorData" and add 3 attributes in that BO.
3. We will create another BO MessageData consists of data and CustomSelectorData fields.
4. create one interface "HandleMessage" having one way operation contains MessageData as input type.
5. We’ll now create the two Java components that our selector will choose from based on our selection algorithm. The Java code is very simple; it writes a string to System.out that identifies itself. We can now insert the code that differentiates the two POJO components.
In the Assembly Editor, add java component and add interface to it then right-click on Component1 and select Generate Implementation. Select default package and click OK. Now Component1 is open in the Java Editor. Scroll down to the showMsg method and replace the todo comment with this line of code:
System.out.println("Style 1: ");
6. Just like step 5 we have to create 2nd java component which it contains code :
System.out.println("Style 2: ");
*Creating the CustomSelector Java class
This class decides which component to select. We could put any logic we like here, but in order to show how dynamic a custom selector can be, we’ll merely invoke the component specified in the metadata contained in the business object passed in through the HandleMessage interface.
7. In New Java Class Wizard, enter for the Package enter com.ibm.bpm.customselectors, and for the Name enter : CustomSelector
8. In the Implemented Interfaces Selection window start tying com.ibm.wbiserver.common.selection.S until the SelectionAlgorithm interfaces appears in the list. Select it, and click OK,
The final New Java Class window should look like this :
9. Add this code to the new java class
public static SelectionAlgorithm getInstance() {
return new CustomSelector();
}
The runtime will use this method to instantiate a new instance of this object when
required.
10. We now need to augment the generated code with new code that will provide our new custom selection logic. We'll replace the generated selectTargets method that was created for us by the wizard with the code below. The values in the selectionKeys parameter passed into the selectTargets method defined below will be set by code that we'll create in the next section of this exercise. In our case it will contain dynamic info about the component we would like our selector to invoke.
add this code : in CustomSelector class:
public static SelectionAlgorithm getInstance() {
return new CustomSelector();
}
public List selectTargets(String selectorNamespace, String
selectorName, String componentType,String operationName, Object[]
selectionKeys, boolean returnAllMatches) throws SelectorException
{
// Dump relevant input parameters
for (int i=0;i<selectionKeys.length;i++) System.out.println("***** "
+ i + ":\t " + selectionKeys[i]);
// Replace empty strings with nulls. See discussion below for rationale
for (int i=0;i<selectionKeys.length;i++) {
Object temp = selectionKeys[i];
if (temp!=null && temp.equals(""))
selectionKeys[i]=null;
}
// The elements of the selectionKeys array are the attributes specified
// in the ParameterDef entries of the selector's .sel file.
// The order of the parameters is preserved.
String targetComponent = (String)selectionKeys[0];
String targetExport = (String)selectionKeys[1];
String targetModule = (String)selectionKeys[2];
/*
Now that we have the requested component or module+export, we can
create an SCAInvocationTarget. This is a representation of the desired
target which we can return to the runtime. If we pass a non-null
targetComponent, SCAInvocationTarget will ignore the other parameters
and create a target definition to a local component with that name. If
targetComponent is null, it will create a link to the export defined by
the targetExport and targetModule fields.
*/
InvocationTarget target = new SCAInvocationTarget(targetModule,
targetExport, null, targetComponent);
// In the line above, we specify null for the third parameter.
// The constructor for SCAInvocationTarget
// can take the name of an Import in this position. However, an import is just an SCA component so you
// can give the import's name in the fourth slot instead.
// We must return a List. However, the list should only contain
// one element since the runtime currently only supports calling one
// target
ArrayList al = new ArrayList(); al.add(target);
return al;
}
11. Creating a custom selector component:
We'll start by creating a standard selector that we will augment with our 'custom selector'
capabilities. If we create a standard selector we can take advantage of the code generation
features of the Integration Server tooling. Its easier for us to modify generated code than
to create the generated artifacts from scratch.
1. To create the Selector, in the SelectorTest module expand the Business Logic
folder. Right-click Selectors and select New - Selector name : "MyCustomSelector"
2. Add the interface to it. and select default destination as Component 1. and specify selection criteria as xpath. and specify those details as following.
12. We'll now edit the generated code to give it the custom behavior. In the Business Integration Perspective, right-click the MyCustomSelector item, and select Show Files.
This opens the Physical Resources view, which displays the generated files for the project. Notice that the MyCustomSelectors.sel and MyCustomSelectors.selt files are highlighted.
13. Right-click MyCustomSelectors.sel, and select Open With - Text Editor. The MyCustomSelectors.sel file is an XML file that contains the definition for the selector. We'll modify the XML to allow us to use custom selection. In the Text Editor, we need to make several changes:
1. Replace this line of code:
<Selector>com.ibm.wbiservers.common.selection.GenericSelector</Se
lector>
With this line, which refers to our custom Java class:
<Selector>com.ibm.customselectors.CustomSelector</Selector>
2. Find the line of code that looks like this:
<ParameterDef xsi:type="acd:XPathParameterDef" parameter="inputMsg"
xPath=" metadata/targetComponent"/>
3. Insert these these 2 lines of code immediately after that line:
<ParameterDef xsi:type="acd:XPathParameterDef" parameter="inputMsg" xPath="data/targetExport"/> <ParameterDef xsi:type="acd:XPathParameterDef" parameter="inputMsg" xPath=" data/targetModule"/>
Those 2 lines add the rest of the CustomSelector metadata object attributes that will be used by our CustomSelector Java class. These parameters are passed to the selectTargets method of our CustomSelector object as the selectionKeys
parameter.
Its important that the three ParameterDef statements are in the order shown above, i.e. targetComponent, targetExport, targetModule (the order in which they are extracted in the selectTargets method).
4. Click Save and Close to exit the MyCustomSelectors.sel file.
Now we have to test this module by deploying that into server. and we will do test Component.