Search in sources :

Example 26 with Codec

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

the class RedissonCodecTest method testSnappyBigV2.

@Test
public void testSnappyBigV2() throws IOException {
    Codec sc = new SnappyCodecV2();
    String randomData = RandomString.make(Short.MAX_VALUE * 2 + 142);
    ByteBuf g = sc.getValueEncoder().encode(randomData);
    String decompressedData = (String) sc.getValueDecoder().decode(g, null);
    assertThat(decompressedData).isEqualTo(randomData);
}
Also used : Codec(org.redisson.client.codec.Codec) RandomString(net.bytebuddy.utility.RandomString) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.jupiter.api.Test)

Example 27 with Codec

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

the class TestDefaultTenant method testExternalizable.

@Test(groups = "fast")
public void testExternalizable() throws IOException {
    final DefaultTenant tenantdata = new DefaultTenant(UUID.randomUUID(), clock.getUTCNow(), clock.getUTCNow(), "er44TT-yy4r", "TTR445ee2", null);
    final Codec code = new SerializationCodec();
    final ByteBuf byteBuf = code.getValueEncoder().encode(tenantdata);
    final DefaultTenant tenantData2 = (DefaultTenant) code.getValueDecoder().decode(byteBuf, null);
    Assert.assertEquals(tenantData2, tenantdata);
}
Also used : Codec(org.redisson.client.codec.Codec) SerializationCodec(org.redisson.codec.SerializationCodec) SerializationCodec(org.redisson.codec.SerializationCodec) ByteBuf(io.netty.buffer.ByteBuf) Test(org.testng.annotations.Test)

Example 28 with Codec

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

the class RedissonFactory method cache.

@EachBean(RedissonCacheConfiguration.class)
public RedissonSyncCache cache(@Parameter RedissonCacheConfiguration configuration, RedissonClient redisson, ConversionService<?> conversionService, @Named(TaskExecutors.IO) ExecutorService executorService) {
    Codec codec = Optional.ofNullable(configuration.getCodec()).orElse(redisson.getConfig().getCodec());
    if (configuration.getExpireAfterAccess().toMillis() != 0 || configuration.getExpireAfterWrite().toMillis() != 0 || configuration.getMaxSize() != 0) {
        RMapCache<Object, Object> mapCache = redisson.getMapCache(configuration.getName(), codec);
        return new RedissonSyncCache(conversionService, mapCache, mapCache, executorService, configuration);
    }
    RMap<Object, Object> map = redisson.getMap(configuration.getName(), codec);
    return new RedissonSyncCache(conversionService, null, map, executorService, configuration);
}
Also used : Codec(org.redisson.client.codec.Codec) RedissonSyncCache(org.redisson.micronaut.cache.RedissonSyncCache)

Example 29 with Codec

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

the class RedissonObjectBuilder method fillCodecMethods.

private static void fillCodecMethods(Map<Class<?>, CodecMethodRef> map, Class<?> clientClazz, Class<?> objectClazz) {
    for (Method method : clientClazz.getDeclaredMethods()) {
        if (!method.getReturnType().equals(Void.TYPE) && objectClazz.isAssignableFrom(method.getReturnType()) && method.getName().startsWith("get")) {
            Class<?> cls = method.getReturnType();
            if (!map.containsKey(cls)) {
                map.put(cls, new CodecMethodRef());
            }
            CodecMethodRef builder = map.get(cls);
            if (// first param is name, second param is codec.
            method.getParameterTypes().length == 2 && Codec.class.isAssignableFrom(method.getParameterTypes()[1])) {
                builder.customCodecMethod = method;
            } else if (method.getParameterTypes().length == 1) {
                builder.defaultCodecMethod = method;
            }
        }
    }
}
Also used : Codec(org.redisson.client.codec.Codec) Method(java.lang.reflect.Method)

Example 30 with Codec

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

the class PublishSubscribeService method unsubscribe.

private CompletableFuture<Codec> unsubscribe(ChannelName channelName, MasterSlaveEntry e, PubSubType topicType) {
    if (connectionManager.isShuttingDown()) {
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Codec> result = new CompletableFuture<>();
    AsyncSemaphore lock = getSemaphore(channelName);
    lock.acquire(() -> {
        PubSubConnectionEntry entry = name2PubSubConnection.remove(new PubSubKey(channelName, e));
        if (entry == null) {
            lock.release();
            result.complete(null);
            return;
        }
        freePubSubLock.acquire(() -> {
            PubSubEntry ee = entry2PubSubConnection.getOrDefault(e, new PubSubEntry());
            Queue<PubSubConnectionEntry> freePubSubConnections = ee.getEntries();
            freePubSubConnections.remove(entry);
            freePubSubLock.release();
            Codec entryCodec;
            if (topicType == PubSubType.PUNSUBSCRIBE) {
                entryCodec = entry.getConnection().getPatternChannels().get(channelName);
            } else {
                entryCodec = entry.getConnection().getChannels().get(channelName);
            }
            AtomicBoolean executed = new AtomicBoolean();
            RedisPubSubListener<Object> listener = new BaseRedisPubSubListener() {

                @Override
                public boolean onStatus(PubSubType type, CharSequence channel) {
                    if (type == topicType && channel.equals(channelName)) {
                        executed.set(true);
                        lock.release();
                        result.complete(entryCodec);
                        return true;
                    }
                    return false;
                }
            };
            ChannelFuture future;
            if (topicType == PubSubType.PUNSUBSCRIBE) {
                future = entry.punsubscribe(channelName, listener);
            } else {
                future = entry.unsubscribe(channelName, listener);
            }
            future.addListener((ChannelFutureListener) f -> {
                if (!f.isSuccess()) {
                    return;
                }
                connectionManager.newTimeout(timeout -> {
                    if (executed.get()) {
                        return;
                    }
                    entry.getConnection().onMessage(new PubSubStatusMessage(topicType, channelName));
                }, config.getTimeout(), TimeUnit.MILLISECONDS);
            });
        });
    });
    return result;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) Timeout(io.netty.util.Timeout) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) java.util(java.util) Logger(org.slf4j.Logger) Codec(org.redisson.client.codec.Codec) ConnectionManager(org.redisson.connection.ConnectionManager) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener) LoggerFactory(org.slf4j.LoggerFactory) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collectors(java.util.stream.Collectors) ChannelFuture(io.netty.channel.ChannelFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Codec(org.redisson.client.codec.Codec) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

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