I have multiple participants in my transaction manager config as below. I want to switch between UAT and LOCAL participants based on the object property that the current context holds (like if specific fields have specific values). How can I achieve this without having to change Java code and just modify the XML config? I am not sure if org.jpos.transaction.participant.JSParticipant is the way to go.
NOTE: I am simply using jpos-template project but not the Jpos-EE
<txnmgr class="org.jpos.transaction.TransactionManager" logger="Q2">
<property name="queue" value="TXNMGR"/>
<property name="sessions" value="10"/>
<property name="max-sessions" value="100"/>
<participant name="LOCAL" class="com.sanjok.qbean.RestParticipant">
<property name="server-signature-public-key"
value="***"/>
<property name="server-encryption-public-key"
value="***"/>
<property name="base64-client-signature-private-key"
value="***"/>
<property name="base64-client-encryption-private-key"
value="***"/>
</participant>
<participant name="UAT" class="com.sanjok.qbean.RestParticipant">
<property name="server-signature-public-key"
value="***"/>
<property name="server-encryption-public-key"
value="***"/>
<property name="base64-client-signature-private-key"
value="***"/>
<property name="base64-client-encryption-private-key"
value="***"/>
</participant>
<participant class="org.jpos.transaction.participant.SendResponse">
</participant>
</txnmgr>
You can use a
Switchtransaction participant with transaction groups like this:<CONTEXT_KEY_TO_SWITCH_ON>is the context key that contains the value based on which you want to select the destiny.<LOCAL_VALUE>is the value of that key that should make the transaction go to local<UAT_VALUE>is the value of that key that should make the transaction go to UAT.<DEFAULT_ENDPOINT_OR_ERROR_GROUP>is the endpoint (or an error group, if you want to handle it like that) where you want to go if none of the previous match. Like thedefaulton a javaswitchstatement.The
Switchtransaction participant is documented in the jPOS Programmer's Guide section 9.8.1.Basically, we are saying that if the key matches
<LOCAL_VALUE>run groupLOCAL, if it matches<UAT_VALUE>run groupUAT, else run whatever group you define there. Participants in each group get executed right after theSwitchparticipant, and before the next one.Select endpoint based on a property of a context object.
The OP asked in a comment to be able to select the endpoint based on a field of the request. For this we need to use code, it can be in a scripted participant.
Best option would be to use
GroovyParticipantsince it enables to precompile the script, but since the OP is not using jPOS-EE, not clear if he doesn't want to, or just isn't but willing to do.Using
BSHGroupSelector.The simplest option is to use
BSHGroupSelectorsince we just need to modify theSwitchparticipant for this xml:resultis the variable used byGroupSelectorto determine the result of theselectmethod, i.e., get the group to run.Using
JSParticipantThere is no way to write a
GroupSelectorwithJSParticipantthat I know of. However, we can use aJSParticipantto extract field 3 to a context key. We would need to add this participant, previous to theSwitchparticipant:Where
cfg/extract-de3.jshas the following content:The
Switchparticipant definition would be as follows: