Skip to content

Using For and While Loops

Note: It is recommended that the Neuron process documentation be reviewed before running this sample. The documentation thoroughly describes each process component individually and describes how to configure each one as well as how to construct a process and associate it with a Neuron Publisher or Subscriber. See the Configuration Notes section at the end of this document for more information.

Overview

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:

  • For
  • While
  • Break
  • Xml Query
  • For Each
  • Push/Pop
  • Decision
  • Code
  • Execute Process

Solution

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.

For Loop

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 1: The beginning of the For Loop and While Loop processes as displayed in the Neuron Process Designer.

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.

Figure 2: For Loop process as displayed in the Neuron Process Designer.

The For process step requires three code blocks to execute – Initializer, Condition and Iterator. These code blocks are shown in Figures 3-5.

Figure 3: The Initializer code block in a For process step. This code step initializes the total number of iterations and a counter to keep track of the current iteration.
Figure 4: The Condition code block in a For process step. This code step validates the current iteration counter against the total number of iterations the For loop should execute.
Figure 5: The Iterator code block in a For process step. This code step increments the current iteration counter by 1.

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 6: For Each Loop process as displayed in the Neuron Process Designer.

While Loop

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.

Figure 7: While Loop process as displayed in the Neuron Process Designer.

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 8: The Condition code block in a While process step. This code step validates the current iteration counter against the total number of iterations the For loop should execute.

Break

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.

Figure 9: The Break process as displayed in the Neuron Process Designer.

Running the Sample

Prerequisites

  1. 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.
  2. 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

  1. Navigate to the Process Designer by selecting the Processes tab in the left panel of Neuron Explorer.
  2. Select the For Loop process from the list on the left of the designer.
  3. On the Process Designer toolbar click the Test button . This will open the Edit Test Message Dialog shown in Figure 10.
Figure 10: Edit Test Message dialog displaying the default test message.
  1. 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.
  2. 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.
Figure 11: For Loop process test run displaying results and exceptions in the Trace Window.
  1. 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.
As an exercise try running this samples process live using the Neuron Test Client. The sample configuration file has two Parties that can be used to accomplish this. The OrderPublisher Party can be used to run the process in the OnPublish mode and the OrderProcessor Party can be used to receive the messages that are published to the ESB. You will need to reconfigure the OrderPublisher Party for each process you want to test.

Configuration Notes

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.

Figure 12: The Neuron Process Designer displaying the For Loop process. Property Grid at the bottom right displaying the properties for the For process step.

Appendix

 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', 'orders@northwind.com')
 GO
 INSERT INTO [Customer] (CustomerID, Name, EmailAddress)
 VALUES (1002, 'LitWare Inc.', 'accounts@litware.com')
 GO
 INSERT INTO [Customer] (CustomerID, Name, EmailAddress)
 VALUES (1003, 'AdventureWorks Cycles', 'admin@adventureworks.com')
 GO 

MessageData Creation Script

A copy of the full database script for this sample can be found in the Neuron Samples folder under the installation directory: Samples\Processes, file name: CreateSampleDB.sql

Verify Connection Strings

  1. Open Neuron Explorer and navigate to the Processes section.
  2. Select the For Loop process listed in left panel.
  3. Select the Xml Query process step named Get Customer List.
  4. In the property grid select the ConnectionString property and click the ellipsis button on the right of the property value.
  5. The Connection Properties dialog will open as shown below in Figure 13.
Figure 13: Connection Properties dialog.
  1. Make sure the SQL Server and Database Name is correct then click the Test Connection button to assure that the connection string is correct.
  2. Click the OK button on the Connection Properties dialog to save your changes.
  3. Repeat steps 3- 7 for the While Loop process.
  4. 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.
Was this article helpful?
Dislike 0
Previous: Using the ODBC Process Step
Next: Transactional Process