Monday, March 14, 2016

MSMQ - Microsoft Message Queuing

Here I tried to cover below details on MSMQ:

1. WHAT IS MSMQ?
2. WHY WE NEED MSMQ?
2.1. PURPOSE
2.2. WHERE APPLICABLE
2.3. DEVELOPER AUDIENCE
3. MSMQ REMOVES MACHINE DEPENDENCY.
4. HOW TO INSTALL MSMQ ON WINDOWS 7 OR VISTA?
5. UNDERSTANDING MSMQ THROUGH SAMPLE PROGRAM
6. SYSTEM.MESSAGING (MSMQ) VS WCF QUEUING
7. PROBLEM WITH MSMQ IN REAL LIFE:
8. REFERENCES


1.       What is MSMQ?

MSMQ is essentially a messaging protocol that allows applications running on separate servers/processes to communicate in a failsafe manner. A queue is a temporary storage location from which messages can be sent and received reliably, as and when conditions permit.

2.       Why we need MSMQ?

Message Queuing (MSMQ) technology enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline.  Applications send messages to queues and read messages from queues

2.1.      Purpose

Let’s understand this by following example. Suppose there are two applications;
Application 1: Agent application - Create policy and sends policy ID to Examiner for review.
Application 2: Examiner application review and process the policy Id.

Approach 1: Agent sends the policy id. Examiner process the request once received. 



Examiner may take time to review the policy; meanwhile agent can submit multiple requests asynchronously.

Challenges:
i.                     System must be capable of handling asynchronous request.
ii.                   What if both the applications are made up of different technologies. They may not talk with each other directly.

These problems can be overcome by making application 2 as a web service.
·         From Application 1 we can invoke the functions in web services asynchronously.

Major Challenge though:
Ø  What if application 2 is temporarily down or examiner is not present at that time?
·         Application 1 will get an exception. -  No Offline Capability.

Approach 2: Intermediate warehouse will resolve above issues. 



Agent and Examiner will not communicate directly. Agent submits the policy request to warehouse. If examiner started working will pick one by one request from warehouse and process it. This intermediate warehouse can be anything like database, excel file, notepad file etc.

Small challenge:
Ø  Automatic prioritization.
It might be the case that Umbrella policies have high priority to be reviewed due to higher premium values then normal policies. Manually, we can append some kind of flag or integer priority value to every policy id before sending it to intermediate warehouse (bit complex to validate policies). Examiner will pick the high priority request/policy to process.

Ø  Warehouse Maintenance
We have to also consider the limitation of warehouse, how to remove the processed request, the correct format for dynamic request, managing priority index etc. 

Approach 3: Using MSMQ to overcome all above problems. 



MS Message Queuing is a windows component having enhanced capabilities to handle asynchronous request. It also provides capabilities to prioritize the request and handle its queue automatically.

2.2.       Where Applicable

Message Queuing provides guaranteed message delivery, efficient routing, security, and priority-based messaging. It can be used to implement solutions to both asynchronous and synchronous scenarios requiring high performance. The following list shows several places where Message Queuing can be used.
·         Mission-critical financial services: for example, electronic commerce.

·         Embedded and hand-held applications: for example, underlying communications to and from embedded devices that route baggage through airports by means of an automatic baggage system.

·         Outside sales: for example, sales automation applications for traveling sales representatives.

·         Workflow: Message queuing makes it easy to create a workflow that updates each system. A typical design pattern is to implement an agent to interact with each system. Using workflow-agent architecture also minimizes the impact of changes in one system on the other systems. With Message Queuing, the loose coupling between systems makes upgrading individual systems simpler.

2.3.      Developer Audience

Message Queuing applications can be developed using C++ APIs or COM objects. Applications can be built in any of the popular development environments: for example, Microsoft® Visual Basic®, Visual Basic® Scripting Edition, Visual C++®, Visual Studio® .NET, Borland Delphi, and Power soft PowerBuilder. This allows applications to be developed for the Internet as well, including both server-side (Internet Information Server) and client-side (Internet Explorer) applications. The .NET Framework offers a set of managed Message Queuing objects.

