finagle 服务中未实现的特征
Traits not implemented in finagle service
我有一个简单的 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
据我所知,我正在使用正确的签名实现特征。
谁能指出这里可能出了什么问题。
谢谢
AdditionServer
是扩展 Transformer
的对象。在你的代码中,这个对象既没有定义 extractPayload
也没有定义 responsePayload
因此你得到了编译错误。
您应该将您的方法定义从 trait 伴随对象移动到 AdditionServer
object AdditionServer extends App with
Transformer[HttpRequest,Add,Result,HttpResponse] {
import Transformer._
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
}
}
如果您希望在伴随对象中定义您的方法,您可以从 AdditionServer
:
中引用它们
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]{
def extractPayload(r:HttpRequest):Add = Transformer.extractPayload(r)
def responsePayload(u:Result):HttpResponse = Transformer.responsePayload(u)
...
...
我有一个简单的 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
据我所知,我正在使用正确的签名实现特征。
谁能指出这里可能出了什么问题。
谢谢
AdditionServer
是扩展 Transformer
的对象。在你的代码中,这个对象既没有定义 extractPayload
也没有定义 responsePayload
因此你得到了编译错误。
您应该将您的方法定义从 trait 伴随对象移动到 AdditionServer
object AdditionServer extends App with
Transformer[HttpRequest,Add,Result,HttpResponse] {
import Transformer._
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
}
}
如果您希望在伴随对象中定义您的方法,您可以从 AdditionServer
:
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]{
def extractPayload(r:HttpRequest):Add = Transformer.extractPayload(r)
def responsePayload(u:Result):HttpResponse = Transformer.responsePayload(u)
...
...