akka streams用于sourcequeuewithcomplete的covarience

uajslkp6  于 2021-06-07  发布在  Kafka
关注(0)|答案(1)|浏览(423)

我有一个actor,它是在一个supervisorator中创建的,这个actor负责将它得到的消息推送到流中。演员来了:

class KafkaPublisher[T <: KafkaMessage: ClassTag] extends Actor {

  implicit val system = context.system
  val log = Logging(system, this.getClass.getName)

  override final def receive = {
    case ProducerStreamActivated(_, stream: SourceQueueWithComplete[T]) =>
      log.info(s"Activated stream for Kafka Producer with ActorName >> ${self.path.name} << ActorPath >> ${self.path} <<")
      context.become(active(stream))

    case other =>
      log.warning("KafkaPublisher got some unknown message while producing: " + other)
  }

  def active(stream: SourceQueueWithComplete[T]): Receive = {
    case msg: T =>
      log.info(s"Got Message >> $msg << Pushing it to stream $stream")
      stream.offer(msg)

    case other =>
      log.warning("KafkaPublisher got the unknown message while producing: " + other)
  }
}
object KafkaPublisher {

  def props[T <: KafkaMessage: ClassTag] =
    Props(new KafkaPublisher[T])
}

可以看到,这个参与者在一个通用消息类型上工作,因为supervisorator负责创建这个通用参与者的具体示例。

trait Event
object Event {
  case class ProducerStreamActivated[T <: KafkaMessage](kafkaTopic: String, stream: SourceQueueWithComplete[T]) extends Event
}

trait KafkaMessage
object KafkaMessage {

  case class DefaultMessage(message: String, timestamp: DateTime) extends KafkaMessage {
    def this() = this("DEFAULT-EMPTY-MESSAGE", DateTime.now(DateTimeZone.UTC))
  }

  case class DefaultMessageBundle(messages: Seq[DefaultMessage], timeStamp: DateTime) extends KafkaMessage {
    def this() = this(Seq.empty, DateTime.now(DateTimeZone.UTC))
  }
}

显然,我得到了以下错误:

Error:(17, 45) pattern type is incompatible with expected type;
 found   : akka.stream.scaladsl.SourceQueueWithComplete[T]
 required: akka.stream.scaladsl.SourceQueueWithComplete[com.eon.pm.messages.KafkaMessage]
Note: T <: com.my.project.messages.KafkaMessage, but trait SourceQueueWithComplete is invariant in type T.
You may wish to define T as +T instead. (SLS 4.5)
    case ProducerStreamActivated(_, stream: SourceQueueWithComplete[T]) =>

sourcequeuewithcomplete不是协变的。我怎样才能减轻这一点?有什么建议吗?

7qhs6swi

7qhs6swi1#

比如:

class KafkaPublisher extends Actor {

  implicit val system = context.system
  val log = Logging(system, this.getClass.getName)

  override final def receive = {
    case ProducerStreamActivated(_, stream) =>
      log.info(s"Activated stream for Kafka Producer with ActorName >> ${self.path.name} << ActorPath >> ${self.path} <<")
      context.become(active(stream))

    case other =>
      log.warning("KafkaPublisher got some unknown message while producing: " + other)
  }

  def active(stream: SourceQueueWithComplete[KafkaMessage]): Receive = {
    case msg: DefaultMessage =>
      log.info(s"Got Message >> $msg << Pushing it to stream $stream")
      stream.offer(msg)

    case msg: DefaultMessageBundle =>
      log.info(s"Got Message >> $msg << Pushing it to stream $stream")
      stream.offer(msg)

    case other =>
      log.warning("KafkaPublisher got the unknown message while producing: " + other)
  }
}
object KafkaPublisher {

  def props = Props(new KafkaPublisher)
}

trait Event
object Event {
  case class ProducerStreamActivated(kafkaTopic: String, stream: SourceQueueWithComplete[KafkaMessage]) extends Event
}

trait KafkaMessage
object KafkaMessage {

  case class DefaultMessage(message: String, timestamp: DateTime) extends KafkaMessage {
    def this() = this("DEFAULT-EMPTY-MESSAGE", DateTime.now(DateTimeZone.UTC))
  }

  case class DefaultMessageBundle(messages: Seq[DefaultMessage], timeStamp: DateTime) extends KafkaMessage {
    def this() = this(Seq.empty, DateTime.now(DateTimeZone.UTC))
  }
}

相关问题