SpinGo / op-rabbit

The Opinionated RabbitMQ Library for Scala and Akka
Other
232 stars 73 forks source link

Error on implicit Marshaler #66

Closed mvbaffa closed 8 years ago

mvbaffa commented 8 years ago

I'am trying to reproduce you manual, so:

val rabbitAddress = new com.rabbitmq.client.Address("IP")
val connectionParams = new ConnectionParams(username = "user", password = "pwd", hosts = List(rabbitAddress))
val rabbitControl = actorSystem.actorOf(Props { new RabbitControl(connectionParams) })

It is connecting OK. But when I include the lines below I got a compiler error

case class Person(name: String, age: Int)
rabbitControl ! Message.queue(Person(name = "Ivanah Tinkle", age = 25), queue = "TEST_SCALA")
Error: Error:(14, 32) could not find implicit value for parameter marshaller: com.spingo.op_rabbit.RabbitMarshaller[TransactionDispatcher.Person]
rabbitControl ! Message.queue(Person(name = "Ivanah Tinkle", age = 25), queue = "TEST_SCALA")

Can you help me?

timcharper commented 8 years ago

I sure apologize if the manual isn't clear on this point! This is something that takes getting used to because implicits are a foreign concept to most (every?) Scala newcomers.

You need to define an implicit Marshalling strategy. If you rolled your own, it would look something like this:

implicit val personMarshaller = new RabbitMarshaller[Person] {
  val contentType = "text/plain"
  protected val contentEncoding = Some("UTF-8")
  private val utf8 = Charset.forName(encoding)
  def marshall(value: Person) =
    value.toString.getBytes(utf8) // just an example. Person.toString is not a good marshalling strategy.
}

The marshaller sets the appropriate message headers which are made unavailable to the unmarshaller.

You can also bring in bundled the play-json or json4s support, and then define a play-json format, bring it in to scope, etc.