![]() | ![]() |
Home |
|
|
jConnect for JDBC Programmer's Reference |
|
| Chapter 2 Programming Information |
|
| Implementing advanced features |
|
| Using event notification |
You can use the jConnect event notification feature to have your application notified when an Open Server procedure is executed.
To use this feature, you must use the SybConnection class, which extends the Connection interface. SybConnection contains a regWatch( ) method for turning event notification on and a regNoWatch( ) method for turning event notification off.
Your application must also implement the SybEventHandler interface. This interface contains one public method, void event(String proc_name, ResultSet params), which is called when the specified event occurs. The parameters of the event are passed to event( ) and it tells the application how to respond.
To use event notification in your application, call SybConnection.regWatch( ) to register your application in the notification list of a registered procedure. Use this syntax:
SybConnection.regWatch(proc_name,eventHdlr,option)
proc_name is a String that is the name of the registered procedure that generates the notification.
eventHdler is an instance of the SybEventHandler class that you implement.
option is either NOTIFY_ONCE or NOTIFY_ALWAYS. Use NOTIFY_ONCE if you want the application to be notified only the first time a procedure executes. Use NOTIFY_ALWAYS if you want the application to be notified every time the procedure executes.
Whenever an event with the designated proc_name occurs on the Open Server, jConnect calls eventHdlr.event( ) from a separate thread. The event parameters are passed to eventHdlr.event( ) when it is executed. Because it is a separate thread, event notification does not block execution of the application.
If proc_name is not a registered procedure, or if Open Server cannot add the client to the notification list, the call to regWatch( ) throws a SQL exception.
To turn off event notification, use this call:
SybConnection.regNoWatch(proc_name)
When you use Sybase event notification extensions, the application needs to call the close( ) method on the connection to remove a child thread created by the first call to regWatch( ). Failing to do so may cause the Virtual Machine to hang when exiting the application.
Event notification exampleThe following example shows how to implement an event handler and then register an event with an instance of your event handler, once you have a connection:
public class MyEventHandler implements SybEventHandler
{
// Declare fields and constructors, as needed.
...
public MyEventHandler(String eventname)
{
...
}
// Implement SybEventHandler.event.
public void event(String eventName, ResultSet params)
{
try
{
// Check for error messages received prior to event
// notification.
SQLWarning sqlw = params.getWarnings();
if sqlw != null
{
// process errors, if any
...
}
// process params as you would any result set with
// one row.
ResultSetMetaData rsmd = params.getMetaData();
int numColumns = rsmd.getColumnCount();
while (params.next()) // optional
{
for (int i = 1; i <= numColumns; i++)
{
System.out.println(rsmd.getColumnName(i) + " =
" + params.getString(i));
}
// Take appropriate action on the event. For example,
// perhaps notify application thread.
...
}
}
catch (SQLException sqe)
{
// process errors, if any
...
}
}
}
public class MyProgram
{
...
// Get a connection and register an event with an instance
// of MyEventHandler.
Connection conn = DriverManager.getConnection(...);
MyEventHandler myHdlr = new MyEventHandler("MY_EVENT");
// Register your event handler.
((SybConnection)conn).regWatch("MY_EVENT", myHdlr,
SybEventHandler.NOTIFY_ALWAYS);
...
conn.regNoWatch("MY_EVENT");
conn.close();}
|
|