The legacy connector client also sends notifications that signal any changes in the state of the connection. These notifications are instances of the HeartBeatNotification class. The HeartBeatClientHandler interface includes methods specifically for registering for heartbeat notifications. These methods are distinct from those of the NotificationRegistration interface that a connector client implements for transmitting agent-side notifications (see 22.1 Registering Manager-Side Listeners).
// Register this manager as a listener for heartbeat notifications // (the filter and handback objects are not used in this example) // echo("\tAdd this manager as a listener for heartbeat notifications..."); connectorClient.addHeartBeatNotificationListener(this, null, null); |
Instances of heartbeat notifications contain the connector address object from the connection that generated the event. This enables the notification handler to listen to any number of connectors and retrieve all relevant information about a specific connection when it triggers a notification. The HeartBeatNotification class defines constants to identify the possible notification type strings for heartbeat events:
CONNECTION_ESTABLISHED – Emitted when the connect method succeeds
CONNECTION_RETRYING – After the heartbeat fails and if the number of retries is not zero, this notification type is emitted once when the first retry is sent
CONNECTION_REESTABLISHED – Emitted if the heartbeat recovers during one of the retries
CONNECTION_LOST – Emitted after the heartbeat and all retries, if any, have failed or when a heartbeat contains the wrong connection identifier, indicating that the connector server has been stopped and restarted
CONNECTION_TERMINATED – Emitted when the disconnect method successfully terminates a connection and frees all the resources it used; therefore, this notification type is received both after a user–terminated connection and after a connection is lost
Once they are established, connections can go through any number of retrying/re-established cycles and then be terminated by the user or determined to be lost and terminated automatically. When the heartbeat mechanism is deactivated by setting the heartbeat period to zero, only heartbeat notifications for normally established and normally terminated connections continue to be sent. In that case, connections can be lost but they are not detected nor indicated by a notification.
The following diagram shows the possible sequence of heartbeat notifications during a connection. Retries are enabled when the getHeartBeatRetries method returns an integer greater than zero.
Example 21–9 shows the source code for the notification handler method in our manager class. The handler prints out the notification type and the RMI address associated with the connector that generated the notification.
public void handleNotification(Notification notification, Object handback){ echo("\n>>> Notification has been received..."); echo("\tNotification type = " + notification.getType()); if (notification instanceof HeartBeatNotification) { ConnectorAddress notif_address = ((HeartBeatNotification)notification).getConnectorAddress(); if (notif_address instanceof RmiConnectorAddress) { RmiConnectorAddress rmi_address = (RmiConnectorAddress) notif_address; echo("\tNotification connector address:"); echo("\t\tTYPE = " + rmi_address.getConnectorType()); echo("\t\tHOST = " + rmi_address.getHost()); echo("\t\tPORT = " + rmi_address.getPort()); echo("\t\tSERVER = " + rmi_address.getName()); } } } |
In the agent application, the connector server does not emit any notifications about the state of its connections. The HTTP protocol-based connectors do provide a count of active clients, but there is no direct access to heartbeat information in an agent's connector servers.