I use Scala with Simple Build Tool (SBT). Have created a project and copied the JForex libs in the lib folder. I have after that writte in the src/main/scala folder a simple file called SForex.scala:
import com.dukascopy.api.Instrument
import com.dukascopy.api.system.IClient
import com.dukascopy.api.system.ClientFactory
import com.dukascopy.api.system.ISystemListener
import java.util.HashSet
import org.slf4j.LoggerFactory
object SForex {
def main(args: Array[String]) {
// Arguments
var username = "XXX"
var password = "XXX"
// Initialize the logger.
val log = LoggerFactory.getLogger(getClass)
org.apache.log4j.BasicConfigurator.configure
// Get default instance of Dukascopy IClient
val client = ClientFactory.getDefaultInstance
// Create system listener
val systemListener = new ISystemListener {
var maxReconnects = 3
override def onConnect {
log.info("[network] connected on dukascopy server")
println("[network] connected on dukascopy server")
maxReconnects = 3
}
override def onDisconnect {
log.warn("[network] disconnected from dukascopy server")
println("[network] disconnected from dukascopy server")
if (maxReconnects > 0) {
client.reconnect
maxReconnects = maxReconnects - 1
} else {
try {
Thread.sleep(10000)
} catch {
case e:InterruptedException => //ignore
}
try {
client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", username, password)
} catch {
case e:Exception => {
log.error(e.getMessage(), e)
println(e.getMessage(), e)
}
}
}
}
override def onStart(processId: Long) {
log.info("[strategy] started process id: " + processId)
println("[strategy] started process id: " + processId)
}
override def onStop(processId: Long) {
log.info("[strategy] stopped process id: " + processId)
println("[strategy] stopped process id: " + processId)
if (client.getStartedStrategies.size == 0)
exit(0)
}
}
// Set system listener
client.setSystemListener(systemListener)
// Connect to Dukascopy
log.info("[network] connecting to dukascopy server...")
println("[network] connecting to dukascopy server...")
client.connect("https://www.dukascopy.com/client/demo/jclient/jforex.jnlp", username, password)
// Wait of Dukascopy response
var maxSeconds = 10
while (maxSeconds > 0 && !client.isConnected) {
Thread.sleep(1000)
maxSeconds = maxSeconds - 1
}
// If not connected, exit
if (!client.isConnected()) {
log.error("[network] failed to connect dukascopy servers")
println("[network] failed to connect dukascopy servers")
exit(1)
}
//now it's running
exit(0)
}
}
If the code passed the last row resp. comment "now it's running", the exit(0) function don't stop the whole application (hang up). I have read in this topic
viewtopic.php?f=5&t=2672&view=previous that ISystemListener use own thread. It's possible kill this thread? And if yes, can the console application after that shutdown normal or should I kill additional another thread?
In another words, I need an advice how a console Application based on JForex API can shutdown normal without Ctrl+C
