Notifications

The JoobContext class provides the RegisterNotificationHandler method to request being notified about events for a specified JADE object and the RegisterClassNotificationHandler method to request being notified about events for all objects of a specified class.

The first parameter is the target of the notifications. The second parameter is the event type number (you can use the constants in the NotificationEventConstants class for system events). The third parameter is the name of the event handler method to be invoked.

The following example shows how you can register for notifications.

JoobContext context = JoobContext.CurrentContext;
// Register for any kind of system event on the 'agent' object
// the event handler method is called 'myEventHandler'
context.RegisterNotificationHandler(agent,
                                    NotificationEventConstants.SystemAny,
                                    myEventHandler);

// Register for user event 5000 for all instances of the Agent class
// the event handler method is called 'event5000Handler'
context.RegisterClassNotificationHandler(agent, 5000, event5000Handler);

There are UnregisterNotificationHandler and UnregisterClassNotificationHandler methods to stop the sending of notifications. The methods have the same parameters as the methods that register for notifications.

The Notify method, which is inherited from the JoobObject class, publishes a user event. The first parameter is the event type number. The second parameter is True if the event is raised immediately and False if it deferred until the next time a transaction is committed. The method is overloaded so that the third parameter, which is user information about the event, can be of any type.

The following example shows how you can cause a user event.

// 'agent' is an instance of the Agent class
agent.Notify(agent, false, 0);

An event handler method has a signature similar to any C# event handler.

void myEventHandler(object sender, NotificationEventArgs e)
{
    JoobContext context = sender as JoobContext;
    int eventNo = e.EventNo;
    ObjectId target = e.Target;
}

The first parameter is JoobContext object that was used to register for notifications. The second parameter is a NotificationEventArgs object, which has the following properties.

Property Description
EventNumber The event number integer that was passed as the middle parameter of the RegisterClassNotificationHandler or RegisterNotificationHandler method.
Target The ObjectId of the object to which the event happened.
UserInfo

For user events, it is an object representing the UserInfo parameter that was passed to the Notify method when the user event was published. For system events, it is a null object reference.

Notifications in JADE and .NET

If a .NET application calls a JADE method that executes the beginNotification or beginClassNotification method, the notification is delivered to the thread that created the JoobContext object.

When the beginNotification or beginClassNotification is executed in a JADE method, it is regarded as subscribing to a different notification than one done by executing the RegisterNotificationHandler or RegisterClassNotificationHandler method in .NET, even if all of the parameters are equivalent. However, when an event is explicitly (or implicitly) caused in either language, it is delivered to all subscribers even if the language that caused the notification did not subscribe to the notification.

When a JoobContext object is disposed of, all subscriptions are cancelled.

As the JADE code is running within .NET, the JADE process will not have an idle state. Consequently, you must execute the doWindowEvents method of the Application class to enable the notification response method to run. For details about the cautions regarding the use of this method, see Volume 1 of the JADE Encyclopaedia of Classes.