In the first part of this blog series, Creating WCF Services using the .NET JMS Adapter–Part 1, a web service that exposed a simple API for publishing JMS messages was built. This outbound operation is a request/response messaging pattern, created by using the adapter to generate a WCF binding for a custom transport channel. In this blog, a WCF Channel Listener service is built to consume the message published by the web service deployed in Part 1. The channel listener is essentially equivalent to an Enterprise Java Message Driven Bean leveraging a JMS Message Listener. Unlike the outbound operation, the inbound listener represents a handler called by the adapter whenever a message is available for consumption from the JMS destination. The handler processes the message, perhaps updating a DB, for example.
A WCF Channel Listener can be hosted in a WCF Service Host process or IIS. This blog explorers deploying the listener to IIS. For an introduction to the .NET JMS Adapter, please see this tutorial on building simple messaging clients.
Building this example requires the JNBridge JMS Adapter for .NET and Visual Studio version 2010, 2012 or 2013. Deployment requires IIS 7.5, or later, and AppFabric for Windows Server. As before in Part 1, ActiveMQ will be the JMS implementation.
Creating the Inbound Binding
In Visual Studio create a new WCF Service Application named WcfJmsListener. Once again, Visual Studio will produce a bunch of default code and names, so the project will have to be cleaned up. Delete the files IService.cs and Service1.svc.cs. Rename the file Service1.svc to WcfJmsListenerService.svc.
Open the Add Adapter Service Reference design tool from the Project menu. Select the binding JNBridgeDotNetJMSAdapter and open the Configure Adapter dialog. Choose ActiveMQ as the JMS implementation. If the adapter has been used in Part 1, then all the correct property values are already loaded into the dialog. There are two modifications required to the configuration properties. Again, because generic operations will used, the property, Work Off Line, is set to true. The property, Queue Name, is set to the name of the JMS destination.
Once the configuration is correct, the Connect button is clicked, allowing selection of operations. Since the operation type is inbound, use the drop down control Select contract type to choose Service (inbound operations). The selected inbound operation is OnReceiveAnnotatedTextFromQueue(). At the bottom of the dialog the Filename prefix field has a custom name entered, WcfJmsListener. The completed dialog is shown below.
When OK is clicked, the adapter supplies WSDL to the WCF ServiceModel metadata utility which generates a couple of source files and updates Web.config. The two files, WcfJmsListenerInterface.cs and WcfJmsListenerService.cs, represent the channel listener interface and implementation, respectively.
Configuring the Channel Listener
As was necessary in Part 1, the service definition file, WcfJmsListenerService.svc, and the IIS service configuration file, Web.config, will have to be modified.
This file is modified replacing the original placeholder defaults with the full service name and implementation file.
Only one modification is needed here. Toggle the property, Offline, to false.
Completing the Listener Implementation
The channel listener implementation has a single method stub, OnReceiveAnnotatedTextFromQueue(). For the purposes of this blog, simply writing the received message to a file will suffice. As a convenience, the System.Diagnostic tracing utility is used.
In order to use the .NET tracing utility, this XML must be added to the configuration file, Web.config.
Publishing the Listener to IIS
The channel listener is published to IIS from Visual Studio using the following profile.
Configuring AppFabric for Windows Server
AppFabric for Windows Server provides an execution container for the WCF channel listener service. This is necessary because there is no client involved to invoke the service as there was in Part 1, therefore the application and its service must be configured for Auto-Start. Enabling Auto-Start is done in two steps. First, the application, WcfJmsListener, must have its WCF services enabled for Auto-Start. The screen-shot montage, below, shows enabling Auto-Start as a configuration option for each individual WCF service in the application.
The next step is to enable Auto-Start for the specific service, WcfJmsListenerService. This is done by clicking on the Services icon in the AppFabric section in the Features View (see the Default Web Site pane in the above montage). The following screen shot montage shows enabling Auto-Start for the individual service.
Sending and Receiving a JMS Message
Using the web service, WcfJmsSendText, and the console application client built in Part 1, a JMS message is sent to the JMS queue. The WCF channel listener service, WcfJmsListenerService, will consume the message from the queue and call the service handler, OnReceiveAnnotatedTextFromQueue(). Here’s the trace file output of the received message..
While the JNBridge JMS Adapter for .NET provides a messaging API for JMS that can be called from C#, it also is a custom WCF transport channel that can produce WCF client and listener channel stack bindings. In Part 1 of this blog, an outbound client service was created that, when deployed to IIS, provided a web service for publishing messages to a JMS queue. In Part 2, a channel listener was created to consume messages. When deployed to IIS, the listener consumed messages published by a web service client.
The source is available here.