-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract the Redis subscription -> Vert.x event bus handler
The `SubscriptionHandler` does almost the same job, but has to be set on the `RedisConnection` explicitly (using the `handler()` method). In addition to forwarding the messages to the event bus, the new `SubscriptionHandler` also forwards subscription/unsubscriptions.
- Loading branch information
Showing
4 changed files
with
107 additions
and
48 deletions.
There are no files selected for viewing
93 changes: 93 additions & 0 deletions
93
src/main/java/io/vertx/redis/client/SubscriptionHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package io.vertx.redis.client; | ||
|
||
import io.vertx.core.Handler; | ||
import io.vertx.core.Vertx; | ||
import io.vertx.core.eventbus.EventBus; | ||
import io.vertx.core.internal.logging.Logger; | ||
import io.vertx.core.internal.logging.LoggerFactory; | ||
import io.vertx.core.json.JsonObject; | ||
import io.vertx.redis.client.impl.types.Multi; | ||
|
||
/** | ||
* A handler that can be installed on a Redis connection using {@link RedisConnection#handler(Handler)} | ||
* in order to consume subscription messages and send them on the Vert.x {@linkplain Vertx#eventBus() event bus}. | ||
* <p> | ||
* By default, the address to which the messages are sent is {@code io.vertx.redis.<Redis channel>}. | ||
* This can be changed by passing a different address prefix to {@link #create(Vertx, String)}. | ||
* For example, if the prefix is {@code com.example}, the address is {@code com.example.<Redis channel>}. | ||
*/ | ||
public class SubscriptionHandler implements Handler<Response> { | ||
private static final Logger LOG = LoggerFactory.getLogger(SubscriptionHandler.class); | ||
|
||
private static final String DEFAULT_ADDRESS_PREFIX = "io.vertx.redis"; | ||
|
||
private final String prefix; | ||
private final EventBus eventBus; | ||
|
||
/** | ||
* Creates a subscription handler that forwards messages to the event bus of the given {@code vertx}. | ||
* The default address prefix is used. | ||
*/ | ||
public static SubscriptionHandler create(Vertx vertx) { | ||
return new SubscriptionHandler(DEFAULT_ADDRESS_PREFIX, vertx.eventBus()); | ||
} | ||
|
||
/** | ||
* Creates a subscription handler that forwards messages to the event bus of the given {@code vertx}. | ||
* The given address prefix is used. | ||
*/ | ||
public static SubscriptionHandler create(Vertx vertx, String addressPrefix) { | ||
return new SubscriptionHandler(addressPrefix, vertx.eventBus()); | ||
} | ||
|
||
private SubscriptionHandler(String prefix, EventBus eventBus) { | ||
this.prefix = prefix; | ||
this.eventBus = eventBus; | ||
} | ||
|
||
@Override | ||
public void handle(Response reply) { | ||
// pub/sub messages are arrays | ||
if (reply instanceof Multi) { | ||
// Detect valid published messages according to https://redis.io/topics/pubsub | ||
String type = reply.get(0).toString(); | ||
|
||
if (reply.size() == 3 && "message".equals(type)) { | ||
eventBus.send(prefix + "." + reply.get(1).toString(), | ||
new JsonObject() | ||
.put("status", "OK") | ||
.put("type", type) | ||
.put("value", new JsonObject() | ||
.put("channel", reply.get(1).toString()) | ||
.put("message", reply.get(2).toString()))); | ||
return; | ||
} | ||
|
||
if (reply.size() == 4 && "pmessage".equals(type)) { | ||
eventBus.send(prefix + "." + reply.get(1).toString(), | ||
new JsonObject() | ||
.put("status", "OK") | ||
.put("type", type) | ||
.put("value", new JsonObject() | ||
.put("pattern", reply.get(1).toString()) | ||
.put("channel", reply.get(2).toString()) | ||
.put("message", reply.get(3).toString()))); | ||
return; | ||
} | ||
|
||
if (reply.size() == 3 && ("subscribe".equals(type) || "psubscribe".equals(type) | ||
|| "unsubscribe".equals(type) || "punsubscribe".equals(type))) { | ||
eventBus.send(prefix + "." + reply.get(1).toString(), | ||
new JsonObject() | ||
.put("status", "OK") | ||
.put("type", type) | ||
.put("value", new JsonObject() | ||
.put("channel", reply.get(1).toString()) | ||
.put("remaining", reply.get(2).toString()))); | ||
return; | ||
} | ||
} | ||
|
||
LOG.warn("No handler waiting for message: " + reply); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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