我正在开展一个项目,我需要做以下事情:
RetryStrategy
重试发送相同的数据。pending
队列中删除,以便它不会被重试,如果由于某种原因没有收到它,那么我们将重新使用RetryStrategy重新发送相同的数据我们使用。例如:如果我们发送了byteArrayA
,其唯一的长地址为addressA
,并且如果它是在其他系统上收到的,那么我的轮询线程会将此addressA
恢复为确认意味着已收到,所以现在我们可以从待处理队列中删除此地址,以便它不会再次重试。
我有两个RetryStrategy
已实施ConstantBackoff
和ExponentialBackoff
。所以我想出了下面的模拟器,它模拟了上面的流程。
public class Experimental {
/** Return the desired backoff delay in millis for the given retry number, which is 1-based. */
interface RetryStrategy {
long getDelayMs(int retry);
}
public enum ConstantBackoff implements RetryStrategy {
INSTANCE;
@Override
public long getDelayMs(int retry) {
return 1000L;
}
}
public enum ExponentialBackoff implements RetryStrategy {
INSTANCE;
@Override
public long getDelayMs(int retry) {
return 100 + (1L << retry);
}
}
/** A container that sends messages with retries. */
private static class Sender {
private final ScheduledExecutorService executorService = Executors.newScheduledThreadPool(20);
private final ConcurrentMap<Long, Retrier> pending = new ConcurrentHashMap<>();
/** Send the given (simulated) data with given address on the given socket. */
void sendTo(long addr, byte[] data, int socket) {
System.err.println("Sending " + Arrays.toString(data) + "@" + addr + " on " + socket);
}
/** The state of a message that's being retried. */
private class Retrier implements Runnable {
private final RetryStrategy retryStrategy;
private final long addr;
private final byte[] data;
private final int socket;
private int retry;
private Future<?> future;
Retrier(RetryStrategy retryStrategy, long addr, byte[] data, int socket) {
this.retryStrategy = retryStrategy;
this.addr = addr;
this.data = data;
this.socket = socket;
this.retry = 0;
}
private synchronized void start() {
if (future == null) {
future = executorService.submit(this);
pending.put(addr, this);
}
}
private synchronized void cancel() {
if (future != null) {
future.cancel(true);
future = null;
}
}
private synchronized void reschedule() {
if (future != null) {
future = executorService.schedule(this, retryStrategy.getDelayMs(++retry), MILLISECONDS);
}
}
@Override
synchronized public void run() {
sendTo(addr, data, socket);
reschedule();
}
}
/**
* Get a (simulated) verified message address. Just picks a pending
* one. Returns zero if none left.
*/
long getVerifiedAddr() {
System.err.println("Pending messages: " + pending.size());
Iterator<Long> i = pending.keySet().iterator();
long addr = i.hasNext() ? i.next() : 0;
return addr;
}
/** A polling loop that cancels retries of (simulated) verified messages. */
class CancellationPoller implements Runnable {
@Override
public void run() {
while (!Thread.currentThread().isInterrupted()) {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
long addr = getVerifiedAddr();
if (addr == 0) {
continue;
}
System.err.println("Verified message (to be cancelled) " + addr);
Retrier retrier = pending.remove(addr);
if (retrier != null) {
retrier.cancel();
}
}
}
}
private Sender initialize() {
executorService.submit(new CancellationPoller());
return this;
}
private void sendWithRetriesTo(RetryStrategy retryStrategy, long addr, byte[] data, int socket) {
new Retrier(retryStrategy, addr, data, socket).start();
}
}
public static void main(String[] args) {
Sender sender = new Sender().initialize();
for (long i = 1; i <= 10; i++) {
sender.sendWithRetriesTo(ConstantBackoff.INSTANCE, i, null, 42);
}
for (long i = -1; i >= -10; i--) {
sender.sendWithRetriesTo(ExponentialBackoff.INSTANCE, i, null, 37);
}
}
}
我想看看上面的代码中是否有任何竞争条件或任何线程安全问题?因为在多线程中获得正确的东西是很困难的。
让我知道是否有更好或更有效的方法来做同样的事情。
答案 0 :(得分:0)
如果您的代码中没有任何针对第三方库的内容,则可以查看此https://github.com/nurkiewicz/async-retry。假设我正确地理解了你的问题,这个lib允许你在不重新发明轮子的情况下处理这类问题。它写得非常清楚,并且有很好的文档记录,所以我希望你能找到自己的方向。 :)