9/26/2012

How to Create Global Modules in JBoss AS7

In other Java application servers and previous versions of JBoss AS, there is the concept of common lib, where users can put shared libraries for use by all deployed apps. How to achieve the same effect in AS7 with modules and their dependency? The easiest (not necessarily the best) way is to create a global module for a collection of shared jars.

1, Create a module directory structure: 

mkdir -p $JBOSS_HOME/modules/com/mycompany/myproject/main 

2, Copy common jar files to the above directory: 

cp my-util.jar my-service.jar $JBOSS_HOME/modules/com/mycompany/myproject/main

3, Create a module.xml file in the new module's main directory:

<?xml version="1.0" encoding="UTF-8"?>
<module xmlns="urn:jboss:module:1.1" name="com.mycompany.myproject">
    <dependencies>
        <module name="javaee.api"/>
    </dependencies>

    <resources>
        <resource-root path="my-util.jar"/>
        <resource-root path="my-services.jar"/>
    </resources>
</module>

4, Declare the module com.mycompany.myproject as a global module, by editing $JBOSS_HOME/standalone/configuration/standalone.xml. Add to the ee subsystem:

<subsystem xmlns="urn:jboss:domain:ee:1.1">
    <global-modules>
        <module name="com.mycompany.myproject" slot="main"/>
    </global-modules>
</subsystem>

Editing server configuration files is not a good idea, but so far this is the only way of adding global modules.

9/24/2012

GlassFish CLI and JBoss AS7 CLI Commands

A rough comparison of GlassFish 3.x CLI and JBoss AS 7 CLI commands:

Tasks GlassFish 3.x JBoss AS 7
List all available commands asadmin list-commands jboss-cli.sh -c "help --commands"
Display help info asadmin --help jboss-cli.sh --help
Check version asadmin version jboss-cli.sh version
Start server or domain asadmin start-domain standalone.sh, or domain.sh
Stop server or domain asadmin stop-domain jboss-cli.sh -c :shutdown
Restart server or domain asadmin restart-domain jboss-cli.sh -c ":shutdown(restart=true)"
Start in debug mode asadmin start-domain -d
asadmin start-domain --debug
standalone.sh --debug
Deploy an app with CLI asadmin deploy ~/test.war jboss-cli.sh -c "deploy ~/test.war"
Undeploy the test.war app asadmin undeploy test jboss-cli.sh -c "undeploy test.war"
List deployed apps asadmin list-applications
asadmin list-components
asadmin list-applications -l
asadmin list-components --long
jboss-cli.sh -c deploy
jboss-cli.sh -c undeploy
jboss-cli.sh -c "deploy -l"
jboss-cli.sh -c "undeploy -l"
jboss-cli.sh -c "ls deploy"
Add a server system property asadmin create-jvm-options -Dbuzz="This is buzz" jboss-cli.sh
-c "/system-property=buzz:add(value='This\ is\ buzz')"
List all server system properties asadmin list-jvm-options jboss-cli.sh -c "/system-property=*:read-resource"
Create a string or primitive JNDI resource asadmin create-custom-resource
--restype=java.lang.Boolean
--factoryclass=
org.glassfish.resources.custom.factory.PrimitivesAndStringFactory
--property value=true resource/flag
jboss-cli.sh -c
"/subsystem=naming/binding=
java\:global\/env\/flag:add(
binding-type=simple, type=boolean, value=true)"
List datasources asadmin list-jdbc-resources jboss-cli.sh -c
"/subsystem=datasources:read-resource-description"
Create datasource using the default db config asadmin create-jdbc-resource --connectionpoolid DerbyPool jdbc/test jboss-cli.sh -c "data-source add --name=test-ds --jndi-name=java\:jboss\/datasources\/test-ds --driver-name=h2 --connection-url=jdbc\:h2\:mem\:test;DB_CLOSE_DELAY\=-1"

jboss-cli.sh -c "data-source enable --name=test-ds"
Delete a datasource asadmin delete-jdbc-resource jdbc/test jboss-cli.sh -c "data-source remove --name=test-ds"
Batch processing based on file input asadmin multimode --file /tmp/glassfish-cli-commands.txt jboss-cli.sh -c "batch --file=/tmp/jboss-cli-commands.txt"

9/21/2012

How to Add System Properties to JBoss AS 7

Option 1, when the server is stopped, add to $JBOSS_HOME/standalone/configuration/standalone.xml, after the <extensions> element:

<system-properties>
    <property name="season" value="spring"/>
</system-properties>

Option 2, when the server is running, run jboss-cli.sh or jboss-cli.bat command to add system property. The property will be persisted to standalone.xml, so it has the same effect as option 1:
jboss-cli.sh -c "/system-property=buzz:add(value='This\ is\ buzz')"

Option 3, add -Dkey=val to standalone.sh command line:
standalone.sh -Dseason=spring

Option 4, pass a properties file containing desired properties to standalone.sh. This is especially convenient if you need a list of dynamic system properties.
standalone.sh -P /tmp/a.properties

Now after the server is up and running, how do you verify your system properties are present with correct value?
  • Test it with any deployed app that prints out the system properties. 
  • View $JBOSS_HOME/standalone/log/server.log, search for the system property name.  Only system properties specified in command line (option 3 & 4) are logged at server startup. 
  • View all system properties in admin console at http://localhost:9990.
    • Runtime tab | Server | Configuration | Environment Properties
  • To list all system properties present in standalone.xml (not including those specified in command line):
    jboss-cli.sh -c "/system-property=*:read-resource"

9/20/2012

JBoss AS7 Ant Tasks for Deploy and Undeploy

I want to use ant to control deploy and undeploy apps to JBoss AS 7 in a simple test. I know I could just have ant copy the WAR, jar, or EAR files to $JBOSS_HOME/standalone/deployments directory, but then I will have to wait till the app is fully initialized before accessing it. So I decided to write some simple ant targets using JBoss AS7 CLI commands.

<?xml version="1.0"?>
<project name="test" default="build" basedir=".">
    <property environment="env"/>
    <property name="jboss.home" value="${env.JBOSS_HOME}" />
    <property name="app.name" value="test.war" />
    <property name="app.path" value="${app.name}" />

    <presetdef name="jboss-cli">
      <java jar="${jboss.home}/jboss-modules.jar" fork="true" >
        <arg line="-mp ${jboss.home}/modules org.jboss.as.cli -c" />
      </java>
    </presetdef>

    <target name="deploy">
      <jboss-cli failonerror="true">
        <arg line="'deploy --force ${app.path}'" />
      </jboss-cli>
    </target>

    <target name="undeploy">
      <jboss-cli failonerror="true">
        <arg line="'undeploy ${app.name}'" />
      </jboss-cli>
    </target>

    <target name="restart">
      <jboss-cli failonerror="true">
        <arg line="':shutdown(restart=true)'" />
      </jboss-cli>
    </target>
</project>
The above deploy and undeploy is equivalent to the following jboss-cli commands:
jboss-cli.sh -c "deploy --force test.war"
jboss-cli.sh -c "undeploy test.war"