Scala 通过代理参与者的DistributedPubSubMediator订阅不工作
我的同事和我一直对DistributedPubSubMediator直接或通过代理角色订阅/取消订阅的不同行为感到困惑。我们进行了一个测试,以显示下面不同的结果 根据我们的理解,ActorRef.forward应该传递给原始发送者,因此消息是直接发送给中介者还是通过代理参与者并不重要。即 为了解决这个问题,我们必须扩展DIstributedPubSubMediator类,并包括DIstributedPubSubMediator对象已经提供的逻辑。理想情况下,我们更愿意直接使用对象并还原代码 这好像是一个错误。有人知道这种不寻常行为的根本原因吗?请帮忙 [2013年10月22日]测试根据罗兰的回答(谢谢)进行更新,并在SubscriberAck和UnscriberAck上添加了expectMsgType。我们现在收到了订阅,但奇怪的是没有收到取消订阅。这不是一个大问题,但我们想知道原因 另一个问题是,如果我们可以一起问的话,通过运行在同一Actor系统中的代理Actor将远程Actor订阅到DistributedPubSubMediator是否是一种良好的做法 目前我们有:Scala 通过代理参与者的DistributedPubSubMediator订阅不工作,scala,akka,Scala,Akka,我的同事和我一直对DistributedPubSubMediator直接或通过代理角色订阅/取消订阅的不同行为感到困惑。我们进行了一个测试,以显示下面不同的结果 根据我们的理解,ActorRef.forward应该传递给原始发送者,因此消息是直接发送给中介者还是通过代理参与者并不重要。即 为了解决这个问题,我们必须扩展DIstributedPubSubMediator类,并包括DIstributedPubSubMediator对象已经提供的逻辑。理想情况下,我们更愿意直接使用对象并还原代码 这好
package star.common.pubsub
import org.scalatest.{BeforeAndAfterAll, FunSuite}
import org.junit.runner.RunWith
import akka.contrib.pattern.DistributedPubSubExtension
import akka.contrib.pattern.DistributedPubSubMediator._
import akka.testkit.TestKit
import akka.actor.{Actor, ActorSystem, ActorRef, Props}
import scala.concurrent.duration._
import com.typesafe.config.ConfigFactory
object MediatorTest {
val config = ConfigFactory.parseString(s"""
akka.actor.provider="akka.cluster.ClusterActorRefProvider"
akka.remote.netty.tcp.port=0
akka.extensions = ["akka.contrib.pattern.DistributedPubSubExtension"]
""")
}
@RunWith(classOf[org.scalatest.junit.JUnitRunner])
class MediatorTest extends TestKit(ActorSystem("test", MediatorTest.config)) with FunSuite {
val mediator = DistributedPubSubExtension(system).mediator
val topic = "example"
val message = "Published Message"
// val joinAddress = Cluster(system).selfAddress
// Cluster(system).join(joinAddress)
test("Direct subscribe to mediator") {
mediator.!(Subscribe(topic, testActor))(testActor)
expectMsgType[SubscribeAck](5 seconds)
mediator.!(Publish(topic, message))(testActor)
expectMsg(2 seconds, message)
mediator.!(Unsubscribe(topic, testActor))(testActor)
expectMsgType[UnsubscribeAck](5 seconds)
mediator ! Publish(topic, message)
expectNoMsg(2 seconds)
}
test("Subscribe to mediator via proxy") {
class Proxy extends Actor {
override def receive = {
case subscribe: Subscribe =>
mediator forward subscribe
case unsubscribe: Unsubscribe =>
mediator forward unsubscribe
case publish: Publish =>
mediator.!(publish)
}
}
val proxy = system.actorOf(Props(new Proxy), "proxy")
proxy.!(Subscribe(topic,testActor))(testActor)
expectMsgType[SubscribeAck](2 seconds)
proxy ! Publish(topic, message)
expectMsg(5 seconds, message)
proxy.!(Unsubscribe(topic,testActor))(testActor)
expectMsgType[UnsubscribeAck](5 seconds)
proxy ! Publish(topic, message)
expectNoMsg(5 seconds)
}
test("Send message to address") {
val testActorAddress = testActor.path.toString
// val system2 = ActorSystem("test", MediatorTest.config)
// Cluster(system2).join(joinAddress)
mediator.!(Subscribe(topic, testActor))(testActor)
expectMsgType[SubscribeAck](5 seconds)
println(testActorAddress) // akka://test/system/testActor1
mediator.!(Publish(topic, message))(testActor)
expectMsg(2 seconds, message)
mediator ! Send(testActorAddress, message, false)
expectMsg(5 seconds, message)
}
}
两件事:
- 您是否使用
并不重要,因为您的测试过程中没有一个有用的发送器(您没有混入转发
);但这不是问题所在隐式发送器
- 您没有转发
消息,这就是它没有发布消息的原因Publish