Sun GlassFish Enterprise Server v3 Add-On Component Development Guide

ProcedureTo Create a Transaction to Update Configuration Data

Any transaction that you create to modify configuration data must use a configuration change transaction to ensure that the change is atomic, consistent, isolated, and durable (ACID).

  1. Set a dependency on the configuration object to update.

  2. Define a method to invoke to perform the transaction.

    1. Use the generic SimpleConfigCode interface to define the method that is to be invoked on a single configuration object, namely: SingleConfigCode<T extends ConfigBeanProxy>().

    2. In the body of this method, implement the run method of the SingleConfigCode<T extends ConfigBeanProxy> interface.

    3. In the body of the run method, invoke the setter methods that are defined for the attributes that you are setting.

      These setter methods are defined in the interface that represents the element whose elements you are setting.

  3. Invoke the static method org.jvnet.hk2.config.ConfigSupport.ConfigSupport.apply.

    In the invocation, pass the following information as parameters to the method:

    • The code of the method that you defined in Step 2

    • The configuration object to update, on which you set the dependency in Step 1


Example 6–10 Creating a Transaction to Update Configuration Data

This example shows code in the execute method of an asadmin subcommand for updating the number-of-instances element of wombat-container-config element.

...
import org.glassfish.api.Param;
...
import org.jvnet.hk2.annotations.Inject;
import org.jvnet.hk2.config.Transactions;
import org.jvnet.hk2.config.ConfigSupport;
import org.jvnet.hk2.config.SingleConfigCode;
import org.jvnet.hk2.config.TransactionFailure;
...
    @Param
    String instances;

    @Inject
    WombatContainerConfig config;

    public void execute(AdminCommandContext adminCommandContext) {
        try {
            ConfigSupport.apply(new SingleConfigCode<WombatContainerConfig>() {
                public Object run(WombatContainerConfig wombatContainerConfig) 
			            throws PropertyVetoException, TransactionFailure {
                    wombatContainerConfig.setNumberOfInstances(instances);
                    return null;
                }
            }, config);
        } catch(TransactionFailure e) {            
        }
    }
...