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. :-)

9 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. We are consequently happy this particular internet point works and your post truly taught me to be. Will take a person on that home suggestions an individual

    RS Gold
    Buy RS Gold
    Cheap RS Gold
    Buy Cheap RS Gold
    RSGold
    Runescape Gold for Sale

    ReplyDelete
  3. We had done a great number of orders with our customers for buying Runescape Gold fast and have hundreds of orders everyday .We are sure that you are impossible to get such an excellent service in buying Cheap Runescape Gold as the same as we Buy Runescape Gold.

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

    RS Gold
    Cheap diablo 3 gold

    ReplyDelete
  5. 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
  6. 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
  7. 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
  8. Nice article, thanks for the information. It's very complete information. I will bookmark for next reference
    jaring futsal | jaring golf | jaring pengaman proyek |
    jaring pengaman bangunan | jaring pengaman gedung
    http://www.jual-jaring.blogspot.com/
    http://www.agen-jaring.blogspot.com/
    http://www.pancasamudera-safetynet.blogspot.com/
    http://www.toko-jaring.blogspot.com/
    http://www.pusat-jaring.blogspot.com/
    http://jualjaringpengaman.blogspot.com/
    https://pancasamudera.wordpress.com/
    https://pasangjaringfutsal.wordpress.com/
    https://jualtambangmurah.wordpress.com/
    https://tokojaring.wordpress.com/
    https://jualjaringfutsal.wordpress.com/
    https://jaringfutsal.wordpress.com/


    ReplyDelete