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

com.microsoft.azure.servicebus.primitives.AsyncUtil Maven / Gradle / Ivy

Go to download

Java library for Azure Service Bus. Please note, a newer package com.azure:azure-messaging-servicebus for Azure Service Bus is available as of December 2020. While this package will continue to receive critical bug fixes, we strongly encourage you to upgrade. Read the migration guide at https://aka.ms/azsdk/java/migrate/sb for more details.

There is a newer version: 3.6.7
Show newest version
package com.microsoft.azure.servicebus.primitives;

import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

// To complete futures using a different thread. Otherwise every future is completed on the single reactor thread
// which badly affects perf and a client can potentially kill the thread or lock the thread.
class AsyncUtil {
	
	public static  boolean completeFutureAndGetStatus(CompletableFuture future, T result)
	{
		try {
			return MessagingFactory.INTERNAL_THREAD_POOL.submit(new CompleteCallable(future, result)).get();
		} catch (InterruptedException | ExecutionException e) {			
			e.printStackTrace();
			return false;
		}				
	}
	
	public static  void completeFuture(CompletableFuture future, T result)
	{
		MessagingFactory.INTERNAL_THREAD_POOL.submit(new CompleteCallable(future, result));			
	}
	
	public static  boolean completeFutureExceptionallyAndGetStatus(CompletableFuture future, Throwable exception)
	{
		try {
			return MessagingFactory.INTERNAL_THREAD_POOL.submit(new CompleteExceptionallyCallable(future, exception)).get();
		} catch (InterruptedException | ExecutionException e) {			
			e.printStackTrace();
			return false;
		}				
	}
	
	public static  void completeFutureExceptionally(CompletableFuture future, Throwable exception)
	{
		MessagingFactory.INTERNAL_THREAD_POOL.submit(new CompleteExceptionallyCallable(future, exception));
	}
	
	public static void run(Runnable runnable)
	{
		MessagingFactory.INTERNAL_THREAD_POOL.submit(runnable);
	}
	
	private static class CompleteCallable implements Callable
	{
		private CompletableFuture future;
		private T result;
		
		CompleteCallable(CompletableFuture future, T result)
		{
			this.future = future;
			this.result = result;
		}
		
		@Override
		public Boolean call() throws Exception {
			return this.future.complete(this.result);
		}		
	}
	
	private static class CompleteExceptionallyCallable implements Callable
	{
		private CompletableFuture future;
		private Throwable exception;
		
		CompleteExceptionallyCallable(CompletableFuture future, Throwable exception)
		{
			this.future = future;
			this.exception = exception;
		}
		
		@Override
		public Boolean call() throws Exception {
			return this.future.completeExceptionally(this.exception);
		}		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy