Feature - ExecutorOptions.idGenerator() method added #3707

pull/4428/head
Nikita Koksharov 3 years ago
parent 32a839fb8c
commit 184d93c72e

@ -100,8 +100,11 @@ public class RedissonExecutorService implements RScheduledExecutorService {
private final ReferenceQueue<RExecutorFuture<?>> referenceDueue = new ReferenceQueue<>(); private final ReferenceQueue<RExecutorFuture<?>> referenceDueue = new ReferenceQueue<>();
private final Collection<RedissonExecutorFutureReference> references = Collections.newSetFromMap(new ConcurrentHashMap<>()); private final Collection<RedissonExecutorFutureReference> references = Collections.newSetFromMap(new ConcurrentHashMap<>());
private final IdGenerator idGenerator;
public RedissonExecutorService(Codec codec, CommandAsyncExecutor commandExecutor, Redisson redisson, public RedissonExecutorService(Codec codec, CommandAsyncExecutor commandExecutor, Redisson redisson,
String name, QueueTransferService queueTransferService, ConcurrentMap<String, ResponseEntry> responses, ExecutorOptions options) { String name, QueueTransferService queueTransferService, ConcurrentMap<String, ResponseEntry> responses,
ExecutorOptions options) {
super(); super();
this.codec = codec; this.codec = codec;
this.commandExecutor = commandExecutor; this.commandExecutor = commandExecutor;
@ -169,6 +172,8 @@ public class RedissonExecutorService implements RScheduledExecutorService {
scheduledRemoteService.setTasksRetryInterval(options.getTaskRetryInterval()); scheduledRemoteService.setTasksRetryInterval(options.getTaskRetryInterval());
asyncScheduledService = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RESULT_OPTIONS); asyncScheduledService = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RESULT_OPTIONS);
asyncScheduledServiceAtFixed = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RemoteInvocationOptions.defaults().noAck().noResult()); asyncScheduledServiceAtFixed = scheduledRemoteService.get(RemoteExecutorServiceAsync.class, RemoteInvocationOptions.defaults().noAck().noResult());
idGenerator = options.getIdGenerator();
} }
protected String generateActiveWorkersId() { protected String generateActiveWorkersId() {
@ -616,23 +621,17 @@ public class RedissonExecutorService implements RScheduledExecutorService {
return new RedissonExecutorBatchFuture(future, result); 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) { protected TaskParameters createTaskParameters(Callable<?> task) {
ClassBody classBody = getClassBody(task); ClassBody classBody = getClassBody(task);
byte[] state = encode(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); return new TaskParameters(id, classBody.getClazzName(), classBody.getClazz(), classBody.getLambda(), state);
} }
protected TaskParameters createTaskParameters(Runnable task) { protected TaskParameters createTaskParameters(Runnable task) {
ClassBody classBody = getClassBody(task); ClassBody classBody = getClassBody(task);
byte[] state = encode(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); return new TaskParameters(id, classBody.getClazzName(), classBody.getClazz(), classBody.getLambda(), state);
} }
@ -974,7 +973,7 @@ public class RedissonExecutorService implements RScheduledExecutorService {
} }
private String generateScheduledTaskId() { private String generateScheduledTaskId() {
return "01" + generateId(); return "01" + idGenerator.generateId();
} }
@Override @Override

@ -27,6 +27,8 @@ public final class ExecutorOptions {
private long taskRetryInterval = 5 * 60000; private long taskRetryInterval = 5 * 60000;
private IdGenerator idGenerator = IdGenerator.random();
private ExecutorOptions() { private ExecutorOptions() {
} }
@ -55,4 +57,19 @@ public final class ExecutorOptions {
return this; 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;
}
} }

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

@ -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);
}
}
Loading…
Cancel
Save