-
Notifications
You must be signed in to change notification settings - Fork 0
Publisher
Danila Rassokhin edited this page Jan 27, 2023
·
1 revision
Publisher allows your components to exchange data. It works as pub-sub pattern, so you can send messages to topics and subscribe on topics to receive messages.
Publisher has 2 types of messaging strategy:
- SEQUENCE
- All scripts will be notified in sequence they were subscribed
- PARALLEL
- Try to notify scripts using parallel unordered streams. Possibly can be faster if you have many scripts with "heavy" logic
basicGamePublisher.setPublisherType(PublisherType.PARALLEL);
public class PublisherExample {
String message;
public PublisherExample() {
// Get publisher instance
GamePublisher<PublisherSubscription, String> basicGamePublisher =
BasicGamePublisher.getInstance();
// Set publisher type
// PARALLEL - use parallel streams to call listeners
// SEQUENCE - iterate through listeners in order they subscribed
basicGamePublisher.setPublisherType(PublisherType.PARALLEL);
// Subscribe to topic "method" and use print method while listener call
PublisherSubscription methodSubscription =
basicGamePublisher.subscribeOn("method", this::print);
// Subscribe to topic "print" and print received object to console
basicGamePublisher.subscribeOn("print", m -> BasicComponentManager.getGameLogger().info(m));
// Subscribe to topic "set" and set current message to received message
basicGamePublisher.subscribeOn("set", m -> message = m.toString());
// Send message "hello" to "method" topic,
// e.g. call all listeners on "method" topic and pass "hello" to them
basicGamePublisher.sendTo("method", "hello");
// Send message "message" to "print" topic,
// e.g. call all listeners on "print" and pass "message" to them
basicGamePublisher.sendTo("print", "message");
// Send GameItem object to "set" topic,
// e.g. call all listeners on "set" and pass GameItem to them
basicGamePublisher.sendTo("set", new GameItem());
// Remove listener from topic
basicGamePublisher.unsubscribe(methodSubscription);
}
public void print(Object message) {
BasicComponentManager.getGameLogger().info(message);
}
}