Search in sources :

Example 11 with Codec

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

the class RedissonObjectBuilder method storeAsync.

public void storeAsync(RObject ar, String fieldName, RMap<String, Object> liveMap) {
    Codec codec = ar.getCodec();
    if (codec != null) {
        codecProvider.registerCodec((Class) codec.getClass(), codec);
    }
    liveMap.fastPutAsync(fieldName, new RedissonReference(ar.getClass(), ar.getName(), codec));
}
Also used : Codec(org.redisson.client.codec.Codec)

Example 12 with Codec

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

the class RedissonObjectBuilder method store.

public void store(RObject ar, String fieldName, RMap<String, Object> liveMap) {
    Codec codec = ar.getCodec();
    if (codec != null) {
        codecProvider.registerCodec((Class) codec.getClass(), codec);
    }
    liveMap.fastPut(fieldName, new RedissonReference(ar.getClass(), ar.getName(), codec));
}
Also used : Codec(org.redisson.client.codec.Codec)

Example 13 with Codec

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

the class RedissonObjectBuilder method createObject.

public RObject createObject(Object id, Class<?> clazz, Class<?> fieldType, String fieldName) {
    Class<? extends RObject> mappedClass = getMappedClass(fieldType);
    try {
        if (mappedClass != null) {
            Codec fieldCodec = getFieldCodec(clazz, mappedClass, fieldName);
            NamingScheme fieldNamingScheme = getNamingScheme(clazz, fieldCodec);
            String referenceName = fieldNamingScheme.getFieldReferenceName(clazz, id, mappedClass, fieldName);
            return createRObject(redisson, mappedClass, referenceName, fieldCodec);
        }
    } catch (Exception e) {
        throw new IllegalArgumentException(e);
    }
    return null;
}
Also used : Codec(org.redisson.client.codec.Codec) NamingScheme(org.redisson.liveobject.resolver.NamingScheme)

Example 14 with Codec

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

the class TasksRunnerService method decode.

@SuppressWarnings("unchecked")
private <T> T decode(TaskParameters params) {
    ByteBuf classBodyBuf = Unpooled.wrappedBuffer(params.getClassBody());
    ByteBuf stateBuf = Unpooled.wrappedBuffer(params.getState());
    try {
        HashValue hash = new HashValue(Hash.hash128(classBodyBuf));
        Codec classLoaderCodec = CODECS.get(hash);
        if (classLoaderCodec == null) {
            RedissonClassLoader cl = new RedissonClassLoader(codec.getClassLoader());
            cl.loadClass(params.getClassName(), params.getClassBody());
            classLoaderCodec = this.codec.getClass().getConstructor(ClassLoader.class).newInstance(cl);
            CODECS.put(hash, classLoaderCodec);
        }
        T task;
        if (params.getLambdaBody() != null) {
            ByteArrayInputStream is = new ByteArrayInputStream(params.getLambdaBody());
            // set thread context class loader to be the classLoaderCodec.getClassLoader() variable as there could be reflection
            // done while reading from input stream which reflection will use thread class loader to load classes on demand
            ClassLoader currentThreadClassLoader = Thread.currentThread().getContextClassLoader();
            try {
                Thread.currentThread().setContextClassLoader(classLoaderCodec.getClassLoader());
                ObjectInput oo = new CustomObjectInputStream(classLoaderCodec.getClassLoader(), is);
                task = (T) oo.readObject();
                oo.close();
            } finally {
                Thread.currentThread().setContextClassLoader(currentThreadClassLoader);
            }
        } else {
            task = (T) classLoaderCodec.getValueDecoder().decode(stateBuf, null);
        }
        Injector.inject(task, RedissonClient.class, redisson);
        Injector.inject(task, String.class, params.getRequestId());
        if (tasksInjector != null) {
            tasksInjector.inject(task);
        }
        return task;
    } catch (Exception e) {
        throw new IllegalStateException("Unable to initialize codec with ClassLoader parameter", e);
    } finally {
        classBodyBuf.release();
        stateBuf.release();
    }
}
Also used : Codec(org.redisson.client.codec.Codec) StringCodec(org.redisson.client.codec.StringCodec) LongCodec(org.redisson.client.codec.LongCodec) ByteArrayInputStream(java.io.ByteArrayInputStream) ObjectInput(java.io.ObjectInput) ByteBuf(io.netty.buffer.ByteBuf) HashValue(org.redisson.misc.HashValue) CustomObjectInputStream(org.redisson.codec.CustomObjectInputStream) RedisException(org.redisson.client.RedisException) RedissonShutdownException(org.redisson.RedissonShutdownException) ExecutionException(java.util.concurrent.ExecutionException)

Example 15 with Codec

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

the class PublishSubscribeService method psubscribe.

public CompletableFuture<Collection<PubSubConnectionEntry>> psubscribe(ChannelName channelName, Codec codec, RedisPubSubListener<?>... listeners) {
    if (isMultiEntity(channelName)) {
        Collection<MasterSlaveEntry> entrySet = connectionManager.getEntrySet();
        AtomicInteger statusCounter = new AtomicInteger(entrySet.size());
        RedisPubSubListener[] ls = Arrays.stream(listeners).map(l -> {
            if (l instanceof PubSubPatternStatusListener) {
                return new PubSubPatternStatusListener((PubSubPatternStatusListener) l) {

                    @Override
                    public boolean onStatus(PubSubType type, CharSequence channel) {
                        if (statusCounter.decrementAndGet() == 0) {
                            return super.onStatus(type, channel);
                        }
                        return true;
                    }
                };
            }
            return l;
        }).toArray(RedisPubSubListener[]::new);
        List<CompletableFuture<PubSubConnectionEntry>> futures = new ArrayList<>();
        for (MasterSlaveEntry entry : entrySet) {
            CompletableFuture<PubSubConnectionEntry> future = subscribe(PubSubType.PSUBSCRIBE, codec, channelName, entry, ls);
            futures.add(future);
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        return future.thenApply(r -> {
            return futures.stream().map(v -> v.getNow(null)).collect(Collectors.toList());
        });
    }
    CompletableFuture<PubSubConnectionEntry> f = subscribe(PubSubType.PSUBSCRIBE, codec, channelName, getEntry(channelName), listeners);
    return f.thenApply(res -> Collections.singletonList(res));
}
Also used : 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener)

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