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.
5. create the jar with the jboss-service.xml file in META-INF folder and change the jar extension to '.sar'
0 comments:
Post a Comment