From 184d93c72e231b86aadeb5813fb6091afb68625e Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 4 Jul 2022 09:36:44 +0300 Subject: [PATCH] Feature - ExecutorOptions.idGenerator() method added #3707 --- .../org/redisson/RedissonExecutorService.java | 19 ++++----- .../org/redisson/api/ExecutorOptions.java | 17 ++++++++ .../java/org/redisson/api/IdGenerator.java | 42 +++++++++++++++++++ .../org/redisson/api/RandomIdGenerator.java | 37 ++++++++++++++++ 4 files changed, 105 insertions(+), 10 deletions(-) create mode 100644 redisson/src/main/java/org/redisson/api/IdGenerator.java create mode 100644 redisson/src/main/java/org/redisson/api/RandomIdGenerator.java diff --git a/redisson/src/main/java/org/redisson/RedissonExecutorService.java b/redisson/src/main/java/org/redisson/RedissonExecutorService.java index 707eede34..8fde94aad 100644 --- a/redisson/src/main/java/org/redisson/RedissonExecutorService.java +++ b/redisson/src/main/java/org/redisson/RedissonExecutorService.java @@ -99,9 +99,12 @@ public class RedissonExecutorService implements RScheduledExecutorService { private final ReferenceQueue> referenceDueue = new ReferenceQueue<>(); private final Collection references = Collections.newSetFromMap(new ConcurrentHashMap<>()); + + private final IdGenerator idGenerator; public RedissonExecutorService(Codec codec, CommandAsyncExecutor commandExecutor, Redisson redisson, - String name, QueueTransferService queueTransferService, ConcurrentMap responses, ExecutorOptions options) { + String name, QueueTransferService queueTransferService, ConcurrentMap responses, + ExecutorOptions options) { super(); this.codec = codec; this.commandExecutor = commandExecutor; @@ -169,6 +172,8 @@ public class RedissonExecutorService implements RScheduledExecutorService { scheduledRemoteService.setTasksRetryInterval(options.getTaskRetryInterval()); asyncScheduledService = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RESULT_OPTIONS); asyncScheduledServiceAtFixed = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RemoteInvocationOptions.defaults().noAck().noResult()); + + idGenerator = options.getIdGenerator(); } protected String generateActiveWorkersId() { @@ -616,23 +621,17 @@ public class RedissonExecutorService implements RScheduledExecutorService { return new RedissonExecutorBatchFuture(future, result); } - private String generateId() { - byte[] id = new byte[16]; - ThreadLocalRandom.current().nextBytes(id); - return ByteBufUtil.hexDump(id); - } - protected TaskParameters createTaskParameters(Callable task) { ClassBody classBody = getClassBody(task); byte[] state = encode(task); - String id = "00" + generateId(); + String id = "00" + idGenerator.generateId(); return new TaskParameters(id, classBody.getClazzName(), classBody.getClazz(), classBody.getLambda(), state); } protected TaskParameters createTaskParameters(Runnable task) { ClassBody classBody = getClassBody(task); byte[] state = encode(task); - String id = "00" + generateId(); + String id = "00" + idGenerator.generateId(); return new TaskParameters(id, classBody.getClazzName(), classBody.getClazz(), classBody.getLambda(), state); } @@ -974,7 +973,7 @@ public class RedissonExecutorService implements RScheduledExecutorService { } private String generateScheduledTaskId() { - return "01" + generateId(); + return "01" + idGenerator.generateId(); } @Override diff --git a/redisson/src/main/java/org/redisson/api/ExecutorOptions.java b/redisson/src/main/java/org/redisson/api/ExecutorOptions.java index ab1c45f40..12c8e713e 100644 --- a/redisson/src/main/java/org/redisson/api/ExecutorOptions.java +++ b/redisson/src/main/java/org/redisson/api/ExecutorOptions.java @@ -27,6 +27,8 @@ public final class ExecutorOptions { private long taskRetryInterval = 5 * 60000; + private IdGenerator idGenerator = IdGenerator.random(); + private ExecutorOptions() { } @@ -55,4 +57,19 @@ public final class ExecutorOptions { return this; } + public IdGenerator getIdGenerator() { + return idGenerator; + } + + /** + * Defines identifier generator + * + * @param idGenerator identifier generator + * @return self instance + */ + public ExecutorOptions idGenerator(IdGenerator idGenerator) { + this.idGenerator = idGenerator; + return this; + } + } diff --git a/redisson/src/main/java/org/redisson/api/IdGenerator.java b/redisson/src/main/java/org/redisson/api/IdGenerator.java new file mode 100644 index 000000000..7a620e508 --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/IdGenerator.java @@ -0,0 +1,42 @@ +/** + * Copyright (c) 2013-2021 Nikita Koksharov + * + * 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.api; + +/** + * Identifier generator + * + * @author Nikita Koksharov + * + */ +public interface IdGenerator { + + /** + * Generates identifier + * + * @return identifier + */ + String generateId(); + + /** + * Returns random identifier generator. Used by default. + * + * @return random identifier generator + */ + static IdGenerator random() { + return new RandomIdGenerator(); + } + +} diff --git a/redisson/src/main/java/org/redisson/api/RandomIdGenerator.java b/redisson/src/main/java/org/redisson/api/RandomIdGenerator.java new file mode 100644 index 000000000..54aecf76e --- /dev/null +++ b/redisson/src/main/java/org/redisson/api/RandomIdGenerator.java @@ -0,0 +1,37 @@ +/** + * Copyright (c) 2013-2021 Nikita Koksharov + * + * 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.api; + +import io.netty.buffer.ByteBufUtil; + +import java.util.concurrent.ThreadLocalRandom; + +/** + * Random identifier + * + * @author Nikita Koksharov + * + */ +public class RandomIdGenerator implements IdGenerator { + + @Override + public String generateId() { + byte[] id = new byte[16]; + ThreadLocalRandom.current().nextBytes(id); + return ByteBufUtil.hexDump(id); + } + +}