Skip to content

Async Message Queue

Yair Ogen edited this page Jun 15, 2014 · 23 revisions

Foundation Communication supports a simple API that exposes API or async messaging. Although JMS being a standard we've found that many times other vendors can do better. Many such vendors are not JMS Compliant (like RabbitMQ) or have a richer non-jms API (like HornetQ core).

Following is an explanation on how to use and configure the Queue API.

API

The Main building blocks are:

  • Message - Basic message POJO.
  • MessageConsumer - basic interface that defines API available for message consumers.
  • MessageProducer - basic interface that defines API available for message producers.
  • HornetQMessagingFactory - This is the main API tp be used to instantiate new consumers and producers. This class supports a Per Thread lifecycle for HornetQ session, consumers and producers

Usage Example

Note: For both producer and consumer, then name is also used as a "configuration prefix" in order to focus the subset of the configuration per producer/consumer. Examples below.

Producer

final MessageProducer producer = HornetQMessagingFactory.createProducer("example");
producer.sendMessage("hello world!");

//optionally send message properties
Map<String,Object> props = new HashMap<String,Object>();
props.put("key1","value2");
producer.sendMessage("hello world!",props);
        }

Consumer

Depending on configuration consumers can be either in a 1:1 form (i.e. producer send message and only one consumer can get it), or 1:n (i.e. many consumers can the same copy although producer sent only once. A.K.A "JMS Topic").

Sync Receive

final MessageConsumer consumer = HornetQMessagingFactory.createConsumer("consumer1");
Message message = consumer.receive(2500);
system.out.println("got message: " + message.getPayloadAsString()):

A-Sync Receive

final MessageConsumer consumer = HornetQMessagingFactory.createConsumer("consumer1");
consumer.registerMessageHandler(new AbstractHornetQMessageHandler() {
    @Override
    public void onMessage(Message message) {
        System.out.println("[1] " + message.getPayloadAsString());
    }
});

Configuration

#Client connections to HornetQ Server - you can define as many entries as needed
service.queue.connections.1.host=10.45.37.122
service.queue.connections.1.port=5445
service.queue.connections.2.host=10.45.37.123
service.queue.connections.2.port=5445

#consumer properties
#-------------------

#consumer1 - this is the consumer name that was used in code to initialize a new consumer
consumer1.queue.name=consumer1

#an optional filter - only messages with proeprties aligned with this filter will be received by this consumer
consumer1.queue.filter=key1='value2'

#set to true if this consumer is a part of a 1:N consumers (like a topic subscriber)
consumer1.queue.isSubscription=true

#if isSubscription is set to true, specify here the publisher address name
consumer1.queue.subscribedTo=myExample

#producer properties
#-------------------

#example is the producer name as supplied in code when initializing a new producer
example.queue.name=myExample
Clone this wiki locally