Thursday 27 October 2016

Apache ActiveMQ with .NET Applications using Apache.NMS.ActiveMQ

The purpose of this repository is to help run a simple tutorial on how to integrate .NET applications with ApacheMQ using the NMS library.
If you'd like to follow the tutorial, please head to the wiki page & read the "Tutorial". Be sure to read the other pages before you commence the actual tutorial so that you understand important concepts.

Overview

Message Queues enable 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. The following illustration shows how a queue can hold messages that are generated by multiple sending applications and read by multiple receiving applications.
Message Oriented Middleware
Apache ActiveMQ ™ is one the most popular and powerful open source messaging and Integration Patterns server. Based on JMS, it allows an asynchronous communication mechanism in the distributed system field. It is fast, supports many Cross Language Clients and Protocols, comes with easy to use Enterprise Integration Patterns and many advanced features while fully supporting JMS 1.1 and J2EE 1.4. Also, it is released under the Apache 2.0 License.
ActiveMQ has a .NET Messaging API called NMS which is easily accessible via the nuget feed. The NMS API This allows you to build .NET applications in C#, VB, or any other .NET language, using a single API to connect to multiple different providers using a JMS style API.
The purpose of this repository is to help run a simple tutorial on how to integrate .NET applications with ApacheMQ using the NMS library.

Why use message queues

Message queues today form a vital component of most enterprise scale applications.
Here are some of the top reasons for using message queues. A more detailed explanation is available at the following link.
  • Decoupling
  • Redundancy
  • Scalability
  • Elasticity & Spikability
  • Resiliency
  • Delivery Guarantees
  • Asynchronous Communication
Before we begin, its important to understand a few basic concepts.

Basic Concepts

Characteristics of messaging services

  • Messages can be consumed sync or async
  • Consumers can filter which message they receive
  • Messages are placed in Destination in sent order
  • Message consumption order cannot be guaranteed

Message Models

  • Point-to-Point or P2P (Queuing Model)
  • Publisher-Subscriber or Pub-Sub (eg: Topic)

Queue

  • A JMS Queue implements load balancer semantics. A single message will be received by exactly one consumer.
  • If there are no consumers available at the time the message is sent it will be kept until a consumer is available that can process the message.
  • If a consumer receives a message and does not acknowledge it before closing then the message will be redelivered to another consumer. A queue can have many consumers with messages load balanced across the available consumers.
  • So Queues implement a reliable load balancer in JMS.
  • Can be thought of as a staging area that contains messages that have been sent and are waiting to be read.
  • Note that, contrary to what the name queue suggests, messages don't have to be delivered in the order sent. If the message driven bean pool contains more than one instance then messages can be processed concurrently and thus it is possible that a later message is processed sooner than an earlier one.
  • A JMS queue only guarantees that each message is processed only once.

Topic

  • A distribution mechanism for publishing messages that are delivered to multiple subscribers.
  • A good analogy for this is an anonymous bulletin board.
  • In JMS a Topic implements publish and subscribe semantics.
    • When you publish a message it goes to all the subscribers who are interested - so zero to many subscribers will receive a copy of the message.
    • Only subscribers who had an active subscription at the time the broker receives the message will get a copy of the message.

What we're trying to develop for the tutorial

For the purpose of this tutorial you will be developing a simple chat client application as shown in the image below.
Application Overview
As shown in the diagram above, you will be developing a .NET solution (Tutorial.sln) with the following projects:
  • Tutorial.Chat.Ui
    • This project is a simple windows forms project which will server as the UI for the appliction.
    • The UI will accept uer inputs & send/enqueue them to a Queue ("App.Message.Processing.Queue") for being processed.
    • The UI will also be listening/subscrbing to a Topic ("App.Message.Chat.Topic") for processed messages.
  • Tutorial.ProfanityFilter.Svc
    • This project is a simple windows service applicaion which will be responsible for accepting messages from the UI and running them through a simple profanity filter.
    • The service will listen/dequeue messages from the aforementioned Queue ("App.Message.Processing.Queue") for processing messages.
    • The service will then send/publish processed messages to the aforementioned Topic ("App.Message.Chat.Topic") for being consumed by the UI.

Before you begin

Before you begin, you will need to install some prerequisites to get this working.
  • You will need JDK (You can google the relevant help doco).
    • Make sure post installation the JDK bin is added to your path.
    • You will also need to setup the "JAVA_HOME" path. I'm on a windows machine so mine is set to "C:\Program Files\Java\jdk1.8.0_91"
  • You will need to download the latest release of Active MQ available here.
  • To help you with the setup of ActiveMQ, you can lookup the help doco at http://activemq.apache.org/getting-started.html.

Start ActiveMQ

  1. You will need to navigate to the appropriate folder within the "bin" directory & run the activemq batch/script. In my case, its "apache-activemq-5.14.1-bin\apache-activemq-5.14.1\bin\win64\activemq.bat"
  2. On startup, the console should give you the different ports where activemq is listening. In my case it was:
    jvm 1 | INFO | Apache ActiveMQ 5.14.1 (localhost, ID:localhost-56683-1476933442897-0:1) is starting
    jvm 1 | INFO | Listening for connections at: tcp://localhost:61616?maximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1 | INFO | Connector openwire started
    jvm 1 | INFO | Listening for connections at: amqp://localhost:5672?maximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1 | INFO | Connector amqp started
    jvm 1 | INFO | Listening for connections at: stomp://localhost:61613?maximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1 | INFO | Connector stomp started
    jvm 1 | INFO | Listening for connections at: mqtt://localhost:1883?maximumConnections=1000&wireFormat.maxFrameSize=104857600
    jvm 1 | INFO | Connector mqtt started
    Additionally, the web console should be available at port 8161 based on the following log line:
    jvm 1 | INFO | ActiveMQ WebConsole available at http://localhost:8161/
    To test your installation, you can:
    • open the above link in a browser
    • Click "Manage ActiveMQ broker"
    • Enter the default username & password which is admin
    • Play around with the UI. This will help you better understand how to administer the message broker. Message Oriented Middleware

