<repository>
<id>mongo-pubsub</id>
<url>https://raw.github.com/Clouke/mongo-pubsub/repository/</url>
</repository>
<dependency>
<groupId>gg.clouke</groupId>
<artifactId>mongo-pubsub</artifactId>
<version>1.0.0</version>
</dependency>
Since MongoDB is not primarily made for Pub/Sub messaging I recommend using Bridge - A Redis Pub/Sub Wrapper written in Java
NOTE: flushing is set to 5 seconds by default, if you wish to disable flushing, use disableFlushing()
MongoPubSubClient client = MongoPubSubClient.newBuilder()
.flushAfterWrite(10L, TimeUnit.SECONDS)
.uri("mongodb://mongodb0.example.com:27017")
.database("my_database")
.build();
MongoPubSubClient client = MongoPubSubClient.newBuilder()
.flushAfterWrite(10L, TimeUnit.SECONDS)
.host("localhost")
.port(27017)
.username("admin")
.password("admin")
.database("my_database")
.build();
client.subscribers()
.listenDirectly("my-listener", // <-- the target
payload -> {
String rawValue = payload.getRawValue("key"); // returns value <- key
SerializableTestObject serializableValue = payload.getValueAs("SerializedObject", SerializableTestObject.class); // deserializes into object
payload.close(); // close the payload
});
@Identifier("my-listener") // <-- the target - @Identifier is required for implementations
public class MyListener implements Subscriber {
@Override
public void onMessage(Payload payload) {
System.out.println("Received payload " + payload);
payload.close(); // close the payload
}
}
client.subscribers().addListener(new MyListener());
client.enqueue("my-listener", Payload.empty() // <-- target identifier, payload
.withRawParameter("key", "value") // simple key-value pair
.withSerializableParameter("SerializedObject", new SerializableTestObject("Jonathan", 20))); // serializable objects
Contributions are highly appreciated! If you feel your pull request is useful, go ahead! Before creating a pull request, make sure your changes works as it should and give a description on what it provides.