Created
November 29, 2012 18:00
-
-
Save ricemery/4170803 to your computer and use it in GitHub Desktop.
Java example on using Akka EventBus
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import akka.actor.ActorRef; | |
import akka.actor.ActorSystem; | |
import akka.actor.Props; | |
import akka.actor.UntypedActor; | |
import akka.event.japi.LookupEventBus; | |
/** | |
* Java example showing the usage of a Akka {@link akka.event.japi.LookupEventBus}. | |
* The case below shows publishing events to two different "channels". A channel is just | |
* a string value carried along with the Event. | |
*/ | |
public class AkkaEventBusExample { | |
public class Event { | |
private String channel; | |
public Event(final String channel) { | |
this.channel = channel; | |
} | |
public String getChannel() { | |
return channel; | |
} | |
} | |
public class SomethingHappenedEvent extends Event { | |
public SomethingHappenedEvent(String channel) { | |
super(channel); | |
} | |
} | |
public class SomethingElseHappenedEvent extends Event { | |
public SomethingElseHappenedEvent(String channel) { | |
super(channel); | |
} | |
} | |
/** | |
* Way to send events to multiple subscribers. Based on Akka {@link akka.event.japi.LookupEventBus}. | |
*/ | |
public class EventBus extends LookupEventBus { | |
/** | |
* Initial size of the index data structure used internally | |
* (i.e. the expected number of different classifiers) | |
*/ | |
@Override | |
public int mapSize() { | |
return 5; | |
} | |
/** | |
* Used to define a partial ordering of subscribers. The ordering is based on Event.channel | |
*/ | |
@Override | |
public int compareSubscribers(Object subscriberA, Object subscriberB) { | |
return ((Event)subscriberA).getChannel().compareTo(((Event)subscriberB).getChannel()); | |
} | |
/** | |
* Extract the classification data from the event. | |
* @param event {@link Event} to classify | |
* @return Channel string from the {@link Event} | |
*/ | |
@Override | |
public Object classify(Object event) { | |
return ((Event)event).getChannel(); | |
} | |
/** | |
* Publish an {@link Event} | |
* @param event {@link Event} to publish | |
* @param subscriber {@link akka.actor.ActorRef} that is subscribed to the {@link Event} | |
*/ | |
@Override | |
public void publish(Object event, Object subscriber) { | |
((ActorRef) subscriber).tell(event); | |
} | |
} | |
public void createBusAndPublishEvents() { | |
// Create the EventBus and the ActorSystem instance. | |
final EventBus eventBus = new EventBus(); | |
final ActorSystem actorSystem = ActorSystem.create("Events"); | |
// Create two different actor instances. The instances will be subscribed to | |
// different channels | |
final ActorRef actor = actorSystem.actorOf(new Props(EventHandler.class)); | |
final ActorRef actor2 = actorSystem.actorOf(new Props(EventHandler.class)); | |
// Subscribe the two actors to the two different channels | |
String CHANNEL1 = "channel1"; | |
String CHANNEL2 = "channel2"; | |
eventBus.subscribe(actor, CHANNEL1); | |
eventBus.subscribe(actor2, CHANNEL2); | |
// Publish a couple of events to the two channels. | |
// Publish to CHANNEL1 | |
eventBus.publish(new SomethingHappenedEvent(CHANNEL1)); | |
// Publish to CHANNEL2 | |
eventBus.publish(new SomethingElseHappenedEvent(CHANNEL2)); | |
actorSystem.shutdown(); | |
} | |
/** | |
* This is the Actor implementation. i.e. The object that is being subscribed to listen for events. | |
* aka: Observer. | |
*/ | |
public static class EventHandler extends UntypedActor { | |
@Override | |
public void onReceive(final Object message) { | |
System.out.println("Event: " + message + " thread: " + Thread.currentThread().getName()); | |
} | |
} | |
public static void main(String[] args) { | |
new AkkaEventBusExample().createBusAndPublishEvents(); | |
} | |
} |
I am not entirely sure right now what the issue was, the code in general works it just had to be tweaked a little if I remember correctly. I am by no means an expert in akka so I just copy-pasted it and I think some adjustments had to be made, I think it was the same thing @ujay68 said. But I was still very grateful for someone to post an example :)
What version of Akka and the JVM are you using?
I should probably tweak it to work with the latest versions. I honestly
hadn't looked at that code in years.
I am glad it was somewhat helpful to you.
…On Wed, Sep 4, 2019 at 2:22 PM CheapGucciClothing ***@***.***> wrote:
I am not entirely sure right now what the issue was, the code in general
works it just had to be tweaked a little if I remember correctly. I am by
no means an expert in akka so I just copy-pasted it and I think some
adjustments had to be made, I think it was the same thing @ujay68
<https://github.com/ujay68> said. But I was still very grateful for
someone to post an example :)
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<https://gist.github.com/4170803?email_source=notifications&email_token=AALYOG676O6H5RS6XVOWGCTQIARJJA5CNFSM4ITDM4BKYY3PNVWWK3TUL52HS4DFVNDWS43UINXW23LFNZ2KUY3PNVWWK3TUL5UWJTQAFYEME#gistcomment-3016898>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AALYOG3NI42XMKKSCY3JA4DQIARJJANCNFSM4ITDM4BA>
.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What do you mean that the code doesn't work? I just ran the code and it executed fine.
I ran the code with JDK 1.8.144 and Akka 2.0.4.
Note that this code is 7 years old. It would not surprise me if it has to be tweaked to work with the latest JVM and Akka.