Tutorial Steps

The tutorial steps have been listed on the wiki page available at https://github.com/rohit-lakhanpal/apache-activemq-nms-tutorial/wiki/Tutorial

Tuesday 19 April 2016

DEV MONTHLY #1: Coding Design Patterns

Coding Design Patterns



One of the most important aspects of writing maintainable code is being able to notice the recurring themes in that code and optimize them. This is an area where knowledge of design patterns can prove invaluable.

What is a Pattern you ask?


A pattern is a reusable solution that can be applied to commonly occurring problems in software design. The best part is, you might be using them without even knowing it!

Why would you use a well-known pattern?


  • Patterns are proven solutions and provide solid approaches to solving issues in software development 
  • Patterns can be easily reused as they can be adapted to suit our own needs
  • Patterns can be expressive as there’s generally a set structure and vocabulary to the solution



Patterns you must know (Language Agnostic)


Below, I am going to highlight some patterns that I love using & I think any good developer must know, but this in no way is a complete list. Be sure to share your fav patterns with others.

Note: I won’t be covering the MVC & MVVM patterns as they’re well documented at various different places.

Factory Pattern

Provides an interface for creating families of related or dependent objects without specifying their concrete classes.

For example lets discuss a scenario where the primary actors are:

  • Client
  • Factory
  • Product




The client is an object that requires an instance of another object (the product) for some purpose. Rather than creating the product instance directly, the client delegates this responsibility to the factory. Once invoked, the factory creates a new instance of the product, passing it back to the client. Put simply, the client uses the factory to create an instance of the product.

The factory completely abstracts the creation and initialization of the product from the client. This indirection enables the client to focus on its discrete role in the application without concerning itself with the details of how the product is created.

Sample code is available here: https://goo.gl/MTeCWS and https://goo.gl/YoQSW4

Builder Design Pattern

Separate the construction of a complex object from its representation so that the same construction process can create different representations. Parse a complex representation, create one of several targets.They address the problem where an application needs to create the elements of a complex aggregate. The specification for the aggregate exists on secondary storage and one of many representations needs to be built in primary storage.

Many times creational patterns (such as this one) are complementary: Builder can use the Abstract Factory to implement which components get built.

Sample code is available here: https://goo.gl/W2CmFX and https://goo.gl/NiRZ4r

Some Other Patterns worth researching
Repository Pattern: https://msdn.microsoft.com/en-us/library/ff649690.aspx
Service locator Pattern: https://msdn.microsoft.com/en-us/library/ff648968.aspx


JavasScript Specific Patterns

Module Pattern


Module pattern is used to further emulate the concept of classes in such a way that we're able to include both public/private methods and variables inside a single object, thus shielding particular parts from the global scope. What this results in is a reduction in the likelihood of our function names conflicting with other functions defined in additional scripts on the page.

The Module pattern encapsulates "privacy", state and organization using closures. It provides a way of wrapping a mix of public and private methods and variables, protecting pieces from leaking into the global scope and accidentally colliding with another developer's interface. With this pattern, only a public API is returned, keeping everything else within the closure private.

This gives us a clean solution for shielding logic doing the heavy lifting whilst only exposing an interface we wish other parts of our application to use. The pattern is quite similar to an immediately-invoked functional expression (IIFE[http://goo.gl/WF41CY] - see the section on namespacing patterns for more on this) except that an object is returned rather than a function.

Sample code: https://goo.gl/KiMXFk

The Revealing Module Pattern

The Revealing Module pattern is an extension of the module pattern requires us to define all of our functions and variables in the private scope and return an anonymous object with pointers to the private functionality we wished to reveal as public.
Sample code: https://goo.gl/d85I9n

Some more links ..

https://addyosmani.com/resources/essentialjsdesignpatterns/book/
https://msdn.microsoft.com/en-us/library/ms954638.aspx
http://programmers.stackexchange.com/questions/125822/how-important-are-design-patterns-in-programming
http://www.dofactory.com/net/design-patterns
https://sourcemaking.com/design_patterns/builder
https://sourcemaking.com/design_patterns/builder


http://www.dofactory.com/images/logo2.png
Data & Object Factory helps developers succeed with .NET Design Patterns through training, products, and a .NET Design Pattern and Practices community

http://cdn.sstatic.net/Sites/programmers/img/apple-touch-icon@2.png?v=8a6d048f3c78&a
programmers.stackexchange.com
I'm a university student and I've just started learning about design patterns and im struggling to understand the purpose of them. I have tried researching them but ...


msdn.microsoft.com
This paper starts with a brief review of SOA concepts and leads up to a detailed discussion of several patterns and anti-patterns that developers can leverage when ...



https://addyosmani.com/resources/essentialjsdesignpatterns/cover/cover.jpg
addyosmani.com
#JavaScript Design Patterns . In this section, we will explore JavaScript implementations of a number of both classic and modern design patterns.