我目前正在关注 http://doc.akka.io/docs/akka/snapshot/scala/camel.html 中的 akka-camel 集成示例.
作为传输,我使用 jetty:http,比如
class Orders extends Actor with Producer {
def endpointUri = "jetty:http://localhost:8877/"
}
端点相同
class MyEndpoint extends Consumer {
def endpointUri = "jetty:http://0.0.0.0:8877/"
def receive = {
case msg: CamelMessage => { println("here", msg ); sender ! "ok"}
case _ => { println("somewhere else") }
}
}
发送简单的文本消息时一切正常
val sys = ActorSystem("some-system")
val orders = sys.actorOf(Props[Orders])
val endp = sys.actorOf(Props[MyEndpoint])
orders ? "hello"
但是发送案例类时不起作用
case class B(id:String)
orders ? B("hello")
输出表明 TypeConversion 出错
akka.camel.AkkaCamelException: No type converter available to convert from type: de.spring.cases.infrastructure.SerializationSpec.B to the required type: java.io.InputStream with value B(hello) ...
原因:
org.apache.camel.NoTypeConversionAvailableException: No type converter available to convert from type: de.spring.cases.infrastructure.SerializationSpec.B to the required type: java.io.InputStream with value B(hello) at org.apache.camel.impl.converter.BaseTypeConverterRegistry.mandatoryConvertTo(BaseTypeConverterRegistry.java:169) at org.apache.camel.component.jetty.JettyHttpProducer.createHttpExchange(JettyHttpProducer.java:135) at org.apache.camel.component.jetty.JettyHttpProducer.process(JettyHttpProducer.java:75) at org.apache.camel.util.AsyncProcessorHelper.process(AsyncProcessorHelper.java:73) at org.apache.camel.processor.SendProcessor$2.doInAsyncProducer(SendProcessor.java:122) at org.apache.camel.impl.ProducerCache.doInAsyncProducer(ProducerCache.java:298) at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:117) at akka.camel.ProducerSupport$ProducerChild.produce(Producer.scala:137) at akka.camel.ProducerSupport$ProducerChild$$anonfun$receive$1.applyOrElse(Producer.scala:111) ... 9 more
在 Object 和 Array[Byte] 之间使用显式(自己的)序列化/反序列化时,一切顺利。
我阅读了有关 Camels TypeConversions 的文章,想知道是否有一种内部方法可以将消息从可序列化对象转码/转码到可序列化对象。看到接收方,直接在接收函数中使用“正常”scala 模式匹配来使用消息会很棒。无需使用额外字段和手动转码进行任何额外调度。
有什么建议吗?
最佳答案
来自Camel code ,正文内容需要是一个 InputStream。所以你必须将 B Object 转换为 InputStream 并将其发送给 Producer。在 Java 中是这样完成的 this
关于java - akka camel 将案例类作为消息发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22400064/