This is archived documentation for an older version of Nirvana (v3.1). Please refer to documentation for the latest version if required.

How to Synchronously consume from a Nirvana Queue

Synchronous queue consumers consume events by calling pop() on the Nirvana queue reader object. Each pop call made on the queue reader will synchronously retrieve the next event from the queue.

An example of a synchronous queue reader is shown below:

public class mySyncQueueReader {

nQueueSyncReader reader = null;
nQueue myQueue = null;

public mySyncQueueReader() throws Exception {

// construct your session and queue objects here

// construct the queue reader
nQueueReaderContext ctx = new nQueueReaderContext(this, 10);
reader = myQueue.createReader(ctx);

}

public void start() throws Exception {

while (true) {

// pop events from the queue
nConsumeEvent event = reader.pop();
go(event);

}

}

public void go(nConsumeEvent event) {

System.out.println("Consumed event "+event.getEventID());

}

public static void main(String[] args) {

try {

mySyncQueueReader sqr = new mySyncQueueReader();
sqr.start();

} catch (Exception e) {

e.printStackTrace();

}

}

}

Sychronous queue consumers can also be created using a selector, which defines a set of event properties and their values that a consumer is interested in. For example if events are being published with the following event properties:

nEventProperteis props =new nEventProperties();
props.put(“BONDNAME”,”bond1”);

If you then provide a message selector string in the form of:

String selector = "BONDNAME='bond1'";

And pass this string into the constructor for the nQueueReaderContext object shown in the example code, then your consumer will only consume messages that contain the correct value for the event property BONDNAME.

An example of a synchronous queue consumer can be found here. For more information on Nirvana message queues, please see the API documentation.