Sunday, March 29, 2009

Enabling OPC information exchange using service bus (nServiceBus) part 2

In the previous post I created a simple message publisher to publish OPC tag information onto the nServiceBus service bus. In this post I’ll create a client that subscribes to the message and retrieves the message from the bus. The application is close to a copy of the publish/subscribe sample provide with the nServiceBus documentaion. Again the app.config can be used unchanged.

The first task is to amend the EventMessageHandler to use the event message class defined in the previous post.

using Messages;
using NServiceBus;
using System;

namespace Subscriber1
{
public class EventMessageHandler : IMessageHandler<EventMessage>
{
public void Handle(EventMessage message)
{
Console.WriteLine("Subscriber 1 received EventMessage with Id {0}.", message.EventId);
Console.WriteLine("Message time: {0}.", message.Time);
Console.WriteLine("Message duration: {0}.", message.Duration);
Console.WriteLine("Message description: {0}.", message.Description);
}
}
}


As you can see very straight forward.



The rest of the Subscriber1 can be used unchanged.



using System;
using Common.Logging;
using Messages;
using NServiceBus;

namespace Subscriber1
{
class Program
{
static void Main()
{
LogManager.GetLogger("hello").Debug("Started.");

var bus = NServiceBus.Configure.With()
.SpringBuilder()
.XmlSerializer()
.MsmqTransport()
.IsTransactional(false)
.PurgeOnStartup(false)
.UnicastBus()
.ImpersonateSender(false)
.SetMessageHandlersFromAssembliesInOrder(
typeof(EventMessageHandler).Assembly
)
.CreateBus()
.Start();

Console.WriteLine("Listening for events. To exit, press 'q' and then 'Enter'.");
while (Console.ReadLine().ToLower() != "q")
{
}
}
}
}



The implementation of second subscriber is similar. When running the program it shows the following result screen.



OPC and PubSub ESB output



You see the OpcEventMessageDispatcher publishes a message onto the bus which is read by both the subscribing clients.

No comments:

Post a Comment