Introduction
The most common pub/sub model
consists of one or more publishers and one or more subscribers with a messaging broker
(a Nirvana Realm Server for example)
in the middle. The message broker marshalls (brokers) messages from publishers to susbcribers.
A message is first published by the publisher application over the network to the message broker, and then
from the message broker, over the network again, to the subscribers. Thus for each message, there are 2 network hops, the first being
from the publisher to the broker, the second being from broker to susbcriber. When network latencies are critical,
each network hop adds unwelcome latency to the end point, i.e the final receipt of the message by subscribers.
Nirvana Direct enables applications to talk directly to Nirvana, this reducing network overhead.
For example, by enabling your publisher application to use Nirvana Direct, all published events written will avoid any network IO and be handled internally through a Nirvana Direct inter process connection.
Ultra low Latencies
Nirvana Realm Servers are already capable of delivering large numbers of events to lots of clients while ensuring low latencies. By integrating a Nirvana Direct Server into your application,
you can take advantage of the reduction of network hops which dramatically reduces latencies for events delivered to consumers.
In our tests performed in our benchmarking labs, we were able to sustain 400,000 events delivered per second, with average latencies
of 0.40 milliseconds per message, and where 99.989% of those events were delivered with latencies of <2ms. For more information on these tests, and to a
see a full breakdown of the results, please click here.
Management
Nirvana Direct is managed in the same way as standard Nirvana Realm Servers. Nirvana Direct Servers can exist in a federated name space. All configurable components of a Nirvana Direct server can be accessed centrally via the Enterprise Manager.
How Does it work?
Nirvana Direct provides all of the pub/sub, message queues and p2p capabilities you will find in a Nirvana Realm Server.
While Nirvana Direct appears like a normal Nirvana Realm Server to clients, Nirvana Direct offers a much smaller resource footprint within which it is required to run. Nirvana Direct is
a streamlined Realm Server that make it easy to run within a client application.
In order to enable your application to run using Nirvana Direct, there are a few easy steps to follow:
Running a Nirvana Direct Publisher Application (Service Mode)
By including the nServer.jar, and nDirect.jar (provided in the Nirvana install package) in your classpath, with a few simple lines of code, your publisher application can take advantage of the superior performance offered by using Nirvana Direct.
The Transport class creates the Nirvana Direct instance, and configures the mode of operation. There are 2 modes, either 'service' more, or listener mode. In this instance, we are creating a publisher in 'service' mode.
Please note that when you start a Nirvana realm using the nDirect API, the transport you start is only a bootstrap transport for your server. After the first start, the network interface configuration is persisted in the data directory created. Changing the server transport details when a data directory is present will start your server but using the information in the data directory and not what you specify in the startTransport method.
Once the transport has been started, your application needs to bind to one or more topics, as shown in the example below. Once the topic has been bound, you are ready to send data onto the topic.
Transport myTransport = Transport.startTransport("nsp://0.0.0.0:9000", "DirectInstance1", "root");
Topic myTopic = myTransport.bindTopic("topic1");
int len = 8;
byte[] data = new byte[len];
long val = System.nanoTime();
int bitPos = 56;
for (int y = 0; y < len; y++) {
data[y] = (byte) ((val >> bitPos) & 0xff);
bitPos -= 8;
}
myTopic.send("Tag", data);
The above code will send an event with the current system time in nanos encoded into a byte array.
Running a Nirvana Direct Subscriber Application (Listener Mode)
Running in listener mode, using Nirvana Direct will not act as a server, but instead, connect to a remote Nirvana Direct instance and consume data from the Direct instance.
Your application must implement the TopicListener interface and the receive(nConsumeEvent) callback through which your aplication will consume the events.
Transport myTransport = Transport.connectToTransport(myUrl, myUser);
Topic myTopic = myTransport.bindTopic(myTopicName);
myTopic.startReceiving(this);
.
.
.
public void receive(nConsumeEvent evt) {
byte[] data = evt.getEventData();
int bitPos = 56;
long val = 0;
for (int x = 0; x < 8; x++) {
val = (((long) data[x] & 0xff) << bitPos) | val;
bitPos -= 8;
}
float latency = ((System.nanoTime() - val) / 1000000.0f);
System.out.println("Rec'd in "+latency);
}
|