Hi All,

Does Event Bus require Publisher and Subscriber to be started in certain 
order? Isn't there a Listener which will push messages to subscribers 
whenever they become alive? 

I am playing with the LookUpBus Example from here 
<http://doc.akka.io/docs/akka/current/java/event-bus.html> but it doesn't 
quite work for me.  here is my sample code and I had also commented on the 
respective lines.

For now, I am just using print statements to debug. since the messages are 
getting printed out I wonder if Publisher and Subscriber to be started in 
certain order?

Thanks!


public class EventBusManager {

    private static final LookupBusImpl LOOKUP_BUS = new LookupBusImpl();

    public static LookupBusImpl getEventBus() {
        return LOOKUP_BUS;
    }
}


public class UserActor extends UntypedActor {

    private final ActorRef out;
    private final String topic;

    public UserActor(ActorRef out, String topic) {
        this.out = out;
        this.topic = topic;
    }

    public static Props props(ActorRef out, String topic) {
        return Props.create(UserActor.class, out, topic);
    }

    public void preStart() {
        EventBusManager.getEventBus().subscribe(self(), topic); // This Actor 
will become the subscriber of even bus so whenever publisher sends a message I

                                                                // Expect This 
Actor to receive the messages 

    } 

    public void postStop() {
        EventBusManager.getEventBus().unsubscribe(self());
    }

    public void onReceive(Object message) throws Exception {

        System.out.println("Received Message: " + message); // I expect the 
messages to be printed here
        if (message instanceof String) {
            out.tell(message, self());
        }
    }


}



public class LookupBusImpl extends LookupEventBus<MsgEnvelope, ActorRef, 
String> {

    // is used for extracting the classifier from the incoming events
    @Override
    public String classify(MsgEnvelope event) {
        return event.topic;
    }

    // will be invoked for each event for all subscribers which registered 
themselves
    // for the event’s classifier
    @Override
    public void publish(MsgEnvelope event, ActorRef subscriber) {
        System.out.println("LOOKUP_BUS_IMPL: " + event.payload);
        subscriber.tell(event.payload, ActorRef.noSender()); // not sure what 
exactly is going on here? But I Expect the above actor to receive the messages
    }

    // must define a full order over the subscribers, expressed as expected from
    // `java.lang.Comparable.compare`
    @Override
    public int compareSubscribers(ActorRef a, ActorRef b) {
        return a.compareTo(b);
    }

    // determines the initial size of the index data structure
    // used internally (i.e. the expected number of different classifiers)
    @Override
    public int mapSize() {
        return 128;
    }



}


public class Application extends Controller {

    public WebSocket<String> handleWebSocketConnection() {
        return WebSocket.withActor(new F.Function<ActorRef, Props>() {
            @Override
            public Props apply(ActorRef actorRef) throws Throwable {
                return UserActor.props(actorRef, "hello");
            }
        });
    }

}

public class Global extends GlobalSettings {


    @Override
    public void onStart(Application application) { 

        Logger.info("Brodcasted to event Bus started.."); // This prints out to 
console fine 

        EventBusManager.getEventBus().publish(new MsgEnvelope("hello", "Hello 
World")); // This is where I am publishing messages to event bus.
        Logger.info("Brodcasted to event Bus completed"); // This prints out to 
console fine 

    }


}

-- 
>>>>>>>>>>      Read the docs: http://akka.io/docs/
>>>>>>>>>>      Check the FAQ: 
>>>>>>>>>> http://doc.akka.io/docs/akka/current/additional/faq.html
>>>>>>>>>>      Search the archives: https://groups.google.com/group/akka-user
--- 
You received this message because you are subscribed to the Google Groups "Akka 
User List" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to akka-user+unsubscr...@googlegroups.com.
To post to this group, send email to akka-user@googlegroups.com.
Visit this group at https://groups.google.com/group/akka-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to