All Downloads are FREE. Search and download functionalities are using the official Maven repository.

br.com.esec.icpm.libs.signature.response.polling.BasePollingService Maven / Gradle / Ivy

Go to download

This library is used to make integration with Certillion server, so our Clients can easily ask for signatures or generate certificates.

There is a newer version: 1.2.0
Show newest version
package br.com.esec.icpm.libs.signature.response.polling;

import java.util.concurrent.TimeUnit;

import com.google.common.util.concurrent.SettableFuture;

import br.com.esec.icpm.libs.Server;
import br.com.esec.icpm.libs.signature.response.Futures;
import br.com.esec.icpm.libs.signature.response.ThreadPool;

public abstract class BasePollingService {

	protected final int WAIT_TIME = 2 * 1000; // 2s
	protected final int MULTIPLIER_FACTOR_WAIT_TIME = 2;

	protected void schedule(final BaseChecker checker) {
		schedule(checker, 0);
	}

	protected void schedule(final BaseChecker checker, int tries) {
		final int waitTime = WAIT_TIME + (WAIT_TIME * (MULTIPLIER_FACTOR_WAIT_TIME * (tries / 25)));
		ThreadPool.instance.schedule(checker, waitTime, TimeUnit.MILLISECONDS);
	}

	protected abstract class BaseChecker implements Runnable {

		protected static final int MAX_EXECUTIONS = 1000;

		protected Server server;
		protected long transactionId;

		protected int counter = 0;

		public BaseChecker(Server server, long transactionId) {
			this.server = server;
			this.transactionId = transactionId;
		}

		@SuppressWarnings("unchecked")
		@Override
		public void run() {
			SettableFuture future = (SettableFuture) Futures.get(server, transactionId);

			// No one waiting for it
			if (future == null || future.isDone() || future.isCancelled()) {
				Futures.remove(server, transactionId);
				return;
			}

			//
			if (counter == MAX_EXECUTIONS) {
				Futures.remove(server, transactionId);
				return;
			}

			check(future);

			counter++;
		}

		protected abstract void check(SettableFuture future);
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy