PubSub messaging handling
parent
e5da696339
commit
7a35bdd31e
@ -0,0 +1,9 @@
|
|||||||
|
package org.redisson.client;
|
||||||
|
|
||||||
|
public interface RedisPubSubListener<V> {
|
||||||
|
|
||||||
|
void onMessage(String channel, V message);
|
||||||
|
|
||||||
|
void onPatternMessage(String pattern, String channel, V message);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,7 @@
|
|||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
public interface Convertor<R> {
|
||||||
|
|
||||||
|
R convert(Object obj);
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
public class EmptyConvertor<R> implements Convertor<R> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public R convert(Object obj) {
|
||||||
|
return (R) obj;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -1,47 +1,22 @@
|
|||||||
/**
|
|
||||||
* 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;
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
import java.io.UnsupportedEncodingException;
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import io.netty.buffer.ByteBuf;
|
import io.netty.buffer.ByteBuf;
|
||||||
import io.netty.util.CharsetUtil;
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
public class PubSubMessageDecoder implements Codec {
|
public class PubSubMessageDecoder implements Decoder<Object> {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String decode(ByteBuf buf) {
|
public Object decode(ByteBuf buf) {
|
||||||
String status = buf.toString(CharsetUtil.UTF_8);
|
String status = buf.toString(CharsetUtil.UTF_8);
|
||||||
buf.skipBytes(2);
|
buf.skipBytes(2);
|
||||||
return status;
|
return status;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public PubSubMessage decode(List<Object> parts) {
|
public Object decode(List<Object> parts) {
|
||||||
return new PubSubMessage(PubSubMessage.Type.valueOf(parts.get(0).toString().toUpperCase()), parts.get(1).toString());
|
return new PubSubMessage(parts.get(1).toString(), parts.get(2).toString());
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public byte[] encode(int paramIndex, Object in) {
|
|
||||||
try {
|
|
||||||
return in.toString().getBytes("UTF-8");
|
|
||||||
} catch (UnsupportedEncodingException e) {
|
|
||||||
throw new IllegalStateException(e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,33 @@
|
|||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
public class PubSubPatternMessage {
|
||||||
|
|
||||||
|
private final String pattern;
|
||||||
|
private final String channel;
|
||||||
|
private final Object value;
|
||||||
|
|
||||||
|
public PubSubPatternMessage(String pattern, String channel, Object value) {
|
||||||
|
super();
|
||||||
|
this.pattern = pattern;
|
||||||
|
this.channel = channel;
|
||||||
|
this.value = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getPattern() {
|
||||||
|
return pattern;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getChannel() {
|
||||||
|
return channel;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Object getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PubSubPatternMessage [pattern=" + pattern + ", channel=" + channel + ", value=" + value + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,22 @@
|
|||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
|
public class PubSubPatternMessageDecoder implements Decoder<Object> {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decode(ByteBuf buf) {
|
||||||
|
String status = buf.toString(CharsetUtil.UTF_8);
|
||||||
|
buf.skipBytes(2);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object decode(List<Object> parts) {
|
||||||
|
return new PubSubPatternMessage(parts.get(1).toString(), parts.get(2).toString(), parts.get(3));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,52 @@
|
|||||||
|
/**
|
||||||
|
* 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.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import io.netty.buffer.ByteBuf;
|
||||||
|
import io.netty.util.CharsetUtil;
|
||||||
|
|
||||||
|
public class PubSubStatusDecoder implements Codec {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String decode(ByteBuf buf) {
|
||||||
|
String status = buf.toString(CharsetUtil.UTF_8);
|
||||||
|
buf.skipBytes(2);
|
||||||
|
return status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PubSubStatusMessage decode(List<Object> parts) {
|
||||||
|
List<String> channels = new ArrayList<String>();
|
||||||
|
for (Object part : parts.subList(1, parts.size()-1)) {
|
||||||
|
channels.add(part.toString());
|
||||||
|
}
|
||||||
|
return new PubSubStatusMessage(PubSubStatusMessage.Type.valueOf(parts.get(0).toString().toUpperCase()), channels);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public byte[] encode(int paramIndex, Object in) {
|
||||||
|
try {
|
||||||
|
return in.toString().getBytes("UTF-8");
|
||||||
|
} catch (UnsupportedEncodingException e) {
|
||||||
|
throw new IllegalStateException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.redisson.client.protocol;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public class PubSubStatusMessage {
|
||||||
|
|
||||||
|
public enum Type {SUBSCRIBE, PSUBSCRIBE, PUNSUBSCRIBE, UNSUBSCRIBE}
|
||||||
|
|
||||||
|
private final Type type;
|
||||||
|
private final List<String> channels;
|
||||||
|
|
||||||
|
public PubSubStatusMessage(Type type, List<String> channels) {
|
||||||
|
super();
|
||||||
|
this.type = type;
|
||||||
|
this.channels = channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<String> getChannels() {
|
||||||
|
return channels;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Type getType() {
|
||||||
|
return type;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "PubSubStatusMessage [type=" + type + ", channels=" + channels + "]";
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue