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));
}
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));
}
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;
}
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();
}
}
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));
}
Aggregations