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 4: Using the .NETCompute node for exception handling

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.
(An IBM developerWorks Contributing Author)
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.

Summary:  This series of four tutorials demonstrates the new support for Microsoft .NET in WebSphere Message Broker V8. Part 4 shows you how to use the .NETCompute node for exception handling, using a scenario that builds in complexity as additional exception conditions are deliberately produced and then handled. 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:  22 Feb 2012
Level:  Intermediate

Comments:  

Hot swap deploy of a .NETCompute node assembly

In the tutorial so far, you have been developing and editing a message flow that has undergone several changes. Each time the flow was altered, you retested it using the Message Broker Toolkit's built-in test client, which deploys the new message flow and then sends a test message through the flow. Whilst doing this, you may have noticed a few seconds delay at the beginning of each test as the Broker packaged and then deployed the flow. To reduce the number of flow redeploys, Message Broker also includes a Hot Swap deploy feature for .NET code written for execution in a .NETCompute node. It speeds up development when writing .NET code for use in Message Broker. When you edit and rebuild a .NET assembly using Microsoft Visual Studio, the assembly file is resaved on the file system. The Broker's execution group has a file watcher that notices this change and then reloads all application domains that use the assembly, so that you don't have to rebuild your .NET code and then redeploy their message flow. Only the first step is required and Hot Swap ensures that the changes are picked up by the Broker. To demonstrate this handy feature, you will create a new message flow that contains a .NETCompute node. Before starting, save and close any open files in Message Broker Toolkit from previous sections of the tutorial.

  1. This scenario will reuse the same queues that you have already created, so begin by removing anything deployed to the default runtime Broker configuration. In the Message Broker Toolkit Brokers view, right-click the execution group default and choose Delete => All Flows and Resources:

    Delete All Flows and Resources
  2. From the Quick Start panel, select Start by creating an application, as shown below:

    Quick Starts Panel
  3. In the resulting wizard, specify an application name of App_DotNet_HotSwap and click Finish:

    Quick Starts Panel
  4. Click New underneath the application that has been created, and in the resulting pop-up window, click Message Flow:

    New Message Flow
  5. Specify a Message Flow name of MyFlow and click Finish:

    New Message Flow Wizard
  6. The Broker message flow you will use to demonstrate the Hot Swap feature is simple. On the flow canvas, drag and drop an MQ Input node and MQ Output node from the WebSphere MQ drawer of the flow palette, and a .NETCompute node from the Transformation drawer of the flow palette. Rename the MQ Input node DOTNET.EXCEPTIONS.IN and rename the MQ Output node DOTNET.EXCEPTIONS.OUT.

    On the MQ Input node Basic properties panel, configure the Queue name to be DOTNET.EXCEPTIONS.IN
    On the MQ Input node Input Message Parsing properties panel, configure the Message domain to be XMLNSC
    On the MQ Output node Basic properties panel, configure the Queue name to be DOTNET.EXCEPTIONS.OUT

    Wire the nodes as shown below and save the message flow: Type Ctrl-S or select File => Save. There will be an error against the .NETCompute node until you configure its assembly:

    Message Flow MyFlow in application App_DotNet_HotSwap
  7. Right-click the .NETCompute node and select Open Microsoft Visual Studio:

    Open Microsoft Visual Studio
  8. When Microsoft Visual Studio is launched, you will see the Start Page. Select New Project, as shown below:

    Microsoft Visual Studio Start Page
  9. The New Project wizard offers three types of project templates in the Message Broker category. Select Project to create a Message Broker message, specify the properties at the bottom of the window as follows, and Click OK:

    Name = HotSwapDotNetProject
    Location = C:\student\DOTNET\lab_exceptions\visual studio 2010\Projects
    Solution Name = HotSwapDotNetProject

    New Project wizard
  10. Edit the UserCode region of the class CreateNode.cs using the code in the listing below. This snippet of C# navigates to an input field, which is expected to contain an integer value. This value has 42 added to it, and is then placed into a field in the output message. Before examining the hot swap capability, we will demonstrate how the catch block of the code works by deliberately sending in some data that contains a string rather than an integer. The string causes a Broker exception of the type NBRecoverableException to be thrown. The logic in the catch branch then re-throws an NBUserException. When you throw this kind of exception in a message flow, the Broker inserts an entry into the Windows Event Log so that you can decide what information to include in the exception. Further context information can be added that would not be available had the original NBRecoverableException not been caught. For example, this scenario includes a serialised copy of the input message as an insert in the log entry. Copy the code below into the UserCode region of CreateNode.cs:

    Listing 14. Code extract for the UserCode region
    
    #region UserCode
    // Add user code in this region to create a new output message
    try 
    {
      NBElement inputXMLNSC = inputRoot[NBParsers.XMLNSC.ParserName];
      NBElement outputXMLNSC =
        outputRoot.CreateLastChildUsingNewParser(NBParsers.XMLNSC.ParserName);
      NBElement message = outputXMLNSC.CreateFirstChild("Message");
      NBElement field = message.CreateFirstChild("","Field");
      field.SetValue(inputXMLNSC["Message"]["Field"].GetInt32() + 42);
    } 
    catch (NBRecoverableException nbrecov)
    {
     byte[] serialisedInput = inputRoot[NBParsers.XMLNSC.ParserName].AsBitStream(0,1208);
     NBUserException nbuser = new NBUserException("User generated exception",
        7522,
        NBSeverity.Warning,
        nbrecov,
        Encoding.UTF8.GetString(serialisedInput));
     nbuser.MessageAsFirstInsert = true;
     throw nbuser;
    }
    #endregion UserCode
    

  11. Save CreateNode.cs by typing Ctrl-S and build the assembly from the right-click menu in the Solution Explorer:

    Build Solution
  12. If you have followed the default naming suggested, then the assembly will have been saved to the file:
    C:\student\DOTNET\lab_exceptions\visualstudio 2010\Projects\HotSwapDotNetProject\
    HotSwapDotNetProject\bin\Debug\HotSwapDotNetProject.dll
    

    . Return to the Message Broker Toolkit and open a Windows Explorer window in front of the message flow you constructed earlier. Drag and drop the assembly HotSwapDotNetProject.dll on top of the .NETCompute node:

    Drag and Drop HotSwapDotNetProject.dll
    This action will configure the Assembly name property of the .NETCompute node:

    Assembly name property of .NETCompute node
  13. Save the message flow: Type Ctrl-S or select File => Save.
  14. Deploy the application: Drag and drop App_DotNet_HotSwap onto the default execution group of the default broker configuration, as shown below:

    Deploy application App_DotNet_HotSwap
  15. Once the deployment completes, switch to Message Broker Explorer to send a test message into the flow. Right-click the input queue DOTNET.EXCEPTIONS.IN and click Put test message:

    Put test message
  16. In the resulting dialog, paste the simple input message in the listing below and click Put message:

    Listing 15. Example XML input message for the scenario
    
    <Message><Field>1</Field></Message>
    

    Put test message
  17. Open the Windows Event Log and you should see that several errors have been placed there by the Broker. A BIP2328 error message indicates that the .NETCompute node found A value of SQL datatype ''CHARACTER'' encountered when datatype ''INTEGER'' expected.. The input data was an XML message that was parsed by the Broker without the use of an XML schema, so all elements were interpreted as containing character data. The .NETCompute node then tried to use the GetInt32 method, causing the BIP2328 error because a character was encountered when an integer was expected. You will also find a BIP7522 error message in the Event Log due to the catch block throwing an NBUserException. The inserts in this message include a serialised copy of the input message:

    Windows Event Log
  18. Having demonstrated this deliberate exception, you will now see how to use the Broker to hot swap the .NET assembly, without the need to redeploy the message flow itself. Return to the Microsoft Visual Studio and edit the line of code in the UserCode region of CreateNode.cs that tries to do the numeric addition on the input field in the message. This line of code containing the mistake is shown in the listing below, followed by a line with the corrected code.

    Listing 16. Line of code in UserCode region of CreateNode.cs, with error line and corrected line
    field.SetValue(inputXMLNSC["Message"]["Field"].GetInt32() + 42);
    
    field.SetValue( ((int)inputXMLNSC["Message"]["Field"]) + 42);
    

  19. After fixing the code, save CreateNode.cs, and then click Rebuild Solution in the Solution Explorer:

    Rebuild solution
  20. Within a few seconds of rebuilding this code, Message Broker should detect that the assembly has been altered. Switch back to the Windows Event Log and you should find that a new entry for BIP7451. This informational message says that a hot swap has occurred and that the application domain has been reloaded:

    Windows Event Log hot swap entry
  21. Return to Message Broker Explorer and the Put Test Message dialog should still be open. Use the same input message as the previous test. Copy the message text from the listing below, paste it into the Message data box, and click Put message:

    Listing 17. Example XML input message for the scenario
    
    <Message><Field>1</Field></Message>
    

    Put test message
  22. This time, the corrected code should run successfully and a message should be written to the output queue DOTNET.EXCEPTIONS.OUT. Close the Put message dialog by clicking Close, and then click on the queue DOTNET.EXCEPTIONS.OUT. Press F5 to refresh the display, and you should see that the Current queue depth now has the value 1, as shown below:

    Current queue depth
  23. Right-click the queue and click Browse messages:

    Current queue depth
  24. The Message Browser should show that the output message carries a field containing the value 43, as shown below:

    Message browser

Congratulations -- you have completed Part 4 of this four-part tutorial series, which has demonstrated the range of the WebSphere Message Broker exception handling and hot swap features available with the .NETCompute node. You can continue on to the resource links and author information by clicking Next below, or you can return to Parts 1, 2, or 3 of the tutorial series.

8 of 10 | 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=795296
TutorialTitle=Using Microsoft .NET in WebSphere Message Broker V8: Part 4: Using the .NETCompute node for exception handling
publish-date=02222012
author1-email=bthomps@uk.ibm.com
author1-email-cc=
author2-email=mgk@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).