Thursday, December 23, 2010

Creating a Daemon-style Eclipse "headless" Application

A daemon application is an application that does not exit immediately and does not wait for any input/interaction from the user. Instead, it is (usually) providing a network service or by being a server. Apache Tomcat servlet container is one such daemon.

To run  a daemon-style application in Eclipse runtime, it is tempting to just use the standard infinite loop:

import org.eclipse.equinox.app.IApplication;
...

public class Application implements IApplication {

@Override
public Object start(IApplicationContext context) throws Exception {
// ... start services here (in another thread) ...
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
break;
}
}
return IApplication.EXIT_OK;
}

While this technique works, I believe the better way is just to immediately return normally. And use Eclipse's -noExit launch argument to let Eclipse know that we're still doing things in the background.

So the start() method can be just like this:

@Override
public Object start(IApplicationContext context) throws Exception {
final ResourceSetImpl resourceSet = new ResourceSetImpl();
Resource dealerResource = resourceSet
.createResource(URI
.createFileURI("/home/ceefour/project/AbisPulsa/workspace/com.abispulsa.model/src/model/YahooDealer.xmi"));
dealerResource.load(null);
dealer = (YahooDealerImpl) dealerResource.getContents().get(0);

dealer.signon();

Resource catalogRes = resourceSet.createResource(URI.createFileURI("/home/ceefour/project/AbisPulsa/workspace/com.abispulsa.model/src/model/Catalog.xmi"));
catalogRes.load(null);
Catalog catalog = (Catalog) catalogRes.getContents().get(0);
dealer.setCatalog(catalog);

return IApplication.EXIT_OK;
}

Then edit the application's Launch Configuration and add the following to program arguments:

-noExit -consoleLog

You may also want to add the OSGi console (argument -console):

-noExit -consoleLog -console

Let's run 'em servers. :-)

6 comments:

  1. Great tip! Sometimes I find it's far simpler to just set osgi.noShutdown to true in my config.ini file, instead of relying on commandline options.

    ReplyDelete
  2. Very cool. I always enjoy the interviews. Thanks for having this blog

    RS Gold
    Cheap diablo 3 gold

    ReplyDelete
  3. I liked your article, I will share your article to everyone!!




    WoW gold|Diablo 3 Gold|RS Gold|Cheap Diablo 3 Gold

    ReplyDelete
  4. An advanced battle system lets you choose gw2 gold every cut, thrust, and swing in real time. Creative graphics plunge you into a world of adventure beauty. A massive fantasy world of warcraft based on over 75 years of books, movies, art, and music, developed in close cooperation with wow gold Conan Inc.

    ReplyDelete
  5. Your AoE-Fear will be highly effective sufficient Buy Guild Wars 2 Gold, excellent to have people from stomps and to provide associates a few space, nah tbh this is the highly effective D3 Gems and 45sec would seem reasonable imo.

    ReplyDelete
  6. DC Universe Online OnlineSony rsgold 2007 phase has begun registration to participate in the Legends PvP Tournament of the popular DC Universe Online for PC and PlayStation 3, in which heroes and villains from across Europe 07 runescape gold played a place to attend the event SOE Live from Las Vegas , where he held the great final.Para participate in this tournament, players have until June 24 to increase its ranking of Legends PvP in DC Universe Online, considering that the tournament is designed for old school runescape money teams of two.

    ReplyDelete