2012-05-28 73 views
7

需要一些關於如何使用由Java(不是Scala!)中的Akka提供的EventBus的建議。網站上的文件似乎是不完整的:http://doc.akka.io/docs/akka/2.0.1/java/event-bus.htmlJava的Akka EventBus示例

據我瞭解,演員都應該以對特定消息的反應,如:

final ActorSystem actorSystem = ActorSystem.create("ServerEvents"); 
final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class)); 
actorSystem.eventStream().subscribe(actor,ServerMessage.class); 

但現在還不清楚如何將消息發送到事件公共汽車。

有人可以分享一些很好的教程/例子/等?

+0

我已經開了票,以提高文檔,同時研究了API: http://doc.akka.io/api/akka/2.0.1/#akka.event.EventStream –

+0

其實我非常想看到這樣的例子。目前我們正在使用Guava的EventBus進行簡單的GUI事件處理。對於繁重的工作,我想介紹Akka並徹底擺脫番石榴。將兩個事件處理庫保存在同一個應用程序中有點愚蠢...... –

回答

10

我覺得你只是一個線路短路:

final ActorSystem actorSystem = ActorSystem.create("ServerEvents"); 
final ActorRef actor = actorSystem.actorOf(new Props(SeverEventHandler.class)); 
actorSystem.eventStream().subscribe(actor,ServerMessage.class); 

actorSystem.eventStream().publish(new ServerMessage()); <<== add this 

雖然ServerEventHandler應該像

public class ServerEventHandler extends UntypedActor { 
    @Override 
    public void onReceive(final Object message) { 
    System.out.println("Got event in thread: " + Thread.currentThread().getName()); 
    System.out.println("Event: " + message); 
    } 
}