Sunday, July 31, 2016

Cron Expression

You have definitely heard of facial expression (which is nothing but one or more motions or positions of the muscles within the skin of the face) or  algebraic expressions (which is built up from integer constants, variables and the algebraic operation [addition, subtraction, multiplication, division and exponentiation by an exponent that is a rational number])

This is Cron expression, a string consist of six or even seven sub expressions (fields) that describe individual details of the schedule (can be a scheduling of batch jobs). Confusing??

Lets consider we want to schedule a job every day at 12:00 (noon). Your scheduler takes similar inputs and schedule a task on that time period;

                                                     cmd: execute xyz_job
                                                      Sch: 12:00 Mon-Sun

Cron expressions are also used to schedule jobs. It is most commonly used in Linux. The above schedule is also written as;
                                                       Sch: 0 0 12 * * ?

Cron Expression Format  (source: bobcravens.com)
The cron expression is a string with 5 space-separated ‘entries’ as shown graphically below:        
1
2
3
4
5
  • Entry #1 – Filters the minutes (0-59) that are allowed to generate a trigger.
  • Entry #2 – Filters the hours (0-23).
  • Entry #3 – Filters the days (1-31).
  • Entry #4 – Filters the months (1-12).
  • Entry #5 – Filters the days of the week (0-6, 0=Sunday).
Each entry can hold a string (no spaces allowed) of characters. Some examples of valid entries are the following:
  • “*” – Any valid value can cause a trigger. The validity of the values is checked to ensure that the resulting date / time exists. For example the day of the month is checked against the current month & year to ensure it is a valid day (including leap years).
  • “3” – Only a single digit (in this case a three) can cause a trigger.
  • “0,30” – A list of digits (in this case a zero and a thirty) can cause a trigger.
  • “1-10” – A range of digits (in this case the numbers 1 through 10).
  • “*/2” – Only even digits in the valid range.
  • “0-30/10” – Only 0,10,20,30 will cause triggers.
This provides quite a bit of flexibility when you combine all five entries to create a cron expression. For example:
  • “0 * * * *” – Triggered at the top of every hour.
  • “0 0 * * *” – Triggered at 12 AM every day.
  • “0 0 1 * *” – Triggered at 12 AM the 1st of every month.
  • “0 0 1 1 *” – Triggered at 12 AM the 1st of January of every year.
  • “0 0 1 1 0” – Triggered at 12 AM the 1st of January when it falls on a Sunday.
  • “*/30 * * * *” – Triggered at the top and bottom of the hour.
  • “0 0 */2 * *” – Triggered every other day at 12 AM.
  • “0 3 * * 6” – Triggered at 3 AM every Saturday.
As you can see these expressions are powerful and simple.

Cron Expressions Allowed Fields and Values (source: oracle)

NameRequiredAllowed ValuesAllowed Special Characters
SecondsY0-59, - * /
MinutesY0-59, - * /
HoursY0-23, - * /
Day of monthY1-31, - * ? / L W C
MonthY0-11 or JAN-DEC, - * /
Day of weekY1-7 or SUN-SAT, - * ? / L C #
YearNempty or 1970-2099, - * /


 Lets dig more to understand some symbols;

The ‘/’ character can be used to specify increments to values. For example, if you put ‘0/15’ in the Minutes field, it means ‘every 15th minute of the hour, starting at minute zero’. If you used ‘3/20’ in the Minutes field, it would mean ‘every 20th minute of the hour, starting at minute three’ - or in other words it is the same as specifying ‘3,23,43’ in the Minutes field. Note the subtlety that “/35” does *not mean “every 35 minutes” - it mean “every 35th minute of the hour, starting at minute zero” - or in other words the same as specifying ‘0,35’.

The ‘?’ character is allowed for the day-of-month and day-of-week fields. It is used to specify “no specific value”. This is useful when you need to specify something in one of the two fields, but not the other.

The ‘L’ character is allowed for the day-of-month and day-of-week fields. This character is short-hand for “last”, but it has different meaning in each of the two fields. For example, the value “L” in the day-of-month field means “the last day of the month” - day 31 for January, day 28 for February on non-leap years. If used in the day-of-week field by itself, it simply means “7” or “SAT”. But if used in the day-of-week field after another value, it means “the last xxx day of the month” - for example “6L” or “FRIL” both mean “the last friday of the month”. You can also specify an offset from the last day of the month, such as “L-3” which would mean the third-to-last day of the calendar month. When using the ‘L’ option, it is important not to specify lists, or ranges of values, as you’ll get confusing/unexpected results.

The ‘W’ is used to specify the weekday (Monday-Friday) nearest the given day. As an example, if you were to specify “15W” as the value for the day-of-month field, the meaning is: “the nearest weekday to the 15th of the month”.

The ‘#’ is used to specify “the nth” XXX weekday of the month. For example, the value of “6#3” or “FRI#3” in the day-of-week field means “the third Friday of the month”

Hope you understand the basic concept and how to write corn expression.


Note: The content of this blog is taken from various authors who extensively worked on Cron expressions. I have collected them and made this page to learn and understand the concept easily.

Sunday, April 3, 2016

Use Of Interface with Example...


Interface is very important concept in C#. The usual answer we get for Why to use Interface is - "To achieve Multiple Inheritance which is not directly supported by C#." This seems to me partially true because we don't have method definition in interface unlike in  class implementing multiple inheritance. Also we can have properties but we do not have variable declaration in interface.

Again the same question then: What is the use of interface?

Microsoft says there are several other reasons why one should use interfaces instead of class inheritance:
  • Interfaces are better suited to situations in which your applications require many possibly unrelated object types to provide certain functionality.
  • Interfaces are more flexible than base classes because you can define a single implementation that can implement multiple interfaces.
  • Interfaces are better in situations in which you do not have to inherit implementation from a base class.
  • Interfaces are useful when you cannot use class inheritance. For example, structures cannot inherit from classes, but they can implement interfaces.
Lets try to understand this with below example:

 



You got something!! Consider a scenario of any bank where you want to check name of the customers associated with the bank.  Bank will not give you all the details like one's account number or account balance. Practically with an object of each account (class in this e.g.) user can access anything which bank certainly don't want to but just account name is something which is not confidential and associated with each account. 


See how we are displaying the customer name - we are adding the object (Account Key) of each customer to array list so that we can iterate through their respective class methods and then passing it to the DisplayCustomers() method. Now we are referring each customer object to the Interface object in foreach. WHY?? WHY?? WHY?? Why the bloody hell we are not directly accessing through respective Class object now?? Because accessing interface provides an upper level security to the class methods. It will only allow us to access myaccountName() method which is declared inside ISbiAccountName intreface and implemented by multiple classes.





 

Output:



There is one more concept called "Implement interface explicitly". 

Consider two person having same name leaving together. How you gonna call them. I guess with some identity - may be adding father's name which is unique. Now, below example it self is explanatory - 



 Comment down below if you have any queries. Happy Learning :)

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.