3.       MSMQ removes Machine Dependency.

By replacing the direct queue with Message queue client can directly send messages to server even when the server goes down and server can continuously send response even when client is unavailable. When any one of the two goes down messages simple resides in queue and wait to get picked up. These messages eventually get processed when the system come back online and start pulling the messages from queue. So it removes the Machine Dependency but what happen if the machine hosting the message went down or network went down?

In this case, the message wouldn’t even be able to reach to Message queue.
            MSMQ has a very useful feature called as outgoing queue. This feature allows the system sending a message to store the message on local queue if it’s unable to send the message out due to network issues or the remote location being offline. The MSMQ service will try to resend these messages at a later time.

4.       How to install MSMQ on windows 7 or Vista?

1.       Open Control Panel.
2.       Click Programs and then, under Programs and Features, click Turn Windows Features on and off.
3.       Expand Microsoft Message Queue (MSMQ) Server, expand Microsoft Message Queue (MSMQ) Server Core, and then select the check boxes for the following Message Queuing features to install:
o    MSMQ Active Directory Domain Services Integration (for computers joined to a Domain).
o    MSMQ HTTP Support.
4.       Click OK.
5.       If you are prompted to restart the computer, click OK to complete the installation.
     
       Note: Click here to see installation of MSMQ 4.0 on windows server 2003, 2008 or 2008 R2.

5.       Understanding MSMQ through Sample Program


It requires System.Messaging namespace provides classes that allow you to connect to, monitor, and administer message queues on the network and send, receive, or peek messages.  Please refer this link to see its class description.

Sender – Application 1 – Agent App. – Sends policy Id to examiner for review. UM policies have higher priority.




Messages are queued in MSMQ service under computer Management



Receiver – Application 2 – Examiner – Process the UM policies on priority others on FIFO basis. Priority can be one of the below:






6.            System.messaging (msmq) vs wcf queuing

Source: Motley queue website – from MSDN library

System.Messaging (SM)
WCF Queuing
Why is this interesting?
At its core, is a wrapper around MSMQ’s APIs.
At its core, is a wrapper around MSMQ’s APIs.
Both technologies wrap MSMQ and benefits robust, stable, and time tested reliable messaging platform no matter which way you go.
Enables an object-oriented view of your application’s asynchronous communication plumbing.
Enables an object oriented view of your application’s business processes.
The WCF programming model is centered on your business operations.  With it, you can stop managing your message transactions, setting message properties, searching for messages in a queue, checking for NACKS, retrying messages, etc.  Instead you can start thinking of the world in terms of business operations (e.g. CreatePurchaseOrder) and focus on your business logic.  Leave the tedium to WCF.
Enables you to write your own end-to-end message encryption/authentication.
Provides end-to-end message encryption/authentication for free.
SM encrypts messages while they’re on the wire and will enforce a queue’s ACL, which is great.  If you want your messages encrypted on disk, however, or any authentication besides who is allowed to send to/receive from a queue, you need to write that yourself.  With WCF Queuing however, you can easily enable message level security, ensuring all data is encrypted, even when in MSMQ’s store or traverses multiple hops.  You can also decorate your business operations to declare which user groups/roles are allowed to invoke them.
Your only hosting option is to write your own service/executable.
Host your service in a custom service/executable or let IIS host it for you.
Need your application available all the time but still want to utilize resources efficiently?  Want your app to recover when it encounters a critical failure?  Host your service in IIS via Web Hosting and it will get activated when messages arrive in your queue and will be recycled if it encounters a failure (configurable of course).
Enables you to write your own poison message handling (but only if you’re down with PInvoking the MSMQ C API).
Provides poison message handling for free.
Ever had a message in stuck in a queue that you couldn’t process and had to build some complex retry/reject logic in your app to accommodate it?  Or, worse yet, do you search for messages to receive from a queue to avoid this problem altogether?  If so, WCF Queuing supports poison message handling out of the box — a huge win in the fight for robust distributed applications and against using a transport mechanism as a data store.
Enables you to write your own message correlation logic.
Provides message correlation for free.
So you’ve got a group of messages you want processed together or by the same receiver?  With SM, you’ve got access to a message’s correlation ID but of course it’s up to you to do the correlating.  With WCF Queuing, you can group your messages in sessions and WCF will ensure those messages are processed together.
Enables you to write your own transactional batching logic.
Provides transactional batching for free.
Want performance and transactional reads?  Batch your reads together in large transactions and only abort in the relative few instances you need to.  Of course you’ll need to write that logic yourself as well as the retry mechanism and make it configurable for tuning purposes if you use SM.  WCF on the other hand supports transactional batching and its configuration out of the box.
Can create, delete, and generally manage MSMQ queues.
Cannot create, delete or manage queues.
*WCF Queuing can’t do everything SM can do; you still want to use it to create and manage your queues.  So arguably you want to use WCF Queuing with SM, as opposed to instead of, but for the actual transmission of data, WCF is clearly the way to go.

