Publish & Subscribe
GreenLightning allows behaviors to communicate with each other using PubSub (short for Publish and Subscribe). One behavior can subscribe to a topic and thus listen to any messages coming from other behaviors about the same topic.
GreenLightning also has the concept of Private Topics - topics between behaviors are discovered at start, and a direct private connection between those behaviors is established, preventing other behaviors from listening if they do not subscribe to the same topic.

PubSub Example

Below is a very simple PubSub example. The GreetingBehavior will send out a greeting message to the topic "Greet" and publish another greeting if it receives a "SayHello" message. The ExampleBehavior will listen to the greeting message and send out the initial "SayHello" message.
We demonstrate usage of the default PubSubListener (filter topics manually using if-else or switch-case) and PubSubMethodListener (direct method call, no branching logic needed).
In our main file, we will simply register the behaviors and add their corresponding subscriptions:
PubSub.java
1
public class PubSub implements GreenApp
2
{
3
@Override
4
public void declareConfiguration(Builder c) {
5
}
6
7
@Override
8
public void declareBehavior(GreenRuntime runtime) {
9
// Create a pubSub listener that will broadcast a message if prompted
10
runtime.addPubSubListener(new GreetingBehavior(runtime, "Greet"))
11
// We want to know when someone is asking for a greeting.
12
.addSubscription("SayHello");
13
14
// Create another behavior that will ask for a Hello on start up and
15
// listen to it.
16
ExampleBehavior example = new ExampleBehavior(runtime, "SayHello");
17
runtime.registerListener(example)
18
// We want to know when someone greets us, so subscribe
19
.addSubscription("Greet", example::listenToGreeting)
20
// This just demonstrates multiple subscriptions are simpler using
21
// PubSubMethodListener instead of PubSubListener
22
.addSubscription("AnExample", example::anotherMessage);
23
}
24
}
Copied!
In GreetingBehavior.java, we create a PubSubFixedTopicService to publish our message:
GreetingBehavior.java
1
public class GreetingBehavior implements PubSubListener {
2
private final PubSubFixedTopicService channel;
3
4
public GreetingBehavior(GreenRuntime runtime, CharSequence publishTopic) {
5
// Create a channel based on topic passed in that we will publish to
6
this.channel = runtime.newCommandChannel().newPubSubService(publishTopic.toString());
7
}
8
9
// We have receive someone asking for a greeting, so send it!
10
@Override
11
public boolean message(CharSequence topic, ChannelReader payload) {
12
//Note if this behavior is subscribed to more than 1 topic we will need
13
//to branch here based on the value of topic.
14
return channel.publishTopic(w -> w.append("Hello World!"));
15
}
16
}
Copied!
In ExampleBehavior.java, we create another PubSubFixedTopicService and use it to publish our given topic on start (we are asking for a Greeting). Then, we have the listenToGreeting method as defined in PubSub.java listening to any new greetings:
ExampleBehavior.java
1
public class ExampleBehavior implements PubSubMethodListener, StartupListener {
2
private final PubSubFixedTopicService channel;
3
4
public ExampleBehavior(GreenRuntime runtime, CharSequence publishTopic) {
5
// Create a new command channel that we will use to ask for a greeting (SayHello topic).
6
this.channel = runtime.newCommandChannel().newPubSubService(publishTopic.toString());
7
}
8
9
@Override
10
public void startup() {
11
// On startup, let's ask for a greeting!
12
channel.publishTopic();
13
}
14
15
public boolean listenToGreeting(CharSequence topic, ChannelReader payload) {
16
// We received a greeting, so listen to it
17
System.out.println("Received a greeting: " + payload.readUTF());
18
return true; // if you were to return false, you will receive the same message again next time
19
}
20
21
public boolean anotherMessage(CharSequence topic, ChannelReader payload) {
22
// Do nothing here, it's just an example.
23
return true;
24
}
25
}
Copied!

Private Topics

Private Topics (always enabled) allow direct communication between behaviors without having to dispatch through a central router, improving performance and security.
However, you may not want Private Topics for certain topics (see warning below). You can use the method definePublicTopics on Builder:
1
@Override
2
public void declareConfiguration(Builder config) {
3
// For our example above, this would disable dirct communication between
4
// ExampleBehavior and GreetingBehavior. The messages would first go through
5
// a grand dispatch before arriving at targeted subscribers.
6
config.definePublicTopics("Greet", "SayHello");
7
}
Copied!
Note that subscribing to a private topic at runtime is not possible. You will have to explicitly declare this topic public if you know that it will be dynamically subscribed to at a later point.
Last modified 3yr ago