Since v0.7.0, ddth-queue
provides a unified API to publish/subscribe messages:
- Publish messages to channels.
- Subscribe to channels and consume messages.
- Create a
IPubSubHub
instance. Pre-made implementations can be used out-of-the-box, see below. - Call
IPubSubHub.publish(channel, msg)
publish a message to a channel. - Call
IPubSubHub.subscribe(channel, subscriber)
to subscribe to a channel:- Multiple subscribers can subscribe to one channel.
- One subscriber instance can subscribe to multiple channels.
- Call
IPubSubHub.subscribe(channel, subscriber)
to unsubscribe from a channel.
Provided via IPubSubHub
interface:
API | Description |
---|---|
boolean publish(String, IMessage) |
Publish a message to a channel. |
void subscribe(String, ISubscriber) |
Subscribe to a channel to receive messages. |
void unsubscribe(String, ISubscriber) |
Unsubscribe from a channel. |
Implementation | Inter-process |
---|---|
In-memory | No |
MongoDB | Yes |
Redis | Yes |
- Inter-process: publishers and subscribers can be from different JVMs.
Publishers and Subscribers are on the same JVM.
See InmemPubSubHub.java.
Utilize MongoDB as pub/sub backend.
Publishers and Subscribers can be on different JVMs.
Utilize Redis' pub/sub feature to distribute messages.
Publishers and Subscribers can be on different JVMs.
See RedisPubSubHub.java.
com.github.ddth.pubsub.impl.universal.*
:UniversalIdIntMessage
: "universal" implementation of message where content isbyte[]
and id isLong
UniversalIdStrMessage
: "universal" implementation of message where content isbyte[]
and id isString
UniversalIdIntMessageFactory
to createUniversalIdIntMessage
, andUniversalIdStrMessageFactory
forUniversalIdStrMessage
com.github.ddth.pubsub.impl.universal.idint.*
: universal pub/sub implementations, where message's id isLong
.com.github.ddth.pubsub.impl.universal.idstr.*
: universal pub/sub implementations, where message's id isString
.
"Universal" message implementation, with the following fields:
id
: message's unique idcom.github.ddth.pubsub.impl.universal.UniversalIdIntMessage
: id is a 64-bitLong
com.github.ddth.pubsub.impl.universal.UniversalIdStrMessage
: id is aString
time
(java.util.Date
): timestamp when the message was createddata
(byte[]
): message's contentpkey
(String
): message's partition key
A subscriber that counts number of received messages. See CountingSubscriber.java.
Universal in-memory pub/sub implementation: publishers and subscribers need to be on the same JVM.
com.github.ddth.pubsub.impl.universal.idint.UniversalInmemPubSubHub
to work with UniversalIdIntMessage
,
and com.github.ddth.pubsub.impl.universal.idstr.UniversalInmemPubSubHub
to work with UniversalIdStrMessage
.
Universal pub/sub implementation that uses MongoDB as medium backend to transit messages: publishers and subscribers can be on different JVMs.
com.github.ddth.pubsub.impl.universal.idint.UniversalMongodbPubSubHub
to work with UniversalIdIntMessage
,
and com.github.ddth.pubsub.impl.universal.idstr.UniversalMongodbPubSubHub
to work with UniversalIdStrMessage
.
Universal pub/sub implementation that uses Redis's pub/sub feature distribute messages: publishers and subscribers can be on different JVMs.
com.github.ddth.pubsub.impl.universal.idint.UniversalRedisPubSubHub
to work with UniversalIdIntMessage
,
and com.github.ddth.pubsub.impl.universal.idstr.UniversalRedisPubSubHub
to work with UniversalIdStrMessage
.