The Message brokers intermediate the communication between different sides of the system. The message queue is the resource to support that. Examples of Message Queue are Java Message Queue, ActiveMQ, RabbitMQ.

Java Message Service (JMS)

The JMS is a enterprise messaging API to handle asynchronous requests or events that will be consumed by an application. JMS falls under middleware, and specifically Message-Oriented Middleware (MOM), which is a relatively low-level of abstraction that runs underneath complementary layers such as database and application adapters, event processing, and business process automation.

Architecture

  • Provider: JMS implementation
  • JMS clients: who send and receive messages
  • Messages: object with information (requests or events)
  • Administered object" preconfigured JMS object.

Message Delivery Models

  • Point-to-Point (queue destination): deliver the message to a queue and then to the registered consumer.
  • Publish/Subscribe (topic destination): deliver the message to all consumers subscribed to the topic destination.

Message

  • Header: information that is used for routing and identifying messages
  • Properties: provide values that clients can use to filter messages
  • Body: contains the actual data to be exchanged

Sun Java System Message Queue: Sun's implementation to the queue supporting point-to-point and publish/subscribe messaging models and synchronous and asynchronous messaging.

Reliable Messaging: JMS can use a persistent messages mode, which guarantees the message is not lost and consumed only once; or non-persistent messages mode, in which the message can be delivered more than once and can be lost. The second one has better performance..


ActiveMQ

Apache ActiveMQ® is the most popular open source, multi-protocol, Java-based message broker.

A summary of how to start the activeMQ is:

  1. Install
    • Ex MAC: $brew install apache-activemq
  2. Run
    • Ex MAC: $/opt/homebrew/opt/activemq/bin/activemq console
  3. Access: http://127.0.0.1:8161/admin/
    • Login: admin; PWD: admin
  4. Test produce a message
    • $curl -u admin:admin -d "body=message" http://localhost:8161/api/message/TEST?type=queue
    • Alternative: $curl -XPOST -d "body=message" http://admin:admin@localhost:8161/api/message?destination=queue://orders.input
  5. Test consume a message
    • wget --user admin --password admin --save-cookies cookies.txt --load-cookies cookies.txt --keep-session-cookies http://localhost:8161/api/message/TEST\?type\=queue
    • Alternative: $curl -XGET http://admin:admin@localhost:8161/api/message?destination=queue://orders.input
  6. Stop
    • Ex MAC: $/opt/homebrew/opt/activemq/bin/activemq stop

The figure below shows the first moment with the produced message (right side) and then values '0' to the message after it is consumed.

Example

Here is an example of an implementation how to produce and consume a message to/from queue.

An alternative is to use jmsTemplate. Here is an example for Producer.

JmsTemplate jmsTemplate = new JmsTemplate();
		jmsTemplate.setConnectionFactory(connectionFactory);

jmsTemplate.send(queue, new MessageCreator() {    			
  @Override
  public Message createMessage(Session session) throws JMSException {
    ObjectMessage message = session.createObjectMessage(messageStr);
      return message;
    }
});    

Attention the URI used as parameter to ActiveMQConnectionFactory. It is beginning by "vm". It is the topology that can be:

  • VM (vm://localhost/foo): manage different JMS groups inside the same JVM
  • Client-Server (tcp://somehost:port): connect the message Broker using TCP, SSL, NIO, etc.
  • Embedded Broker: communcation between the client and server (broker) are all within the same JVM and so do not use real networking
  • Peer to Peer: This allows peer based clusters to be created where there is no server - just clients connecting together.
  • JXTA (jxta://hostname:port): use the full JXTA stack for negotiating NAT and across firewalls and so forth for creating a true peer based JMS network.


REFERENCE