如何使用Akka将使用Scala的Play 2与Camel集成?

时间:2014-06-12 17:15:52

标签: scala playframework apache-camel akka

是否有人在Scala中整合了Play2 - Akka - Camel?

我收到java.lang.NoClassDefFoundError: Could not initialize class play.api.libs.concurrent.Execution

在初始化上下文时,似乎与akka-camel组件有关。

我正试图将我的演员挂钩到全局对象

import play.api.mvc._
import play.api._
import util.MailSender
import akka.actor.{Actor, Props}
import play.api.libs.concurrent.Akka
import play.api.GlobalSettings
import play.api.templates.Html
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import akka.camel.Consumer

/**
 * Application global object, used here to schedule jobs on application start-up.
 */
object Global extends GlobalSettings {

  override def onStart(application: play.api.Application) {
        SampleSender.createActor
}
}

Camel Actors初始化程序

import akka.actor.Props
import scala.concurrent.duration._
import akka.camel.CamelExtension
import akka.actor.ActorSystem
import actor.HttpExample.HttpTransformer
import actor.HttpExample.HttpProducer
import actor.HttpExample.HttpConsumer
import play.libs.Akka

object SampleSender {
 def createActor: Unit = {
    println("--camel--Start--")
        val httpTransformer = Akka.system().actorOf(Props[HttpTransformer])
        val httpProducer = Akka.system().actorOf(Props(classOf[HttpProducer], httpTransformer))
        val httpConsumer = Akka.system().actorOf(Props(classOf[HttpConsumer], httpProducer))

    println("--camel--End--")
  } 
}

代理商

 package actor

import play.libs.Akka
import akka.actor.Props
import akka.actor.Actor
import akka.actor.ActorRef
import akka.actor.ActorSystem
import akka.actor.Props
import akka.actor.Status.Failure
import akka.actor.actorRef2Scala
import akka.camel.CamelMessage
import akka.camel.Consumer
import akka.camel.Producer


object HttpExample {

  class HttpConsumer(producer: ActorRef) extends Consumer {
    def endpointUri = "jetty:http://0.0.0.0:8875/"

    def receive = {
      case msg => producer forward msg
    }
  }

  class HttpProducer(transformer: ActorRef) extends Actor with Producer {
    // bridgeEndpoint=true makes the producer ignore the Exchange.HTTP_URI header, 
    // and use the endpoint's URI for request
    def endpointUri = "jetty://http://akka.io/?bridgeEndpoint=true"

    // before producing messages to endpoints, producer actors can pre-process
    // them by overriding the transformOutgoingMessage method  
    override def transformOutgoingMessage(msg: Any) = msg match {
      case camelMsg: CamelMessage => camelMsg.copy(headers =
        camelMsg.headers(Set(Exchange.HTTP_PATH)))
    }

    // instead of replying to the initial sender(), producer actors can implement custom
    // response processing by overriding the routeResponse method
    override def routeResponse(msg: Any) { transformer forward msg }
  }

  class HttpTransformer extends Actor {
    def receive = {
      case msg: CamelMessage =>
        sender() ! (msg.mapBody { body: Array[Byte] =>
          new String(body).replaceAll("Akka ", "AKKA ")
        })
      case msg: Failure => sender() ! msg
    }
  }

}

这是我的依赖 这是使用play 2.2.3的版本

"com.typesafe.akka" % "akka-camel_2.10" % "2.3.3",
  "org.apache.camel" % "camel-jetty" % "2.10.3",
  "org.apache.camel" % "camel-quartz" % "2.10.3",
  "org.slf4j" % "slf4j-api" % "1.7.2",
  "ch.qos.logback" % "logback-classic" % "1.0.7"

以下是一些日志

[info] play - Application started (Dev)
[info] a.e.s.Slf4jLogger - Slf4jLogger started
[error] p.nettyException - Exception caught in Netty
java.lang.NoClassDefFoundError: Could not initialize class play.api.libs.concurrent.Execution$
        at play.core.server.netty.PlayDefaultUpstreamHandler.handleAction$1(PlayDefaultUpstreamHandler.scala:197) ~[play_2.10.jar:2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler.messageReceived(PlayDefaultUpstreamHandler.scala:170) ~[play_2.10.jar:2.2.3]
        at com.typesafe.netty.http.pipelining.HttpPipeliningHandler.messageReceived(HttpPipeliningHandler.java:62) ~[netty-http-pipelining.jar:n
a]
        at org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108) ~[netty.jar:na]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) ~[netty.jar:na]
        at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) ~[netty.jar:na]
[error] p.nettyException - Exception caught in Netty
java.lang.NoSuchMethodError: akka.actor.ActorSystem.dispatcher()Lscala/concurrent/ExecutionContext;
        at play.core.Invoker$.<init>(Invoker.scala:24) ~[play_2.10.jar:2.2.3]
        at play.core.Invoker$.<clinit>(Invoker.scala) ~[play_2.10.jar:2.2.3]
        at play.api.libs.concurrent.Execution$Implicits$.defaultContext$lzycompute(Execution.scala:7) ~[play_2.10.jar:2.2.3]
        at play.api.libs.concurrent.Execution$Implicits$.defaultContext(Execution.scala:6) ~[play_2.10.jar:2.2.3]
        at play.api.libs.concurrent.Execution$.<init>(Execution.scala:10) ~[play_2.10.jar:2.2.3]
        at play.api.libs.concurrent.Execution$.<clinit>(Execution.scala) ~[play_2.10.jar:2.2.3]
[error] p.nettyException - Exception caught in Netty
java.lang.NoClassDefFoundError: Could not initialize class play.api.libs.concurrent.Execution$
        at play.core.server.netty.PlayDefaultUpstreamHandler.handleAction$1(PlayDefaultUpstreamHandler.scala:197) ~[play_2.10.jar:2.2.3]
        at play.core.server.netty.PlayDefaultUpstreamHandler.messageReceived(PlayDefaultUpstreamHandler.scala:170) ~[play_2.10.jar:2.2.3]
        at com.typesafe.netty.http.pipelining.HttpPipeliningHandler.messageReceived(HttpPipeliningHandler.java:62) ~[netty-http-pipelining.jar:n
a]
        at org.jboss.netty.handler.codec.http.HttpContentDecoder.messageReceived(HttpContentDecoder.java:108) ~[netty.jar:na]
        at org.jboss.netty.channel.Channels.fireMessageReceived(Channels.java:296) ~[netty.jar:na]
        at org.jboss.netty.handler.codec.frame.FrameDecoder.unfoldAndFireMessageReceived(FrameDecoder.java:459) ~[netty.jar:na]

1 个答案:

答案 0 :(得分:1)

我认为你有一个二元不兼容的问题。

Play 2.2.3正在使用Akka 2.2.0,但你使用的是akka-camel 2.3.3。

尝试降级到akka-camel 2.2.0。