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);
runner.setDaemon(true);
runner.setName("SampleService");
running = true;
runner.start();
log.info("Sample Service start() ");
}

public void stop() {
if (runner != null) {
running = false;
runner.interrupt();
runner = null;
}
truncateJmsMessageData();
log.info("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");
log.info("Connected to server: " + server);

}

public void destroy() {
truncateJmsMessageData();
log.info("Sample Service destroy() ");

}

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

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

}

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



jboss:type=Service,name=SystemProperties



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

Related Posts:

  • Spring + JPA + Hibernate Sample IntroductionStand-alone Spring application sample, Spring framework supports the persistence layer to be fully implemented through JPA. Simple example of configure the Spring needed components to perform persistence over sta… Read More
  • Whois domain detail by javaApache commmon org.apache.commons.net.whois.WhoisClient class provides the domain details. Following details: Domain Name: UTTESH.COM Registrar: GODADDY.COM, LLC Sponsoring Registrar IANA ID: 146 Whois Server: whois.godaddy… Read More
  • 6 Steps for contribute your jar/project to open source maven repository Prerequisites: 1. Sign Up at Sonatype. 2. Create your project in JIRA. 3. Create a Key Pair. 4. Create and Sign your Artifacts. 5. Deploy your Artifacts to your Staging Repository. 6. Promote your Repository Step #1: Sign Up… Read More
  • Java get system environment variable detailsGet System environment variable details. import java.util.Map; /** * * @author Uttesh Kumar T.H. */ public class FindJdk { public static void main(String[] args) { Map<String, String> env = System.gete… Read More
  • Command execution by Java Processorsample code to execute any command on operating system by using java processor. for example : To find jdk installed in system, We can run the command "where javac" for window and "where is javac" for linux the above command… Read More

0 comments:

Post a Comment