VisiBroker for Java Developer’s Guide : Event Queue

Event Queue
This section provides information about the Event Queue feature. This feature is provided for the server-side only.
A server can register listeners to the event queue based on event types that the server is interested and therefore can process those events when the server needs to do so.
Event types
Currently, connection event type is the only event type generated.
Connection events
There are two connection events that the VisiBroker ORB will generate and push to the registered connection event, as follows:
Connection established: indicates that a new client is connected to the server successfully.
Connection closed: indicates that an existing client is disconnected from the server.
Event listeners
A server implements and registers listeners with the VisiBroker ORB based on event types the server needs to process. The connection event listener is the only event listener supported.
IDL definition
The interface definitions are as follows:
module EventQueue {
// Connection event types
enum EventType {UNDEFINED, CONN_EVENT_TYPE};
// Peer (Client) connection info
struct ConnInfo {
string ipaddress; // in %d.%d.%d.%d format
long port;
long connID;
};
// Marker interface for all types of event listeners
local interface EventListener {};
typedef sequence<EventListener> EventListeners;
// connection event listener interface
local interface ConnEventListener : EventListener{
void conn_established(in ConnInfo info);
void conn_closed(in ConnInfo info);
};
// The EventQueue manager
local interface EventQueueManager : interceptor::InterceptorManager {
void register_listener(in EventListener listener, in EventType type);
void unregister_listener(in EventListener listener, in EventType type);
EventListeners get_listeners(in EventType type);
};
};
The details of the interface definitions are described in the following sections.
ConnInfo structure
The ConnInfo structure contains the following client connection information.
EventListener interface
The EventListener interface section is the marker interface for all types of event listeners.
ConnEventListeners interface
The ConnEventListeners interface defines the following operations.
The server-side application is responsible for the implementation of the ConnEventListener interface as well as the processing of the events being pushed into the listener.
EventQueueManager interface
The EventQueueManager interface is used as a handle by the server-side implementation for the registration of event listeners. This interface defines the the following operations.
How to return the EventQueueManager
An EventQueueManager object is created upon ORB initialization. Server-side implementation returns the EventQueueManager object reference using the following code:
com.inprise.vbroker.interceptor.InterceptorManagerControl control =
com.inprise.vbroker.interceptor.InterceptorManagerControlHelper.narrow(
orb.resolve_initial_references("VisiBrokerInterceptorControl"));
EventQueueManager manager =
(EventQueueManager)control.get_manager("EventQueue");
EventListener theListener = ...
manager.register_listeners(theListener);
Event Queue code samples
This section contains some code samples for registering EventListeners and implementing a connection EventListener.
Registering EventListeners
The SampleServerLoader class contains the init() method which is called by the ORB during initialization. The purpose of the ServerLoader is to register an EventListener by creating and registering it to the EventQueueManager.
import com.inprise.vbroker.EventQueue.*;
import com.inprise.vbroker.interceptor.*;
import com.inprise.vbroker.PortableServerExt.*;
public class SampleServerLoader implements ServiceLoader {
public void init(org.omg.CORBA.ORB orb) {
try {
InterceptorManagerControl control =
InterceptorManagerControlHelper.narrow(
orb.resolve_initial_references("VisiBrokerInterceptorControl"));
EventQueueManager queue_manager =
(EventQueueManager) control.get_manager("EventQueue");
queue_manager.register_listener((EventListener)new
ConnEventListenerImpl(),EventType.CONN_EVENT_TYPE);
}
catch(Exception e) {
e.printStackTrace();
throw new org.omg.CORBA.INITIALIZE(e.toString());
}
System.out.println("============>SampleServerLoader: ConnEventListener
registered");
}
public void init_complete(org.omg.CORBA.ORB orb) {
}
public void shutdown(org.omg.CORBA.ORB orb) {
}
}
Implementing EventListeners
The ConnEventListenerImpl contains a connection event listener implementation sample. The ConnEventListener interface implements the conn_established and conn_closed operations at the server-side application. For more information, see “ConnEventListeners interface”. The implementation enables the connection to idle for 30000 milliseconds while waiting for a request at the server-side. These operations are called when the connection is established by the client and when the connection is dropped, respectively.
import com.inprise.vbroker.EventQueue.*;
import org.omg.CORBA.LocalObject;

public class ConnEventListenerImpl extends LocalObject implements ConnEventListener {
public void conn_established(ConnInfo info) {
System.out.println("Received conn_established: address = " +
info.ipaddress + " port = " + info.port +
" connID = " + info.connID);
System.out.println("Processing the event ...");
try {
Thread.sleep(30000);
} catch (Exception e) { e.printStackTrace(); }
}
public void conn_closed(ConnInfo info) {
System.out.println("Received conn_closed: address = " +
info.ipaddress+ " port = " + info.port +
" connID = " + info.connID);
}
}