Search in sources :

Example 1 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class RedissonObjectFactory method fromReference.

public static <T> T fromReference(RedissonClient redisson, RedissonReference rr) throws Exception {
    Class<? extends Object> type = rr.getType();
    CodecProvider codecProvider = redisson.getConfig().getCodecProvider();
    if (type != null) {
        if (type.isAnnotationPresent(REntity.class)) {
            RLiveObjectService liveObjectService = redisson.getLiveObjectService();
            REntity anno = type.getAnnotation(REntity.class);
            NamingScheme ns = anno.namingScheme().getDeclaredConstructor(Codec.class).newInstance(codecProvider.getCodec(anno, type));
            return (T) liveObjectService.get(type, ns.resolveId(rr.getKeyName()));
        }
        List<Class<?>> interfaces = Arrays.asList(type.getInterfaces());
        for (Class<?> iType : interfaces) {
            if (builders.containsKey(iType)) {
                // user cache to speed up things a little.
                Method builder = builders.get(iType).get(isDefaultCodec(rr));
                return (T) (isDefaultCodec(rr) ? builder.invoke(redisson, rr.getKeyName()) : builder.invoke(redisson, rr.getKeyName(), codecProvider.getCodec(rr.getCodecType())));
            }
        }
    }
    throw new ClassNotFoundException("No RObject is found to match class type of " + rr.getTypeName() + " with codec type of " + rr.getCodecName());
}
Also used : CodecProvider(org.redisson.codec.CodecProvider) Codec(org.redisson.client.codec.Codec) REntity(org.redisson.api.annotation.REntity) NamingScheme(org.redisson.liveobject.resolver.NamingScheme) Method(java.lang.reflect.Method) RLiveObjectService(org.redisson.api.RLiveObjectService)

Example 2 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class MasterSlaveConnectionManager method unsubscribe.

public Codec unsubscribe(final String channelName, final AsyncSemaphore lock) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        lock.release();
        return null;
    }
    Codec entryCodec = entry.getConnection().getChannels().get(channelName);
    entry.unsubscribe(channelName, new BaseRedisPubSubListener() {

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            if (type == PubSubType.UNSUBSCRIBE && channel.equals(channelName)) {
                if (entry.release() == 1) {
                    freePubSubConnections.add(entry);
                }
                lock.release();
                return true;
            }
            return false;
        }
    });
    return entryCodec;
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 3 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class MasterSlaveConnectionManager method punsubscribe.

@Override
public RFuture<Codec> punsubscribe(final String channelName, boolean temporaryDown) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        return null;
    }
    freePubSubConnections.remove(entry);
    final Codec entryCodec = entry.getConnection().getChannels().get(channelName);
    if (temporaryDown) {
        final RPromise<Codec> result = newPromise();
        entry.punsubscribe(channelName, new BaseRedisPubSubListener() {

            @Override
            public boolean onStatus(PubSubType type, String channel) {
                if (type == PubSubType.PUNSUBSCRIBE && channel.equals(channelName)) {
                    result.trySuccess(entryCodec);
                    return true;
                }
                return false;
            }
        });
        return result;
    }
    entry.punsubscribe(channelName, null);
    return newSucceededFuture(entryCodec);
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 4 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class MasterSlaveConnectionManager method unsubscribe.

@Override
public RFuture<Codec> unsubscribe(final String channelName, boolean temporaryDown) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        return null;
    }
    freePubSubConnections.remove(entry);
    final Codec entryCodec = entry.getConnection().getChannels().get(channelName);
    if (temporaryDown) {
        final RPromise<Codec> result = newPromise();
        entry.unsubscribe(channelName, new BaseRedisPubSubListener() {

            @Override
            public boolean onStatus(PubSubType type, String channel) {
                if (type == PubSubType.UNSUBSCRIBE && channel.equals(channelName)) {
                    result.trySuccess(entryCodec);
                    return true;
                }
                return false;
            }
        });
        return result;
    }
    entry.unsubscribe(channelName, null);
    return newSucceededFuture(entryCodec);
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 5 with Codec

use of org.redisson.client.codec.Codec in project redisson by redisson.

the class RedissonBuckets method getAsync.

@Override
public <V> RFuture<Map<String, V>> getAsync(String... keys) {
    if (keys.length == 0) {
        return RedissonPromise.newSucceededFuture(Collections.emptyMap());
    }
    Codec commandCodec = new CompositeCodec(StringCodec.INSTANCE, codec, codec);
    RedisCommand<Map<Object, Object>> command = new RedisCommand<Map<Object, Object>>("MGET", new MapGetAllDecoder(Arrays.<Object>asList(keys), 0));
    return commandExecutor.readBatchedAsync(commandCodec, command, new SlotCallback<Map<Object, Object>, Map<String, V>>() {

        final Map<String, V> results = new ConcurrentHashMap<>();

        @Override
        public void onSlotResult(Map<Object, Object> result) {
            for (Map.Entry<Object, Object> entry : result.entrySet()) {
                if (entry.getKey() != null && entry.getValue() != null) {
                    results.put((String) entry.getKey(), (V) entry.getValue());
                }
            }
        }

        @Override
        public Map<String, V> onFinish() {
            return results;
        }

        @Override
        public RedisCommand<Map<Object, Object>> createCommand(List<String> keys) {
            return new RedisCommand<>("MGET", new BucketsDecoder(keys));
        }
    }, keys);
}
Also used : CompositeCodec(org.redisson.codec.CompositeCodec) Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) CompositeCodec(org.redisson.codec.CompositeCodec) RedisCommand(org.redisson.client.protocol.RedisCommand) MapGetAllDecoder(org.redisson.connection.decoder.MapGetAllDecoder) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) BucketsDecoder(org.redisson.connection.decoder.BucketsDecoder)

Aggregations

Codec (org.redisson.client.codec.Codec)34 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)11 java.util (java.util)10 Collectors (java.util.stream.Collectors)9 ConnectionManager (org.redisson.connection.ConnectionManager)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7 ChannelFuture (io.netty.channel.ChannelFuture)6 ChannelFutureListener (io.netty.channel.ChannelFutureListener)6 Timeout (io.netty.util.Timeout)6 StringCodec (org.redisson.client.codec.StringCodec)6 ByteBuf (io.netty.buffer.ByteBuf)5 java.util.concurrent (java.util.concurrent)5 CompletableFuture (java.util.concurrent.CompletableFuture)5 PubSubPatternStatusListener (org.redisson.PubSubPatternStatusListener)5 org.redisson.client (org.redisson.client)5 PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)5