HomeIndex > FAQ > GBean Development > Is it possible to get a reference to the NotificationBroadcasterSupport from a GBean?

Yes. The following example shows how to obtain a reference to the NotificationBroadcasterSupport for the GBean and demonstrates how to send notifications using the NotificationBroadcasterSupport:

public class Example {

    private AtomicInteger notificationSequence = new AtomicInteger(1);
    private NotificationBroadcasterSupport notificationBroadcasterSupport;
    private AbstractName name;

    public Example(Kernel kernel, AbstractName name) {
        this.notificationBroadcasterSupport = getNotificationBroadcasterSupport(kernel, name);
        this.name = name;
    }

    private static NotificationBroadcasterSupport getNotificationBroadcasterSupport(Kernel kernel,
                                                                                    AbstractName name) {
        try {
            MBeanServerKernelBridge bridge = 
                (MBeanServerKernelBridge)kernel.getGBean(MBeanServerKernelBridge.class);
            return bridge.getNotificationBroadcasterSupport(name);
        } catch (Exception e) {            
            return null;
        }
    }

    protected boolean isNotificationSupported() {
        return (this.notificationBroadcasterSupport != null);        
    }
    
    protected void sendNotification() {
        if (!isNotificationSupported()) {
            return;
        }
        Notification notification = new Notification("EXAMPLE_NOTIFICATION_TYPE",
                                                     name.getObjectName().getCanonicalName(),   
                                                     this.notificationSequence.getAndIncrement());
        notification.setUserData("hello");
        
        this.notificationBroadcasterSupport.sendNotification(notification);
    }

    public static final GBeanInfo GBEAN_INFO;

    static {
        GBeanInfoBuilder infoBuilder = GBeanInfoBuilder.createStatic(Example.class);
        infoBuilder .addAttribute("abstractName", AbstractName.class, false);
        infoBuilder .addAttribute("kernel", Kernel.class, false);

        infoFactory.setConstructor(new String[] { "kernel",  
                                                  "abstractName" });

        GBEAN_INFO = infoBuilder.getBeanInfo();
    }

    public static GBeanInfo getGBeanInfo() {
        return GBEAN_INFO;
    }
}