Using For and While Loops
- Neuron ESB
- Samples and Walkthroughs
- Process Samples
- Using For and While Loops
This sample demonstrates how you can use For, For Each, While and Break process steps to execute looping logic in your processes.
Process Components Demonstrated:
- Xml Query
- For Each
- Execute Process
There are five processes included with this sample. The For Loop, For Each Loop and While Loop processes all implement the same scenario. The fourth process demonstrates the use of the Break process step. The fifth process handles exceptions by enriching the exception and re-throwing it.
The steps displayed in Figure 1 are the same for the For Loop, For Each Loop and While Loop processes. The beginning of the For Loop process depicted in Figure 1 has a Push, Xml Query, Code and Pop process step. These steps are used to retrieve a list of email addresses from SQL Server and create a context property that the loop process steps will utilize for determining the number of iterations to execute. These steps are contained within the Try execution block of an Exception process step. Inside the Catch execution block, a Process Execute step calls the Exception Handling process to process any exceptions that might arise.
Figure 2 shows the For process stepand the steps to be executed for each iteration. These steps will dynamically set the email address of the outgoing message and publish that message to the bus.
The For process step requires three code blocks to execute – Initializer, Condition and Iterator. These code blocks are shown in Figures 3-5.
For Each Loop
Figure 6 shows the For Each process stepand the steps to be executed for each iteration. (Figure 1 contains the same process steps as the beginning section of the For Each Loop process). These steps will dynamically set the email address of the outgoing message and publish that message to the bus.
Figure 7 shows the While process stepand the steps to be executed for each iteration. (Figure 1 contains the same process steps as the beginning section of the While Loop process). These steps will dynamically set the email address of the outgoing message and publish that message to the bus. Notice that the While process step requires a separate iteration Code step.
In contrast to the For process step, the While process step only requires one code block to execute – Condition. This code block is shown in Figure 8.
Figure 9 shows the Break process sample. The steps in the process demonstrate the use of a For process step with a decision step to determine when the For Loop should Break. The For process step initializes a counter to zero and increments by one for each iteration. The Decision step checks the counter, and when it is equal to two the Break step will execute.
Running the Sample
- Database creation – A SQL database is required to run this sample. To create the database and table associated with this sample run the SQL script titled MessageData Creation Script found in the Appendix section of this document.
- For each process, verify the connection string used in the Xml Query process steps. Follow the instructions titled Verify Connection Strings found in the Appendix section of this document.
Open the Sample
To open this sample, see the topic Using the Neuron Samples and select the Using For and While Loops sample.
Run the Sample
- Navigate to the Process Designer by selecting the Processes tab in the left panel of Neuron Explorer.
- Select the For Loop process from the list on the left of the designer.
- On the Process Designer toolbar click the Test button . This will open the Edit Test Message Dialog shown in Figure 10.
- Leave the default test message in the message field. The contents of this message are not important for the sample. Click the OK button on the Edit Test Message dialog. Once the dialog is closed the process will begin execution.
- As the process runs each step will be highlight in green as it is executed. In addition, any trace messages will be visible in the Trace Window. As you visually follow the execution not the sequence of steps. When the process completes, the Trace Window should look similar to what is shown in Figure 11.
- Repeat steps 1 – 5 for the For Each Loop, While Loop and Break processes. You should see similar results to the trace window in Figure 11.
All processes with the exception of the Code process step are configured by selecting and setting their properties in the property grid located at the bottom right of the process designer. The Code process step is configured by selecting the Edit option from the short cut menu that is available when right-clicking the Code step in the process designer. See the process documentation for more information.
USE [master] GO CREATE database MessageData GO USE [MessageData] GO CREATE table [Customer](CustomerID int, Name nvarchar(50),EmailAddress nvarchar(50)) GO INSERT INTO [Customer] (CustomerID, Name, EmailAddress) VALUES (1001, 'Northwind Traders', 'email@example.com') GO INSERT INTO [Customer] (CustomerID, Name, EmailAddress) VALUES (1002, 'LitWare Inc.', 'firstname.lastname@example.org') GO INSERT INTO [Customer] (CustomerID, Name, EmailAddress) VALUES (1003, 'AdventureWorks Cycles', 'email@example.com') GO
MessageData Creation Script
Verify Connection Strings
- Open Neuron Explorer and navigate to the Processes section.
- Select the For Loop process listed in left panel.
- Select the Xml Query process step named Get Customer List.
- In the property grid select the ConnectionString property and click the ellipsis button on the right of the property value.
- The Connection Properties dialog will open as shown below in Figure 13.
- Make sure the SQL Server and Database Name is correct then click the Test Connection button to assure that the connection string is correct.
- Click the OK button on the Connection Properties dialog to save your changes.
- Repeat steps 3- 7 for the While Loop process.
- Save the changes to the process and the configuration by first clicking the Apply button at the top of the process designer then the Save button on the Neuron Explorer toolbar.