5.4.4. JMX 通知の受信
JBoss Cache ユーザーは、 リスナーを登録して 「キャッシュリスナの追加 - キャッシュイベントに対する登録」 の章で説明したキャッシュイベントを受信できます。 また、 別の方法としてユーザーはキャッシュの管理情報インフラストラクチャを使用して JMX 通知よりこれらのイベントを受信することもできます。
CacheJmxWrapper に対して NotificationListener を登録すると、 キャッシュイベントは通知としてアクセス可能になります。
CacheJmxWrapper より受信可能な通知一覧は、 「JMX MBean 通知」 の JMX 通知に関する項を参照してください。
下記は、 JBoss AS 環境の実行時にプログラムを用いてキャッシュ通知を受信する方法の例になります。 この例では、 クライアントはフィルタを使用して興味のあるイベントを指定しています。
MyListener listener = new MyListener();
NotificationFilterSupport filter = null;
// get reference to MBean server
Context ic = new InitialContext();
MBeanServerConnection server = (MBeanServerConnection)ic.lookup("jmx/invoker/RMIAdaptor");
// get reference to CacheMgmtInterceptor MBean
String cache_service = "jboss.cache:service=TomcatClusteringCache";
ObjectName mgmt_name = new ObjectName(cache_service);
// configure a filter to only receive node created and removed events
filter = new NotificationFilterSupport();
filter.disableAllTypes();
filter.enableType(CacheNotificationBroadcaster.NOTIF_NODE_CREATED);
filter.enableType(CacheNotificationBroadcaster.NOTIF_NODE_REMOVED);
// register the listener with a filter
// leave the filter null to receive all cache events
server.addNotificationListener(mgmt_name, listener, filter, null);
// ...
// on completion of processing, unregister the listener
server.removeNotificationListener(mgmt_name, listener, filter, null);
前の例で使用された単純な通知リスナー実装を以下に示します。
private class MyListener implements NotificationListener, Serializable
{
public void handleNotification(Notification notification, Object handback)
{
String message = notification.getMessage();
String type = notification.getType();
Object userData = notification.getUserData();
System.out.println(type + ": " + message);
if (userData == null)
{
System.out.println("notification data is null");
}
else if (userData instanceof String)
{
System.out.println("notification data: " + (String) userData);
}
else if (userData instanceof Object[])
{
Object[] ud = (Object[]) userData;
for (Object data : ud)
{
System.out.println("notification data: " + data.toString());
}
}
else
{
System.out.println("notification data class: " + userData.getClass().getName());
}
}
}
JBoss Cache 管理実装は、 クライアントが MBean 通知を受信するよう登録された後でのみキャッシュイベントをリッスンします。 クライアントが通知のために登録されると、 MBean はキャッシュリスナとして MBean 自体を除外します。