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

com.github.ddth.queue.jclient.impl.ThriftHttpQueueClientFactory Maven / Gradle / Ivy

package com.github.ddth.queue.jclient.impl;

import java.util.concurrent.ExecutionException;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.cache.RemovalListener;
import com.google.common.cache.RemovalNotification;

/**
 * Factory to create {@link ThriftHttpQueueClient}.
 * 
 * @author Thanh Nguyen 
 * @since 0.1.0
 */
public class ThriftHttpQueueClientFactory {
    private static LoadingCache cache = CacheBuilder.newBuilder()
            .removalListener(new RemovalListener() {
                @Override
                public void onRemoval(
                        RemovalNotification notification) {
                    notification.getValue().destroy();
                }
            }).build(new CacheLoader() {
                @Override
                public ThriftHttpQueueClient load(String urls) throws Exception {
                    ThriftHttpQueueClient queueClient = new ThriftHttpQueueClient(urls);
                    queueClient.init();
                    return queueClient;
                }
            });

    public static void cleanup() {
        cache.invalidateAll();
    }

    /**
     * Helper method to create a new {@link ThriftHttpQueueClient} instance.
     * 
     * @param urls
     *            format
     *            {@code http://host1:port1/uri1,https://host2:port2/uri2,http://host3:port3/uri3...}
     * @return
     */
    public static ThriftHttpQueueClient newQueueClient(String urls) {
        try {
            return cache.get(urls);
        } catch (ExecutionException e) {
            return null;
        }
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy