|
How do i
create an event based Server Service
Firstly, in the same way that publish / subscribe and
message queues use an RNAME,
the p2p API also requires one to connect to the realm.
The code snippet below shows you how this is achieved
:
String[] RNAME=({“nsp://127.0.0.1:9000”});
nSessionAttributes nsa=new nSessionAttributes(RNAME);
nServiceFactory factory = new nServiceFactory( nsa
);
Once the service factory is created, and we are connected
to the realm, we can create the event server service.
nServerService server = factory.createEventService(
"example",
"this service is an example" );
while ( true ) {
nEventService serv = (nEventService)server.accept();
/**
.your logic goes here....i.e. what does you server
service do with the instance of nEventService created
once a connection is established?
does it go and query a database, make a connection
send an email? etc etc.
*/
System.out.println("Got connection "+serv.getServiceInfo().getName());
}
When connections are made to the event service, the
service can receive events from clients either synchronously
or asynchronously via a callback interface.
To synchrnously allow the server service to read incoming
events, you can do the following:
nConsumeEvent event = serv.read();
Which will return an event once one is received from
the client service.
To asynchronously receive events from an event service,
you can implement the nEventServiceListener interface
which will require you to implement the following method:
public void receivedEvent(nConsumeEvent evt)
{
System.out.println("Consumed event "+event.getEventID());
}
And you need to call the registerListener(your_listener_class)
on the nEventService object.
You can send events back to the client by using the
following command:
serv.write(new nConsumeEvent("TAG",
message.getBytes()));
How do i create
an event based Client Service
The nServiceFactory object establishes a connection
with the Nirvana realm, and is the factory object from
which we can then find our service, or obtain a list
of available services:
nServiceInfo info = factory.findService("example");
nEventService serv = (nEventService)factory.connectToService(
info );
Once you have connected to the service, and you have
an instance of the service, you can then begin publishing
your Nirvana events to the service, by using the following
command:
serv.write(new nConsumeEvent("TAG",
message.getBytes()));
To receive responses from the server service, the client
service can receive events either synchronously or asynchronously
via a callback interface.
To synchrnously allow the server service to read incoming
events, you can do the following:
nConsumeEvent event = serv.read();
Which will return an event once one is received from
the server service.
To asynchronously receive events from an event service,
you can implement the nEventServiceListener interface
which will require you to implement the following method:
public void receivedEvent(nConsumeEvent evt)
{
System.out.println("Consumed event "+event.getEventID());
}
And you need to call the registerListener(your_listener_class)
on the nEventService object.
For more information on Nirvana p2p, please see the
API documentation.
|