It looks like Event Bus requires Subscriber to start first prior to the 
publisher publishing messages. Is there anyway publisher can simply fire 
and forget and whenever consumer becomes available it can consume from the 
Event Bus ? I don't need anything like Distributed Pub Sub since I just 
want this mechanism only within a single JVM.

On Thursday, March 23, 2017 at 2:49:01 PM UTC-7, kant kodali wrote:
>
> 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 [email protected].
To post to this group, send email to [email protected].
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