我有一个简单的 finagle 服务,如下所示:
import com.twitter.finagle.{Http,Service}
import com.twitter.util.{Await, Future}
import java.net.InetSocketAddress
import org.jboss.netty.handler.codec.http._
import org.jboss.netty.buffer.ChannelBuffers.copiedBuffer
import org.jboss.netty.util.CharsetUtil
import net.liftweb.json._
import com.twitter.finagle.http.{Request,Response,RichHttp}
import com.twitter.finagle.builder.ServerBuilder
case class Add(num1:Int, num2:Int)
case class Result(result:Int)
trait Transformer[R1,T,U,R2] {
def extractPayload(r:R1):T
def responsePayload(u:U):R2
}
object Transformer{
implicit object AddPayloadTransformer
extends Transformer[HttpRequest,Add,Result,HttpResponse]{
implicit val formats = DefaultFormats
def extractPayload(r:HttpRequest):Add = {
val body = r.getContent
val length = body.readableBytes()
val bytes = new Array[Byte](length)
body.getBytes(body.readerIndex(),bytes,0,length)
val bodyStr = new String(bytes)
parse(bodyStr).extract[Add]
}
def responsePayload(u:Result):HttpResponse = {
val result = u.result
val response = Response(HttpVersion.HTTP_1_1,
HttpResponseStatus.OK)
response.setContentTypeJson()
response.setContentString(
s"""
{
"result": $result
}
""")
response.httpResponse
}
}
}
object AdditionServer extends App with Transformer[HttpRequest,Add,Result,HttpResponse]{
private def add(add:Add):Result = Result(add.num1 + add.num2)
private def additionHandler(req:HttpRequest): HttpResponse = {
val addPayload = extractPayload(req)
val result = add(addPayload)
responsePayload(result)
}
val service = new Service[HttpRequest,HttpResponse] {
def apply(req: HttpRequest): Future[HttpResponse] =
{
Future.value(additionHandler(req))
}
}
val server = Http.serve(":8080", service)
Await.ready(server)
println("Started service")
}
当我尝试编译它时,出现以下异常:
AdditionService.scala:49: object creation impossible, since:
[error] it has 2 unimplemented members.
[error] /** As seen from object AdditionServer, the missing signatures are as follows.
[error] * For convenience, these are usable as stub implementations.
[error] */
[error] def extractPayload(r: org.jboss.netty.handler.codec.http.HttpRequest): Add = ???
[error] def responsePayload(u: Result): org.jboss.netty.handler.codec.http.HttpResponse = ???
[error] object AdditionServer extends App with Transformer[HttpRequest,Add,Result,HttpResponse]{
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
据我所知,我正在使用适当的签名来实现这些特征。
有人可以指出这里可能出了什么问题。
谢谢