Skip to main content

By clicking Submit, you agree to the developerWorks terms of use.

The first time you sign into developerWorks, a profile is created for you. Select information in your developerWorks profile is displayed to the public, but you may edit the information at any time. Your first name, last name (unless you choose to hide them), and display name will accompany the content that you post.

All information submitted is secure.

  • Close [x]

The first time you sign in to developerWorks, a profile is created for you, so you need to choose a display name. Your display name accompanies the content you post on developerworks.

Please choose a display name between 3-31 characters. Your display name must be unique in the developerWorks community and should not be your email address for privacy reasons.

By clicking Submit, you agree to the developerWorks terms of use.

All information submitted is secure.

  • Close [x]

Using Microsoft .NET in WebSphere Message Broker V8: Part 1: Using the .NETCompute node sample

Matthew Golby-Kirk (mgk@uk.ibm.com), Software Developer, IBM
Matthew Golby-Kirk photo
Matthew Golby-Kirk is a software developer working on the WebSphere Message Broker development team at the IBM Hursley Software Lab in the UK. He works on the design and implementation of the HTTP and Web services support, along with the ESQL language run time in WebSphere Message Broker. You can contact Matthew at mgk@uk.ibm.com.
Ben Thompson (bthomps@uk.ibm.com), Consulting IT Specialist, IBM
Author photo
Ben Thompson is a Consulting IT Specialist working on the WebSphere Message Broker development team at the IBM Hursley Software Lab in the UK. He is currently working on the design and implementation of .NET support in Message Broker. In the past, he has worked for IBM Software Services for WebSphere designing and implementing WebSphere solutions for IBM customers worldwide. You can contact Ben at bthomps@uk.ibm.com.

Summary:  This series of four tutorials demonstrates the new support for Microsoft .NET in WebSphere Message Broker V8. Part 1 shows you how to use the .NETCompute node to filter, modify, and create messages, and provides a sample scenario along with explanatory C# code snippets. Readers should be familiar with either Microsoft .NET or WebSphere Message Broker but need not be familiar with both.

View more content in this series

Date:  21 Dec 2011
Level:  Intermediate

Comments:  

Creating the .NET solution in Microsoft Visual Studio

