Friday Sep 11, 2009

How to create pluggable container-monitoring element in GlassFish v3

For new modules like JRuby which can be added at runtime, it is possible to use v3 monitoring config using pluggability as given in the sample code below.
import org.glassfish.internal.api.Init;
import org.jvnet.hk2.annotations.Service;
import org.jvnet.hk2.component.PostConstruct;
import org.jvnet.hk2.annotations.Inject;
import org.glassfish.api.Startup;
import com.sun.enterprise.config.serverbeans.MonitoringService;
import org.jvnet.hk2.config.SingleConfigCode;
import org.jvnet.hk2.config.ConfigSupport;
import org.glassfish.api.monitoring.ContainerMonitoring;
import org.jvnet.hk2.config.TransactionFailure;
import java.beans.PropertyVetoException;

@Service
public class AddonInit implements PostConstruct {

    @Inject
    MonitoringService ms;

    public void postConstruct() {
        mprint("addon init postConstruct() ...");
        if (ms.getContainerMonitoring("jruby") == null) {
            try {
                ConfigSupport.apply(new SingleConfigCode<MonitoringService>() {
                    public Object run(MonitoringService param)
                    throws PropertyVetoException, TransactionFailure {
                        ContainerMonitoring newItem = param.createChild(ContainerMonitoring.class);
                        newItem.setName("jruby");
                        newItem.setLevel("HIGH");
                        param.getContainerMonitoring().add(newItem);
                        return newItem;
                    }
                }, ms);
            } catch (TransactionFailure tf) {
                // log warning
                // handle exception
            }
        }
    }

    private void mprint(String str) {
        if (debug) {
            System.out.println("... MSR: " + str);
        }
    }

    private final boolean debug = true;
}

About

msreddy

Search

Categories
Archives
« April 2014
SunMonTueWedThuFriSat
  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
   
       
Today