프로그래밍
vertx 3.1에서 redis pub-sub 사용하기
약올랑
2015. 11. 6. 16:15
개요
vertx 3.1에서 redis를 이용해 pub-sub가 동작하지 않았다.
vertx 공식 문서와 vertx redis client github에 있는 예제를 그대로 했음에도 안되었는데,
원인은 컨수머에 등록한 이름이 vertx 2에서 사용했던 구버전의 이름이었다.
redis client 소스를 파악해 정상적인 이름을 얻어냈다.
올바른 예제 코드
vertx.eventBus().
This file contains hidden or 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
public class MainVerticle extends AbstractVerticle { | |
EventBus eb; | |
RedisClient redis; | |
@Override | |
public void start() { | |
redis = RedisClient.create(vertx, new RedisOptions()); | |
eb = vertx.eventBus(); | |
vertx.eventBus().<JsonObject>consumer("io.vertx.redis.channel1", received -> { | |
// do whatever you need to do with your message | |
JsonObject value = received.body().getJsonObject("value"); | |
// the value is a JSON doc with the following properties | |
// channel - The channel to which this message was sent | |
// pattern - Pattern is present if you use psubscribe command and is the pattern that matched this message channel | |
// message - The message payload | |
System.out.println("consumer:" + value); | |
}); | |
redis.subscribe("channel1", r -> { | |
System.out.println("subscribe:" + r.result()); | |
}); | |
vertx.setTimer(1000, handler->{ | |
redis.publish("channel1", "bar", v->{ | |
System.out.println("publish:" + v.result()); | |
}); | |
}); | |
} | |
} |
This file contains hidden or 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
<dependency> | |
<groupId>io.vertx</groupId> | |
<artifactId>vertx-redis-client</artifactId> | |
<version>3.1.0</version> | |
</dependency> |