The following images were taken with Microsoft Visual Studio Professional Edition, but you can also use Microsoft Visual Studio Express Edition.

  1. Once Microsoft Visual Studio has launched, you will see the Start Page below. Select New Project, as highlighted in the red box:

    Microsoft Visual Studio new project
  2. The New Project wizard offers you the three types of Project templates. Select the one named Project to filter a Message Broker message. Specify the properties at the bottom of the window as follows and Click OK:

    Name = SampleDotNetProject
    Location = C:\student\DOTNET\lab_sample\visual studio 2010\Projects
    Solution Name = SampleDotNetProject

    New project dialog

    If the WebSphere Message Broker Toolkit is installed after Microsoft Visual Studio, then the Message Broker project templates will be automatically installed ready for you to use. However, if the Broker Toolkit is installed first, then you will need to manually install the templates by executing the file IBM.Broker.DotNet.vsix and stepping through the wizard. If you have used the default installation location, you will find this file at
    C:\Program Files (x86)\IBM\WMBT800\wmbt.

  3. Once the project is created, expand the Solution Explorer view at top right. You should see that the FilterNode.cs file has been created to contain the C# class, and that a reference to IBM.Broker.Plugin has been added. This assembly contains the API provided by Message Broker to transform messages in a .NETCompute node.

    Solution explorer
  4. In the main window, FilterNode.cs has been created with a few lines of template code, which you will add to in the next step. The code is created within a namespace (highlighted below with a red box) that corresponds to the name of the project, SampleDotNetProject. The main entry point for .NET code executed in a .NETCompute is the Evaluate method, which contains a UserCode region where a flow developer typically adds their code. When the Propagate method is invoked, the message assembly leaves the .NETCompute node down the nominated terminal. When you edit this code in the next step, you will change the Evaluate method so that it creates an extra output terminal, and invokes the Propagate method against a chosen output terminal, depending on the content of the message data.

    FilterNode.cs
  5. Replace the contents of the Evaluate method in FilterNode.cs (shown by the red box in the above image) with the sample code provided below. Remember to delete the existing call to the Propagate method from the template that was generated in the previous step. This call is the line of code outTerminal.Propagate(inputAssembly); Also, when you do the copy, be sure to include the definitions for the Alternate and Failure terminals, which appear in the first few lines of the code shown below:

    Listing 1. Sample code for Evaluate method of FilterNode.cs
    
    NBOutputTerminal outTerminal = OutputTerminal("Out");
    NBOutputTerminal altTerminal = OutputTerminal("Alternate");
    NBOutputTerminal failureTerminal = OutputTerminal("Failure");
    
    NBMessage inputMessage = inputAssembly.Message;
    NBElement root = inputMessage.RootElement;
    
    #region UserCode
         // Add user code in this region to filter the message
         // The following expression deliberately uses LastChild in
         // preference to FirstChild in case an XML Declaration is present!
         switch(root[NBParsers.XMLNSC.ParserName].LastChild.Name)
         {
              case "LoyaltyProgram":
                   outTerminal.Propagate(inputAssembly);
                   break;
              case "SaleEnvelope":
                   altTerminal.Propagate(inputAssembly);
                   break;
              default:
                   failureTerminal.Propagate(inputAssembly);
                   break;
         }
    #endregion UserCode
    

    Save the edited file FilterNode.cs" Press Ctrl-S or use the option from the File menu.
  6. Add a new class to modify a Message Broker message from Solution Explorer: Right click the Project level of the hierarchy (SampleDotNetProject) and select Add => Class. In the resulting Add New Item dialog, select Class to modify a Message Broker message, and make sure that you specify the name ModifyNode.cs by default, the wizard suggests a name of ModifyNode1.cs). Click Add:

    Add new item dialog for ModifyNode.cs
  7. Edit the ModifyNode.cs file using the code below to populate the UserCode region of the template:

    Listing 2. Sample code for UserCode region of ModifyNode.cs
    
    #region UserCode
         NBElement xmlRoot = outputRoot[NBParsers.XMLNSC.ParserName];
         NBElement xmlDecl = xmlRoot[NBParsers.XMLNSC.XmlDeclaration, "XmlDeclaration"];
         if (xmlDecl == null)
         {
              // Create an XML Declaration if required
              NBParsers.XMLNSC.CreateXmlDeclaration(xmlRoot, "1.0", "UTF-8", "yes");
         }
         string notarget = "";
         string ns = "http://www.example.org/store";
         NBElement storeDetails = xmlRoot[notarget,"LoyaltyProgram"][ns, "StoreDetails"];
         string storeName = "";
         string storeStreet = "";
         string storeTown = "Happyville";
         switch ((string)storeDetails[ns, "StoreID"])
         {
              case "001":
                   storeName = "Broker Brothers Central";
                   storeStreet = "Exuberant Avenue";
                   break;
              case "002":
                   storeName = "Broker Brothers Mall";
                   storeStreet = "Enthusiastic Crescent";
                   break;
              case "003":
                   storeName = "Broker Brothers District";
                   storeStreet = "Peaceful Road";
                   break;
         }
         storeDetails.CreateLastChild(ns, "StoreName", storeName);
         storeDetails.CreateLastChild(ns, "StoreStreet", storeStreet);
         storeDetails.CreateLastChild(ns, "StoreTown", storeTown);
    #endregion UserCode
    

    Save the edited file ModifyNode.cs" Press Ctrl-S or use the option from the File menu.
  8. Add a new class to create a Message Broker message using the Solution Explorer: Right-click the Project level of the hierarchy (SampleDotNetProject) and select Add => Class. In the Add New Item dialog, select Class to create a Message Broker message, and make sure that you specify the name CreateNode.cs by default, the wizard suggests a name of CreateNode1.cs). Click Add:

    Add New Item dialog for CreateNode.cs
  9. Edit the CreateNode.cs file using the code below to populate the UserCode region of the template:

    Listing 3. Sample code for UserCode region of CreateNode.cs
    
    #region UserCode
         outputRoot["Properties"]["MessageSet"].SetValue("DotNetLibrary");
         outputRoot["Properties"]["MessageType"].SetValue("File");
         outputRoot.CreateLastChildUsingNewParser(NBParsers.DFDL.ParserName);
         NBElement File = 
              outputRoot[NBParsers.DFDL.ParserName].CreateFirstChild(null, "File");
         NBElement inxmlRoot = inputRoot[NBParsers.XMLNSC.ParserName];
         IEnumerable<NBElement> invoices = 
              inxmlRoot["SaleEnvelope"]["SaleList"].Children("Invoice");
         foreach (NBElement invoice in invoices)
         {
              TransformInvoice(File, invoice);
         }
         // Define Local Environment override to dynamically control the MQOutput node
         NBElement outLE = outAssembly.LocalEnvironment.RootElement;
         NBElement mqLE = 
              outLE.CreateFirstChild(null, "Destination").CreateFirstChild(null, "MQ");
         mqLE = mqLE.CreateFirstChild(null, "DestinationData");
         mqLE.CreateFirstChild(null, "queueName", "DOTNET.OUT");
    #endregion UserCode
    

  10. Add a new method named TransformInvoice to the CreateNode.cs file using the code provided below. Be sure to copy it to the correct position in the file. In the hierarchy of the file, this method should be a sibling of the CopyMessageHeaders method.

    Listing 4. Sample code for the TransformInvoice method of CreateNode.cs
    
    private static void TransformInvoice(NBElement outFileEl, NBElement inInvEl)
    {
         // This method creates a structure based on
         // the Invoice Element in the input message
         IEnumerable<NBElement> items = inInvEl.Children("Item");
         foreach (NBElement item in items)
         {
              NBElement record = outFileEl.CreateLastChild(null, "Record");
              string notgt = "";
              record.CreateLastChild(notgt, "Code1", (string)item["Code", 0]);
              record.CreateLastChild(notgt, "Code2", (string)item["Code", 1]);
              record.CreateLastChild(notgt, "Code3", (string)item["Code", 2]);
              record.CreateLastChild(notgt, "Description", (string)item["Description"]);
              record.CreateLastChild(notgt, "Category", (string)item["Category"]);
              record.CreateLastChild(notgt, "Price", (decimal)item["Price"]);
              record.CreateLastChild(notgt, "Quantity", (Int32)item["Quantity"]);
         }
    }
    

    You can leave the CopyMessageHeaders method (which was provided as part of the template when you added CreateNode.cs) unchanged. Save the edited file CreateNode.cs: Press Ctrl-S or use the option from the File menu.
  11. From the Solution Explorer, right-click on the Solution and select Build (or use the F6 shortcut):

    Build the Solution

    How to open the Output window in Microsoft Visual Studio

    If the Output window is not visible and you are using Microsoft Visual Studio Professional Edition, you can open it using Debug => Windows => Output. If the Output window is not visible and you are using Microsoft Visual Studio Express Edition, you can open it using View => Output.

  12. The Output window shows you where the built assembly file has been saved on your file system. Depending on how you have Microsoft Visual Studio configured, the Build Output window may not be immediately visible, in which case you should follow the instructions in the sidebar to open the Output window. Build Output

    If you have used the default naming suggested throughout the tutorial, then you should find that the assembly file has been saved at:
    C:\student\DOTNET\lab_sample\visual studio 2010\Projects\SampleDotNetProject\
    SampleDotNetProject\bin\Debug\SampleDotNetProject.dll
    

