Wednesday, July 14, 2010

Jboss service sample

Jboss server as the some features like writing the service which will give permission to access the start, stop ..etc methods of mbean, Where we can write our code which are required on jboss start or shutdown....

Following steps we have to follow for writing the jboss service(.sar).

1. Created the interface which will extend Service interface of jboss and name of the interface should have 'MBean' at the tail-end of interface name.

import org.jboss.system.Service;

public interface SampleServiceMBean extends Service {


2. Write a class which implements 'SampleServiceMBean' and 'Runnable' interface.

public class SampleService implements JmsTablesDropMBean, Runnable {}

3. now the SampleService class will have all the moethods of the service interface here we can write our code.

public void start() throws Exception {
runner = new Thread(this);
running = true;
runner.start();"Sample Service start() ");

public void stop() {
if (runner != null) {
running = false;
runner = null;
truncateJmsMessageData();"Sample Service stop() ");

protected Properties getJndiProps() {
// TODO: later init these from tradescope configuration
Properties result = new Properties();
result.put("java.naming.factory.initial", "org.jnp.interfaces.NamingContextFactory");
result.put("java.naming.provider.url", "jnp://localhost:1099");
result.put("java.naming.factory.url.pkgs", "org.jboss.naming:org.jnp.interfaces");
return result;

public void create() throws Exception {
InitialContext ic = new InitialContext(getJndiProps());
server = (RMIAdaptor) ic.lookup("jmx/rmi/RMIAdaptor");"Connected to server: " + server);


public void destroy() {
truncateJmsMessageData();"Sample Service destroy() ");


public void run() {
while (running) {
try {
// do nothing
} catch (Throwable t) {
log.warn("Exception caught", t);
try {
} catch (Exception e) {
// Don't care

public static void main(String[] args) throws Exception {
SampleService jmsTablesDrop = new SampleService();


4. write jboss-service.xml file for the service declaration.


5. create the jar with the jboss-service.xml file in META-INF folder and change the jar extension to '.sar'

Changing Default HSQLDB to User Database in Jboss for JMS

As we know jboss uses HSQLDB for the jms persistence to modify this to persist the JMS messages to user Database like mysql,oracle..e.t Following changes as to be made in jboss.

1. Delete the hsqldb-ds.xml from JBOSS_HOME/server/default/deploy folder.

2. Copy the respective database related ds file from JBOSS_HOME/docs/examples/jca/*-ds.xml file to deploy folder of default.

3. Change the jndi-name in *-ds.xml file to "DefaultDS".

4. Delete hsqldb-jdbc2-service.xml file from JBOSS_HOME/server/default/jms folder.

5. Copy the respective database persitence manager service xml file *-jdbc2-service.xml from JBOSS_HOME//docs/examples/jms to JBOSS_HOME/server/default/jms folder.

6. Change the jndi name in the *-jdbc2-service.xml to "DefaultDS" .

7. Rename the hsqldb-jdbc-state-service.xml to respective database name *-jdbc-state-service.xml, its optional you can keep the file as it is.

8. Copy the respective database connector jar file to /JBOSS_HOME/server/default/lib folder.

Now the configuration is modified for the jms persistence to user database and data will persist to jms_message table only when the huge number of jms are generated and its a temporary storage once the jms message is consumed it will deleted automatically from the jms_message table.

HsqlDB change zip file.
Source file