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'


Post a Comment