The next section of the tutorial shows you how to unite the message flow development and the C# assembly and deploy to Message Broker.

5 of 9 | Previous | Next

Comments



Help: Update or add to My dW interests

What's this?

This little timesaver lets you update your My developerWorks profile with just one click! The general subject of this content (AIX and UNIX, Information Management, Lotus, Rational, Tivoli, WebSphere, Java, Linux, Open source, SOA and Web services, Web development, or XML) will be added to the interests section of your profile, if it's not there already. You only need to be logged in to My developerWorks.

And what's the point of adding your interests to your profile? That's how you find other users with the same interests as yours, and see what they're reading and contributing to the community. Your interests also help us recommend relevant developerWorks content to you.

View your My developerWorks profile

Return from help

Help: Remove from My dW interests

What's this?

Removing this interest does not alter your profile, but rather removes this piece of content from a list of all content for which you've indicated interest. In a future enhancement to My developerWorks, you'll be able to see a record of that content.

View your My developerWorks profile

Return from help

static.content.url=http://www.ibm.com/developerworks/js/artrating/
SITE_ID=1
Zone=WebSphere
ArticleID=781973
TutorialTitle=Using Microsoft .NET in WebSphere Message Broker V8: Part 1: Using the .NETCompute node sample
publish-date=12212011
author1-email=mgk@uk.ibm.com
author1-email-cc=
author2-email=bthomps@uk.ibm.com
author2-email-cc=

Tags

Help
Use the search field to find all types of content in My developerWorks with that tag.

Use the slider bar to see more or fewer tags.

Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere).

My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).

Use the search field to find all types of content in My developerWorks with that tag. Popular tags shows the top tags for this particular content zone (for example, Java technology, Linux, WebSphere). My tags shows your tags for this particular content zone (for example, Java technology, Linux, WebSphere).