Thursday, December 18, 2008

Getting Started with Mirth (Part 1)

December 18, 2008 Posted by Jason Irwin 7 comments

This is the first post in a 2 part series. The second post is here

Recently I posted about my positive experience when using Mirth by Webreach as an open source communication layer between one of our in-house applications and a third party and proprietary web service interface. That post received a large amount of attention (relative to my other posts :o) ) so now I will post a quickstart guide to creating a similar setup.

In this post I will go over the very basics of using Mirth (please dig in and play around with some more advanced options/configurations), specifically I will demonstrate how to use Mirth to read data from a database, package the data up in a SOAP envelope, send the data to a web service and process the SOAP response. I'll also show you how easy it is to monitor channels once deployed.

Once you've installed and opened Mirth for the first time you'll be presented with an empty dashboard - but it wont stay empty for long. This is where you'll see the status of your live channels, so let's build one! Part 1 of this post will deal with the channel source. Part 2 will deal with the destination.


Clicking on the 'Channels' link on the LHS opens a blank pane. Right-click anywhere in the blank pane and choose 'New Channel'.


The screen that opens will present you with 4 tabs: summary, source, destination, scripts. First thing's first - enter some summary data. I named my channel ("Jason's Sender Channel"), changed the 'Incoming Data' type to XML (most other options are specific to medical information systems), added a description and chose to encrypt stored messages and prune them from the database after 5 days. That's all I need from the summary tab for right now.


The source tab is where you'll specify where your data is coming from. There are plenty of options here as you can see from the (partially) displayed dropdownlist in the screenshot below. For my purposes I'll need a database reader.


For a SQL Server database I choose SQL Server/Sybase as the Driver. The URL is of the format jdbc:jtds:sqlserver://<host>/<database>. In my case it goes as follows:


For demonstrative purposes i'll set the polling type to interval and set it to 10000 ms. Otherwise I could set it to Time and have the channel run at a given time every day. There are two choices for how the query itself will be written: Javascript or SQL. I'll stick with SQL in this post. I've created a simple table with 3 fields in my demo database. The table creation script is as follows:

  1: USE [demodb]
  2: GO
  4: /****** Object:  Table [dbo].[mytable]    Script Date: 12/18/2008 22:49:20 ******/
  6: GO
  9: GO
 12: GO
 14: CREATE TABLE [dbo].[mytable](
 15:  [id] [int] IDENTITY(1,1) NOT NULL,
 16:  [name] [varchar](30) NOT NULL,
 17:  [isactive] [bit] NOT NULL,
 18:  [created_date] [datetime] NOT NULL
 19: ) ON [PRIMARY]
 21: GO
 24: GO

Once completed, my source looks as follows:


Here's where things get cool...I want to specify what data gets passed to my destination and how I want that data to be formatted. The easiest way to do this is to use a transformer (on the menu on the LHS) on the source.

First thing is first: update your message template. In this case the inbound message template will be automatically created based on the database fields we are querying. I want to specify an outbound template that will be passed to the channel's destination. See the textbox outlined in red below:


To summarize, I changed the names of the field (my query prefixed them with the table name) and instead of using a single 'name' field, I created a 'first_name' and 'last_name' field.

Now that the message templates are ready there are numerous ways to map from the inbound message to the outbound message - all are performed in the message tree tab. The easiest by far is to drag a value from the inbound message template tree to the outbound message template tree. In the below screenshot I have dragged the value node under 'mytable_id' in the inbound tree onto the value node under 'id' in the outbound tree resulting in step #0 displayed in the middle section of the window. I will do this for the mytable_isactive and mytable_created_date fields.


To map the mytable_name to first_name and last_name we'll have to be a little trickier. Right-click the white space in the middle pane and click 'Add New Step'.


Name this new step and set its type to Javascript. Obviously you can then use the power of the javascript (and java too) language to perform the desired functionality. (TIP: Feel free to drag items from the message templates into the javascript editor window. This will provide you with the correct object names when assigning etc.).

I have added the following Javascript to split the string and assign the respective values to the first_name and last_name output fields

  1: combinedNameString = msg['mytable_name'].toString()
  2: arrName = combinedNameString.split(" ")
  3: tmp['first_name'] = arrName[0]
  4: tmp['last_name'] = arrName[1]

Our finished transformer will look as follows:


In the next couple of days i'll post part 2 which will demonstrate using the channel destination to call a SOAP method, passing this info wrapped up in a SOAP envelope. Thanks for reading..