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

com.groupbyinc.common.directbeacon.DirectBeaconClient Maven / Gradle / Ivy

There is a newer version: 198
Show newest version
package com.groupbyinc.common.directbeacon;

import com.groupbyinc.common.jackson.Mappers;
import com.groupbyinc.common.util.ThreadUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class DirectBeaconClient implements AutoCloseable {

  public static final DirectBeaconClient EMPTY = new DirectBeaconClient() {
    @Override
    public  void send(String uriString, DirectBeacon beacon, String authToken) {
      //do nothing
    }
  };
  private static final int CONNECT_TIMEOUT = 1000;
  private static final int CONNECTION_REQUEST_TIMEOUT = 5000;
  private static final int SOCKET_TIMEOUT = 1000;

  private static class DirectBeaconClientQueue {

    private static DirectBeaconClientQueue instance = new DirectBeaconClientQueue();

    private ExecutorService queue = Executors.newFixedThreadPool(100, ThreadUtils.defaultThreadFactory("directbeacon"));

    private DirectBeaconClientQueue() {

    }

    protected static DirectBeaconClientQueue getInstance() {
      return instance;
    }

    protected void offer(final DirectBeaconClientTask task) {
      queue.submit(task);
    }
  }

  private final CloseableHttpClient httpClient;

  public DirectBeaconClient() {
    this(createHttpClient());
  }

  public DirectBeaconClient(CloseableHttpClient httpClient) {
    this.httpClient = httpClient;
  }

  private static CloseableHttpClient createHttpClient() {
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(200);
    cm.setDefaultMaxPerRoute(200);
    HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    RequestConfig requestConfig = RequestConfig.custom()
        .setConnectTimeout(CONNECT_TIMEOUT)
        .setConnectionRequestTimeout(CONNECTION_REQUEST_TIMEOUT)
        .setSocketTimeout(SOCKET_TIMEOUT)
        .build();
    return clientBuilder.setConnectionManager(cm)
        .setDefaultRequestConfig(requestConfig)
        .build();
  }

  public  void send(String uriString, final DirectBeacon beacon, String authToken) {
    URI uri;
    try {
      uri = new URI(uriString);
    } catch (URISyntaxException e) {
      throw new IllegalStateException("Couldn't send beacon", e);
    }

    validate(beacon);

    DirectBeaconClientQueue queue = DirectBeaconClientQueue.getInstance();
    queue.offer(new DirectBeaconClientTask<>(httpClient, uri, beacon, authToken));
  }

  public  void validate(DirectBeacon beacon) {
    if (beacon == null) {
      throw new IllegalStateException("No beacon to send.");
    } else if (StringUtils.isBlank(beacon.getResponseId())) {
      throw new IllegalStateException("No responseId found in beacon. You must define a responseId or we can't use this information.");
    } else if (StringUtils.isBlank(beacon.getCustomerId())) {
      throw new IllegalStateException("No customerId found in beacon. You must define a customerId to send this beacon. \n\n" + Mappers.writeValueAsString(beacon));
    }
  }

  @Override
  public void close() throws Exception {
    if (httpClient != null) {
      httpClient.close();
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy