I am trying my hand at scala+Akka and I am trying to figure out fault tolerance. I have an actor which receives message from a supervisor and inserts data into the DB. The Supervisor restarts the actor when it experiences a fault.
I am changing the connection string in the postRestart() in case there are connectivity issues to the DB. Now whenever there is a connectivity issue with one DB, the actor restarts and starts inserting data into another DB.
Is this a good enough approach? What is the recommended approach?
Supervisor:
class SocialSupervisor extends Actor {
override val supervisorStrategy=OneForOneStrategy(loggingEnabled = false){
case (e:Exception)=>Restart
}
val post_ref=context.actorOf(Props[Post])
def receive={
case Get_Feed(feed)=>{
//get data from feed
post_ref!Post_Message(posted_by,post)
}
}
}
Actor:
class Post extends Actor{
val config1=ConfigFactory.load()
var config=config1.getConfig("MyApp.db")
override def postRestart(reason: Throwable) {
config=config1.getConfig("MyApp.backup_db")
super.postRestart(reason)
}
def insert_data(commented_by:String,comment:String){
val connection_string=config.getString("url")
val username=config.getString("username")
val password=config.getString("password")
//DB operations
}
def receive={
case Post_Message(posted_by,message)=>{
insert_data(posted_by, message)
}
}
}