Skip to content
This repository was archived by the owner on May 30, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,21 @@ apply plugin: 'signing'
apply plugin: 'idea'
apply plugin: 'com.github.johnrengelman.shadow'

configurations.all {
// check for updates every build for dependencies with: 'changing: true'
resolutionStrategy.cacheChangingModulesFor 0, 'seconds'
}

repositories {
mavenCentral()
mavenLocal()
// Before LaunchDarkly release artifacts get synced to Maven Central they are here along with snapshots:
maven { url "https://oss.sonatype.org/content/groups/public/" }
mavenCentral()
}

allprojects {
group = 'com.launchdarkly'
version = "0.20.0"
version = "1.0.0-SNAPSHOT"
sourceCompatibility = 1.7
targetCompatibility = 1.7
}
Expand All @@ -24,11 +31,11 @@ dependencies {
compile "com.google.code.gson:gson:2.2.4"
compile "com.google.guava:guava:19.0"
compile "org.slf4j:slf4j-api:1.7.7"
compile "org.glassfish.jersey.media:jersey-media-sse:2.20"
compile group: "com.launchdarkly", name: "okhttp-eventsource", version: "0.1.3-SNAPSHOT", changing: true
compile "redis.clients:jedis:2.8.0"
testCompile "org.easymock:easymock:3.3"
testCompile "org.easymock:easymock:3.4"
testCompile 'junit:junit:[4.10,)'
testRuntime "org.slf4j:slf4j-simple:1.7.7"
testRuntime "ch.qos.logback:logback-classic:1.1.3"
}

jar {
Expand All @@ -50,7 +57,7 @@ buildscript {
}
dependencies {
classpath 'org.ajoberstar:gradle-git:0.12.0'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.2'
classpath 'com.github.jengelman.gradle.plugins:shadow:1.2.3'
}
}

Expand Down Expand Up @@ -125,12 +132,6 @@ uploadArchives {
description 'Official LaunchDarkly SDK for Java'
url 'https://github.com/launchdarkly/java-client'

scm {
connection 'scm:svn:http://foo.googlecode.com/svn/trunk/'
developerConnection 'scm:svn:https://foo.googlecode.com/svn/trunk/'
url 'http://foo.googlecode.com/svn/trunk/'
}

licenses {
license {
name 'The Apache License, Version 2.0'
Expand Down
37 changes: 19 additions & 18 deletions src/main/java/com/launchdarkly/client/EventProcessor.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.launchdarkly.client;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.google.gson.Gson;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.CloseableHttpResponse;
Expand All @@ -14,22 +15,34 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.*;

class EventProcessor implements Closeable {
private final ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(new DaemonThreadFactory());
private final ScheduledExecutorService scheduler;
private final Random random = new Random();
private final BlockingQueue<Event> queue;
private final String apiKey;
private final LDConfig config;
private final Consumer consumer;

EventProcessor(String apiKey, LDConfig config) {
this.apiKey = apiKey;
this.queue = new ArrayBlockingQueue<>(config.capacity);
this.consumer = new Consumer(config);
this.config = config;
ThreadFactory threadFactory = new ThreadFactoryBuilder()
.setDaemon(true)
.setNameFormat("LaunchDarkly-EventProcessor-%d")
.build();
this.scheduler = Executors.newSingleThreadScheduledExecutor(threadFactory);
this.scheduler.scheduleAtFixedRate(consumer, 0, config.flushInterval, TimeUnit.SECONDS);
}

boolean sendEvent(Event e) {
if (config.samplingInterval > 0 && random.nextInt(config.samplingInterval) != 0) {
return true;
}
return queue.offer(e);
}

Expand All @@ -43,18 +56,8 @@ public void flush() {
this.consumer.flush();
}

static class DaemonThreadFactory implements ThreadFactory {
public Thread newThread(Runnable r) {
Thread thread = new Thread(r);
thread.setDaemon(true);
return thread;
}
}

class Consumer implements Runnable {
private final Logger logger = LoggerFactory.getLogger(Consumer.class);


private final CloseableHttpClient client;
private final LDConfig config;

Expand All @@ -78,6 +81,7 @@ public void flush() {
}

private void postEvents(List<Event> events) {
logger.debug("Posting " + events.size() + " event(s)..");
CloseableHttpResponse response = null;
Gson gson = new Gson();
String json = gson.toJson(events);
Expand All @@ -95,24 +99,21 @@ private void postEvents(List<Event> events) {
if (status >= 300) {
if (status == HttpStatus.SC_UNAUTHORIZED) {
logger.error("Invalid API key");
}
else {
} else {
logger.error("Unexpected status code: " + status);
}
}
else {
logger.debug("Successfully processed events");
} else {
logger.debug("Successfully posted " + events.size() + " event(s).");
}
} catch (IOException e) {
logger.error("Unhandled exception in LaunchDarkly client", e);
logger.error("Unhandled exception in LaunchDarkly client attempting to connect to URI: " + config.eventsURI, e);
} finally {
try {
if (response != null) response.close();
} catch (IOException e) {
logger.error("Unhandled exception in LaunchDarkly client", e);
}
}

}
}
}
Loading