7.       Problem with MSMQ in real life:

Microsoft Message Queuing (MSMQ) is powerful solution for asynchronous applications. However, things don't look so good when MSMQ must be managed - supplied MSMQ management console (MMC) does not cover many real life situations.
  •         I.            Poison Messages
  •       II.            Queued Components Messages
  •     III.            Backup and restore MSMQ messages
  •     IV.            Need to manage remote queues. – can’t be done by MMC.

8.       References


Please refer the msdn link for details on MSMQ. 

Nested try catch finally...

Google around and no one can say "NO" to this. Yes definitely we can have nested try catch finally. This is used in real time when we are not sure if finally will throw some exceptions. Below example is clearly illustrating the use of nested blocks.

Note: Below code is in C#

Sunday, March 13, 2016

Abstraction And Information Hiding

One day I will have my own Mobile OS Company :)

An application for mobile manufacturing company - Every phone must have to implement caller
and sms feature but it depends on company to company (or model to model) if they want to include other features or not which are readily available , you just have to use it without knowing its implementation (like facebook in this example).

Below Code in C#.

Why AngularJs ?

With changing pace of requirements from end user the technology is also changing with leaps and bound speed.The entry of AngularJs has added beauty over many scripting languages. There are lot of advantages Angular has provided free to the developers and lot of links are available to take a deep dive in Angular. What I like the most about Angular is - "Its simplicity". Let's understand this by a simple example;
Suppose we want to print the text on screen as soon as the user type the text in TextBox. To achieve this, I can think of following three terminology in scripting languages:
1 So sweet -  JavaScript   
2 Twister - JQuery 
3.The Hero - AngularJs.

Let's start with implementing the requirement using JavaScript.

Don't you think this is simple and sweet. A single liner statement inside function works out. But here we have a small problem. What if the requirement suddenly changes and client want us to print the text inside textbox instead of H2. What if the effects on DOM are not defined. Then we can probably think of JQuery.

Introducing JQuery...










Wow..here comes the twister. Don't you think the code is very intelligent. Using dollars and selectors it looks very close to the machine programming....lol. Well jokes apart, though the jquery code sometimes looks complex but has very enhanced features provided to the developers. There is nothing which we cannot do with jquery what we can with JavaScript; even better. But still we have the same dependency of script with DOM. Consider the above situation where we have to print the text in multiple DOM elements say in TextBox. You have to call the HERO- ANGULARJS. Another  thing I most like about Angular as a developer perspective is automatically Two - way binding and MV* pattern. Developers  please check out the advantages and features of Angular and you fall in love with it for sure.

The stunning Entry of our Hero - AngularJs...














 I mean look at the simplicity. I don't have to care about HTML. whatever you want just bind with the model name and the output is what you desire.

Guys give a try...I am sure you will start using Angular. :)

Please let me know if you have any queries on this.