Using a .NETCompute node to handle a WebSphere Message Broker exception
- Amend the message flow
MyFlowthat you have been working with in the previous sections of the tutorial to include a .NETCompute node after the Trace node by dragging it from the Transformation drawer of the palette . The .NETCompute node API provides some methods that can interpret Message Broker exceptions, so that if you want to compose an exception for your own purposes, you can easily do so. Wire the Out terminal of the .NETCompute node to the In terminal of an MQ Output node by dragging it from the WebSphere MQ drawer of the palette. Rename the MQ Output node
DOTNET.EXCEPTIONS.CATCHand set its Queue name property to the same value:
- Save the message flow, which should now look like this:
Double-click the .NETCompute node, or right-click on it and select Open Microsoft Visual Studio.
- When Microsoft Visual Studio is launched, you will see the Start Page. Select New Project, as shown below:
C:\student\DOTNET\lab_exceptions\visual studio 2010\Projects
Solution Name =
The New Project wizard provides 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 New Project wizard, as listed beneath the image below, and Click OK:
- After the project is created, expand the Solution Explorer view. You should see that the CreateNode.cs file has been created to contain the C# class, and a reference to IBM.Broker.Plugin has been added.
This assembly contains the helper methods from Message Broker to transform messages in a .NETCompute node:
- Locate the UserCode region of the CreateNode.cs file:
- Add the C# code in the listing below to the UserCode region:
Listing 3. UserCode region demonstrating method CreateExceptionFromExceptionList:
#region UserCode // Add user code in this region to create a new output message NBMessage inboundExceptionList = inputAssembly.ExceptionList; NBException exc = CreateExceptionFromExceptionList(inboundExceptionList); String pleasantExceptionMessage = exc.FormattedMessage(false); NBElement outputXMLNSC = outputRoot.CreateLastChildUsingNewParser(NBParsers.XMLNSC.ParserName); NBElement field = outputXMLNSC.CreateFirstChild("Message").CreateFirstChild("Field"); field.CreateFirstChild(NBParsers.XMLNSC.AsIsElementContent, "", pleasantExceptionMessage); #endregion UserCode
The above code begins by navigating to the ExceptionList structure, which you have already examined using the Trace node, and which is passed into the .NETCompute node as part of the Input Message Assembly (logical tree hierarchy). The method
CreateExceptionFromExceptionListconverts the hierarchical ExceptionList tree structure into an NBException object, which provides a method named
FormattedMessage, which re-inflates the object into a readable exception in which all message inserts have been reinserted. In the example, this nicely formatted message is then copied into the output message structure by the .NETCompute node.
- With the Visual Studio development work complete, the next step is to build an assembly file that you will reference from the .NETCompute node. From Solution Explorer, right click on the Solution and select Build Solution:
- The Output window will show 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 visible -- if it is not, follow the instructions in the sidebar.
If you have used the default naming suggested throughout the tutorial, the assembly file should have been saved at:
C:\student\DOTNET\lab_exceptions\visual studio 2010\Projects\ExceptionsDotNetProject\ ExceptionsDotNetProject\bin\Debug\ExceptionsDotNetProject.dll
Copy this location, as you will use it to configure the .NETCompute node in the next step.
- Return to WebSphere Message Broker Toolkit and configure the Assembly Name property of the .NETCompute node by pasting it from the previous instruction:
- Save the message flow: Type Ctrl-S or select File => Save.
- Return to the open test client file
App_DotNet_Exceptions.mbtest, right click the Invoke Message Flow level in the hierarchy on the panel on the left, and select Re-run. The flow should be redeployed and the same message will be sent to the input queue again. This time, you should see an output message appear on the queue named
- Examine the output message and note in particular that the exception text should be nicely formatted in the Field element of the output XML message, which should look like this:
Listing 4. Output message formatted using .NETCompute node method FormattedMessage
<Message> <Field>( MB8BROKER.default ) Exception thrown by throw node 'DotNetExceptions.Throw'; text is 'This is a deliberate exception!'. The throw node 'DotNetExceptions.Throw' has received a message and thus has thrown an exception as this is its normal behavior. The message text associated with this exception is 'This is a deliberate exception!'. Since this is application-generated (by message flow behavior), the user action is determined by the message flow and the type of exception generated.</Field></Message>