Skip to content

Async Message Queue

Yair Ogen edited this page May 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

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

Clone this wiki locally