GlassFish admin console provides GUI way of doing things but I found using CLI is a lot faster.
asadmin create-custom-resource ...
asadmin list-custom-resources ...
asadmin delete-custom-resource ...
asadmin get 'resources.custom-resource.*'
asadmin set ...
For example:
$ asadmin create-custom-resource --restype=java.lang.String --factoryclass=org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property value="http\://javahowto.blogspot.com" resource/javahowtoTo look up the custom resource created above, just perform the following in any web or EJB component classes, e.g., servlet, filter, interceptor, web listener, EJB bean class:
$ asadmin create-custom-resource --restype=java.lang.Double --factoryclass=org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property value=0.5 resource/rate
$ asadmin create-custom-resource --restype=java.lang.Boolean --factoryclass=org.glassfish.resources.custom.factory.PrimitivesAndStringFactory --property value=true resource/flag
try {An easier way to obtain these resources is through
InitialContext ic = new InitialContext();
String javahowto = (String) ic.lookup("resource/javahowto");
double rate = (Double) ic.lookup("resource/rate");
boolean flag = (Boolean) ic.lookup("resource/flag");
System.out.printf(
"custom resources from lookup: javahowto=%s, rate=%s, flag=%s%n",
javahowto, rate, flag);
} catch (NamingException e) {
throw new RuntimeException(e);
}
@Resource
injection in any web or EJB component class: @Resource(lookup="resource/javahowto")These custom resources can also be accessed from a remote Java SE client:
private String withLookup;
@Resource(lookup="resource/rate")
private double rate;
@Resource(lookup="resource/flag")
private boolean flag;
To compile and run the remote client:
import javax.naming.*;
/**
* A generic remote client for testing JNDI lookup.
*/
public class TestLookup {
public static void main(String args[]) throws Exception {
InitialContext ic = new InitialContext();
for(String name : args) {
System.out.printf("Looked up jndi name %s, got %s%n",
name, ic.lookup(name));
}
}
}
The warning message "Cannot find javadb client jar file" can be safely ignored. To avoid this annoying message, just add command line system property AS_DERBY_INSTALL, pointing to a valid derby installation:
javac TestLookup.java
java -cp "$GLASSFISH_HOME/modules/*:." TestLookup resource/javahowto resource/rate resource/flag
Feb 16, 2012 8:21:21 PM com.sun.enterprise.v3.server.CommonClassLoaderServiceImpl findDerbyClient
INFO: Cannot find javadb client jar file, derby jdbc driver will not be available by default.
Looked up jndi name resource/javahowto, got http://javahowto.blogspot.com
Looked up jndi name resource/rate, got 0.5
Looked up jndi name resource/flag, got true
You can view all attributes and properties of a custom resource with asadmin get command:
java -cp "$GLASSFISH_HOME/modules/*:." -DAS_DERBY_INSTALL=$GLASSFISH_HOME/../javadb TestLookup resource/javahowto resource/rate resource/flag
To update the value of a custom resource with asadmin set command:
asadmin get 'resources.custom-resource.resource/rate.*'
resources.custom-resource.resource/rate.property.value=0.6
resources.custom-resource.resource/rate.enabled=true
resources.custom-resource.resource/rate.factory-class=org.glassfish.resources.custom.factory.PrimitivesAndStringFactory
resources.custom-resource.resource/rate.jndi-name=resource/rate
resources.custom-resource.resource/rate.object-type=user
resources.custom-resource.resource/rate.res-type=java.lang.Double
Command get executed successfully.
asadmin set 'resources.custom-resource.resource/rate.property.value'=0.6
resources.custom-resource.resource/rate.property.value=0.6
Command set executed successfully.