2016-09-14 76 views
0

首先,我對scala比較陌生。Scala模式匹配總是默認情況下

考慮以下簡單的類NullLogService,它將結果記錄到控制檯。

package services 

import akka.actor._ 
import com.amazonaws.services.simpleemail.model.SendRawEmailResult 
import model.Email 

import scala.util.Try 

object NullLogService { 
    case class Log(email: Email, amazonResult: Try[_]) 
    case class LogError(email: Email, amazonException: Try[_]) 
} 

class NullLogService extends Actor { 
    import NullLogService._ 

    def receive = { 
    case Log(email, amazonResult) => println("Email to: " + email.to + ". AmazonResultSucess:" + amazonResult.isSuccess.toString + ". AmazonResult: " + amazonResult.get.toString) 
    case LogError(email, amazonException) => println("Error: Email to: " + email.to + ". AmazonException:" + amazonException.get.toString) 
    case [email protected]_ => println("Default case: "+default.toString) 
    } 
} 

它總是打印Default case: Log(TO:[email protected],Success({MessageId: Fake_Sent_OK}))

我不知道發生了什麼,因爲類型相同(Log(Email,Try(_)))!它應該去「登錄」的情況下,但總是下降到默認情況下!

讓我瘋狂。

代碼涉及

主類:

import java.io.File 
import java.util.concurrent.TimeUnit 

import akka.actor.SupervisorStrategy.Resume 
import akka.actor._ 
import akka.routing.RoundRobinPool 
import akka.util.Timeout 
import com.thenewmotion.akka.rabbitmq._ 
import com.typesafe.config.{ConfigFactory, ConfigParseOptions} 
import model.Email 
import services.EmailService.EmailInfo 
import services.{EmailService, LogService, NullLogService} 
import utils.StringUtils 

import scala.concurrent.duration.Duration 

object Main extends App { 
    implicit val system = ActorSystem() 

    val emailServiceRef: ActorRef = system.actorOf(RoundRobinPool(rate).withSupervisorStrategy(supervisorStrategy).props(Props(new EmailService)), "emailWorkerPool") 
    val logServiceRef = system.actorOf(RoundRobinPool(1).props(Props(new NullLogService)), "logWorker") 
    val email = new Email(stringMap.apply("USEREMAIL"), stringMap.apply("REPLYTO"), stringMap.apply("SUBJECT"), stringMap.apply("BODY"), unsubscribeURL) 
    emailServiceRef ! EmailInfo(email, logServiceRef) 
} 

EmailService:

package services 

import akka.actor._ 
import com.amazonaws.services.simpleemail.model.SendRawEmailResult 
import model.Email 
import services.LogService.{Log, LogError} 

object EmailService { 
    case class EmailInfo(email: Email, logServiceRef: ActorRef) 
} 

class EmailService extends Actor { 
    import EmailService._ 

    def receive = { 
    case EmailInfo(email, logServiceRef) =>  
     val emailResult = new SendRawEmailResult 
     emailResult.setMessageId("Fake_Sent_OK") 
     val amazonResult = Try(emailResult) 
     logServiceRef ! Log(email, amazonResult) 
} 
} 
+2

它適用於我:'Email to:[email protected]。 AmazonResultSucess:真。 AmazonResult:電子郵件結果' 我唯一能想到的是在發送消息時是否使用同一個類'NullLogService.Log'。 – Jatin

+1

我不得不同意賈丁。它也適用於我。你在使用ActorSystem嗎?是否還有其他代碼參與? – siebenschlaefer

+0

我改變了我的NullLogService來打印默認情況。它打印日誌(到:電子郵件@ email.com,成功({MessageId:Fake_Sent_OK}))'。案例是「日誌」,類型是電子郵件。另外,我將第二個參數的類型更改爲「Try(_)」。還是行不通。我不知道該怎麼辦:C –

回答

1

大概你沒有刪除LogService.LogLogService.LogError,否則你會得到一個編譯錯誤EmailService。在這種情況下NullLogService不應該定義自己的消息,但使用LogService的那些:

package services 

import akka.actor._ 
import com.amazonaws.services.simpleemail.model.SendRawEmailResult 
import model.Email 

import scala.util.Try 
import LogService.{Log, LogError} 

// no object NullLogService unless you need it for something else 

class NullLogService extends Actor { 
    def receive = { 
    case Log(email, amazonResult) => println("Email to: " + email.to + ". AmazonResultSucess:" + amazonResult.isSuccess.toString + ". AmazonResult: " + amazonResult.get.toString) 
    case LogError(email, amazonException) => println("Error: Email to: " + email.to + ". AmazonException:" + amazonException.get.toString) 
    case [email protected]_ => println("Default case: "+default.toString) 
    } 
} 

然後你就可以使用相同的協議(比如,NullLogServiceSlf4jLogServiceInMemoryLogService)不改變不同的服務之間切換每次進口。

+0

我打算接受我的回答,但要感謝您的建議,我接受您的建議。謝謝。代碼現在更清潔 –

0

實測值的誤差。我不得不改變我正在導入的記錄器類。
我現在使用NullLogService,而不是LogService。

package services 

import akka.actor._ 
import com.amazonaws.services.simpleemail.model.SendRawEmailResult 
import model.Email 
//Change this --> import services.LogService.{Log, LogError} <--- 
//To the class you are using --v 
import services.NullLogService.{Log, LogError} 


object EmailService { 
    case class EmailInfo(email: Email, logServiceRef: ActorRef) 
} 

class EmailService extends Actor { 
    import EmailService._ 

    def receive = { 
    case EmailInfo(email, logServiceRef) =>  
     val emailResult = new SendRawEmailResult 
     emailResult.setMessageId("Fake_Sent_OK") 
     val amazonResult = Try(emailResult) 
     logServiceRef ! Log(email, amazonResult) 
} 
} 

這是奇怪的,因爲沒有運行時錯誤(如「NullLogService不導入」)