PubSub support
parent
6c182ed77e
commit
e5da696339
@ -0,0 +1,44 @@
|
||||
package org.redisson.client;
|
||||
|
||||
import org.redisson.client.handler.RedisData;
|
||||
import org.redisson.client.protocol.Codec;
|
||||
import org.redisson.client.protocol.PubSubMessage;
|
||||
import org.redisson.client.protocol.PubSubMessageDecoder;
|
||||
import org.redisson.client.protocol.RedisCommand;
|
||||
import org.redisson.client.protocol.RedisCommands;
|
||||
import org.redisson.client.protocol.StringCodec;
|
||||
|
||||
import io.netty.channel.Channel;
|
||||
import io.netty.channel.ChannelFuture;
|
||||
import io.netty.util.concurrent.Future;
|
||||
import io.netty.util.concurrent.Promise;
|
||||
|
||||
public class RedisPubSubConnection {
|
||||
|
||||
final Channel channel;
|
||||
final RedisClient redisClient;
|
||||
|
||||
public RedisPubSubConnection(RedisClient redisClient, Channel channel) {
|
||||
this.redisClient = redisClient;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public Future<PubSubMessage> subscribe(String ... channel) {
|
||||
return async(new PubSubMessageDecoder(), RedisCommands.SUBSCRIBE, channel);
|
||||
}
|
||||
|
||||
public Future<Long> publish(String channel, String msg) {
|
||||
return async(new StringCodec(), RedisCommands.PUBLISH, channel, msg);
|
||||
}
|
||||
|
||||
public <T, R> Future<R> async(Codec encoder, RedisCommand<T> command, Object ... params) {
|
||||
Promise<R> promise = redisClient.getBootstrap().group().next().<R>newPromise();
|
||||
channel.writeAndFlush(new RedisData<T, R>(promise, encoder, command, params));
|
||||
return promise;
|
||||
}
|
||||
|
||||
public ChannelFuture closeAsync() {
|
||||
return channel.close();
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package org.redisson.client.protocol;
|
||||
|
||||
public class PubSubMessage {
|
||||
|
||||
public enum Type {SUBSCRIBE, MESSAGE}
|
||||
|
||||
private Type type;
|
||||
private String channel;
|
||||
|
||||
public PubSubMessage(Type type, String channel) {
|
||||
super();
|
||||
this.type = type;
|
||||
this.channel = channel;
|
||||
}
|
||||
|
||||
public String getChannel() {
|
||||
return channel;
|
||||
}
|
||||
|
||||
public Type getType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "PubSubReplay [type=" + type + ", channel=" + channel + "]";
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright 2014 Nikita Koksharov, Nickolay Borbit
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
package org.redisson.client.protocol;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.util.List;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
public class PubSubMessageDecoder implements Codec {
|
||||
|
||||
@Override
|
||||
public String decode(ByteBuf buf) {
|
||||
String status = buf.toString(CharsetUtil.UTF_8);
|
||||
buf.skipBytes(2);
|
||||
return status;
|
||||
}
|
||||
|
||||
@Override
|
||||
public PubSubMessage decode(List<Object> parts) {
|
||||
return new PubSubMessage(PubSubMessage.Type.valueOf(parts.get(0).toString().toUpperCase()), parts.get(1).toString());
|
||||
}
|
||||
|
||||
@Override
|
||||
public byte[] encode(int paramIndex, Object in) {
|
||||
try {
|
||||
return in.toString().getBytes("UTF-8");
|
||||
} catch (UnsupportedEncodingException e) {
|
||||
throw new IllegalStateException(e);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue