Category Archives: JBOSS

JBOSS AS 7 as service (JBoss AS 7 in Silent [background] mode)

In most of our cases we will be pushed to instances requiring to run JBoss AS 7 in silent mode in our LINUX environment. It is very simple to make our JBoss AS 7 to run in silent or in background mode. Let us come out of the mode as I am demonstrating a very simple idea here.


OS: RHEL 5.4 64 Bit


Installation of  JBOSS AS 7 under /opt/jboss

Installation of JDK under /usr/java/jdk1.6.0_30


To start and stop JBoss like a service. (Eg. service jboss start / service jboss stop)


1. Create file under /etc/profile.d/ with following contents

export JAVA_HOME=/usr/java/jdk1.6.0_30
export PATH=$JAVA_HOME/bin:$PATH

In this step, we are establishing the installed JDK to be used to run our JBoss instance.

2. Create file under /etc/profile.d/ with following contents

export JBOSS_HOME=/opt/jboss

In this step, we are setting JBOSS_HOME and make to come under path visibility. There is a reason to make ‘MODULEPATH=’ as empty. If not, JBoss AS 7 may not get loaded and throw  exceptions as it may not be able to load modules..

3. Create jboss file under /etc/init.d/jboss with following contents

# Provides: jboss
# Required-Start: $local_fs $remote_fs $network $syslog
# Required-Stop: $local_fs $remote_fs $network $syslog
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# Short-Description: Start/Stop JBoss AS v7.0.0
#source some script files in order to set and export environmental variables
#as well as add the appropriate executables to $PATH
[ -r /etc/profile.d/ ] && . /etc/profile.d/
[ -r /etc/profile.d/ ] && . /etc/profile.d/

echo “Starting JBoss 7”
sh /etc/init.d/iptables save
sh /etc/init.d/iptables stop
sh ${JBOSS_HOME}/bin/ -b >/dev/null 2>/dev/null &

echo “Stopping JBoss 7”
sh ${JBOSS_HOME}/bin/ –connect command=:shutdown

# give stuff some time to stop before we restart
sleep 60
# protect against any services that can’t stop before we restart
su -l jboss -c ‘killall java’

case “$1” in
# echo “Starting JBoss AS 7.0.0”
# sudo -u jboss sh ${JBOSS_HOME}/bin/ -b > /dev/null
# echo “Stopping JBoss AS 7.0.0”
# sudo -u jboss sh ${JBOSS_HOME}/bin/ –connect command=:shutdown
# echo “Restarting JBoss AS 7.0.0”
echo “Usage: /etc/init.d/jboss {start|stop|restart}”
exit 1

exit 0

4. After creating these three files, execute the following command

# chmod a+x /etc/init.d/jboss

#chmod a+x /etc/profile.d/

#chmod a+x /etx/profile.d/

5. Now login as SU

# su

and execute

#service jboss start

This should start your jboss server in background.


Thats all, you made your JBoss AS 7 to run in silent (background) mode.


Datasource configuration setup for JBoss AS 7 with example of Postgresql

Our application always demands the datasource setup in the server side to manager all our database connections.

We need to understand the modules available in JBoss before even we are starting the setup. Modules folder is located under \JBOSS-HOME\modules under which you can see lot of packaged structure which starts like com, org, etc.. There we need to copy our desired jar files by creating or copying it under right hierarchy.

In my instance,

1. Deploy Driver / Module of DB

I copied my postgresql-8.4-701.jdbc3.jar file to c:\jboss\modules\org\postgresql\main and created module.xml with following content

<jboss:module:1.0″ name=”org.postgresql”>
<resource-root path=”postgresql-8.4-701.jdbc3.jar”/>
<module name=”javax.api”/>
<module name=”javax.transaction.api”/>

Then, try restarting the JBoss, you should be able to see the postgresql driver deployed and your postgresql-8.4-701.jdbc3.jar.index created in the same directory where you copied your jar file.

2. Configure DB Driver

Then after successful deployment of you driver module, its time to edit your standalone.xml or domain.xml by appending following piece of xml code to it,

<driver name=”org.postgresql” module=”org.postgresql”>

Copy this information under subsystem>datasources element. Now, it means that you have included the same driver for the standalone startup. This enables the user to add the datasource to his server. This can be achieved in may ways, thought admin console in web, or programatically by editing standalone.xml / domain.xml or by CLI.

I will explain how to add datasource programatically and test it in CLI and sample java code.(contact me if you need steps to deploy by other means).

3. Configure Datasource in standalone.xml / domain.xml

Edit your standalone.xml present in c:\jboss\standalone\configuration folder. There add following lines of code under subsystem>datasources element

<datasource jndi-name=”java:jboss/datasources/Test” pool-name=”java:jboss/datasources/Test_Pool” enabled=”true” jta=”true” use-java-context=”true” use-ccm=”true”>

Note: Make sure the datasource name has the prefix of java:\ or java:jboss\ or else, your datasource cannot be referenced.

Then with that, you are done on the configuration side. Start/restart your server where you can notice in console that the datasource is started.

4. Testing Datasource

4.1 Testing the datsource in CLI

Connect your CLI to your local server by the following code

You are disconnected at the moment. Type ‘connect’ to connect to the server or ‘help’ for the list of supported commands.
[disconnected /] connect
Connected to standalone controller at localhost:9999
[standalone@localhost:9999 /]

Then execute the following code,

[standalone@localhost:9999 /] /subsystem=datasources/data-source=java\:jboss\/datasources\/Test:test-connection-in-pool
“outcome” => “success”,
“result” => [true]

4.2 Testing datasource in JAVA Code

Note: You cannot test datasource from a standalone java api. You should put that code / java api in side a war / web application and deploy in to the JBoss where you have deployed the data source and try it.

In my case, I have created a servlet called TestServlet and in GET method I have added the following lines of code

DataSource ds = null;

Context ctx = null;

try {
String strDSName = “java:jboss/datasources/Test”;
ctx = new InitialContext();
ds = (javax.sql.DataSource) ctx.lookup(strDSName);
resp.getWriter().print(“Success getting DS : ” + ds.getClass());
} catch (Exception e) {
resp.getWriter().print(“Error getting DS : ” + e);

Try calling the servlet by giving its context/servlet path which should print the success message.


Kousik Rajendran.