Universal Messaging 9.7 | Universal Messaging Developer Guide | Enterprise APIs | Enterprise Developer's Guide for Java | Peer to Peer Services | Peer to Peer Event-based Server Services
 
Peer to Peer Event-based Server Services
Universal Messaging Peer to Peer Event-based Services communicate via events which are published by an Event-based Client, and received and responded to by an Event-based Server Service.
Creating 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 how this is achieved:

String[] RNAME=({"nsp://127.0.0.1:9000"});
nSessionAttributes nsa = new nSessionAttributes(RNAME);
nServiceFactory factory = new nServiceFactory( nsa );
The nServiceFactory object establishes a connection with the Universal Messaging Realm, and is the factory object from which we can construct our Event-based Server Service:

nServerService Server = factory.createEventService( "example",
"Example Event-based Service" );
while ( true ) {
nEventService serv = (nEventService) server.accept();
// your logic goes here....
// e.g. query a database, make a connection, send an email, etc.
System.out.println("Got connection " + serv.getServiceInfo().getName());
}
The code snippet above shows how to create an Event-based Server Service and wait for Client connections. Developers are free to decide how the Server Service should respond once a Client connects to the Server Service.
When connections are made to the Event-based Server Service, the Service can receive events from Clients either synchronously or asynchronously via a callback interface.
Synchronously Receiving Events from the Client
The Server Service can synchronously read incoming events. The following code will return an event once one is received from the Client:

nConsumeEvent event = serv.read();
Asynchronously Receiving Events from the Client
The Server Service may alternatively asynchronously receive events by implementing the nEventServiceListener interface and its receivedEvent method:

public void receivedEvent(nConsumeEvent evt) {
Console.WriteLine("Consumed event " + event.getEventID());
}
You will also need to call registerListener(your_listener_class) on the nEventService object.
Sending Events to Clients
You can send events back to the Client as follows:

serv.write(new nConsumeEvent("TAG", message.getBytes()));

Copyright © 2013-2015 | Software AG, Darmstadt, Germany and/or Software AG USA, Inc., Reston, VA, USA, and/or its subsidiaries and/or its affiliates and/or their licensors.