How do I keep a piece of code in Java always running on ubuntu/centOS?

149 views Asked by At

I have written some Java code (using the interactive brokers API) to poll for futures pricing every 50ms and when it's in a situation I like, it will purchase or sell X contracts, etc.

Timer t = new Timer( );
t.scheduleAtFixedRate(new TimerTask() {

    @Override
    public void run() {
      // get pricing, look for situational stuff
    }
}, 0, 50);

However, I'm lost as to how to write the code to properly keep the program always running in the background. If it matters, I'm developing this on OS X and production environment can be either ubuntu or centOS

1

There are 1 answers

3
brian On BEST ANSWER

A java program will run until the last non daemon thread stops or you call one of the stop methods (like System.exit(0)). Since your timer is non daemon, it will run forever. If you have a window, then that also has a thread.

Since you've presumably connected to IB, EReader creates a non-daemon thread to read from the socket. You have to disconnect in order to stop this thread.

A better choice would be to subscribe to market data and do something when new data is received. Note that 50ms is too fast to ask for data since IB only updates a few times per second anyway.