I am trying to create a Launcher program for my application and was going through the App
trait and main
method to decide which one to use. The only difference that I found between both is:
(1) Threaded code that references the object will block until static initialization is complete. However, because the entire execution of an object extending Application takes place during static initialization, concurrent code will always deadlock if it must synchronize with the enclosing object.
In my case I have a Launcher
which basically initialize a Kafka
loader object which keeps on running kafka
poll. Below is my Launcher
trait:
trait Launcher extends LazyLogging {
val config: Config
val actorSystem: ActorSystem
val sourceMessagesType = config.getString("app.source.dataType")
val targetTopic = config.getString("app.kafka.targetTopic")
val targetTopicPartitions =config.getInt("app.kafka.targetTopicPartitions")
val routingManager = HashedPartitionRoutingManager(targetTopic, targetTopicPartitions)
logger.info(s"Initializing MorpheusStreamDataLoader for $sourceMessagesType type")
sourceMessagesType.toLowerCase match {
case "json" => JSonDataLoader(config, routingManager)
case "avro" => AvroDataLoader(config, routingManager)
case _ => throw new IllegalArgumentException
s"Messages of type ${sourceMessagesType.toLowerCase} are not supported.\n"
}
}
Now to launch my application I was trying to find which is best to use, App
or main
method. However main
method implementation doesn't work at all:
object ClientLauncher extends Launcher {
def main(args: Array[String]): Unit = {
override val config=ConfigFactory.load(args(0))
override val actorSystem: ActorSystem=ActorSystem("ClientActorSystem")
}
}
When I do this I get error on override
modifier that override modifier is not allowed here
. However if I use App
trait it doesn't gives me any compile time error.
object ClientLauncher extends App with Launcher {
override val config=ConfigFactory.load(args(0))
override val actorSystem: ActorSystem=ActorSystem("ClientActorSystem")
}
The information that I got after reading couple of posts about App
trait and main
was that there is not difference other than the delayed initialization that happens when we use App
trait. Why override
doesn't works for main
method and works for App
? and what is the best way for me to launch my application?