Skip to content

Instantly share code, notes, and snippets.

View soverby's full-sized avatar

Sean Overby soverby

  • US
View GitHub Profile
@soverby
soverby / ThrottledExecutor.java
Last active March 16, 2017 22:13
I needed a way to throttle an executor so I didn't overload a receiver (ElasticSearch - doing multi-threaded bulk updates to it).
package com.soverby.test;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.IntStream;
public class ThrottledExecutor {
@soverby
soverby / AccessPool.java
Last active March 17, 2017 12:47
I needed a way to manage a limited pool of some resource across many threads. There are a few things about this that bug me, it's got some smells but it's 'ok' as an implementation imo.
package com.soverby.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Semaphore;
import java.util.concurrent.locks.ReentrantLock;
public class AccessPool<T> {
@soverby
soverby / TimingSupplier.java
Last active April 18, 2018 23:29
Manages one or more timers, I primarily use this for timing code execution.
package com.soverby.test;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Supplier;
import static java.lang.String.format;
@soverby
soverby / ClientHttpPoolConfiguration.java
Last active February 27, 2024 15:54
RestTemplate backed by Apache HttpClient Connection Pool. Addresses RestTemplate HttpConnection exhaustion. Note this implementation is not route tunable and should be expanded when tuning by route is required. Example error: I/O error on POST request for "https://someendpoint": Timeout waiting for connection from pool; nested exception is org.a…
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 org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class ClientHttpPoolConfiguration {
// Example of a fully imperative approach to writing Java code...
public class FullyImperative {
// @Inject
private final ItemRepository itemRepository;
// @Inject
private final InventoryLocationRepostitory inventoryLocationRepostitory;
// Find the first available InventoryLocation for a given item id.
public InventoryLocation itemByFirstAvailable(String itemId)
// Somewhere on the road from imperative to functional...
public class PartiallyImperative {
// @Inject
private ItemRepository itemRepository;
// @Inject
private InventoryLocationRepostitory;
// Optionally return inventory location with available inventory for item with the given id
public Optional<InventoryLocation> itemByFirstAvailable(String itemId) throws NotFoundException {
// And finally a functional example of the find location with non-zero inventory for item example
public class Functional {
// @Inject
private ItemRepository itemRepository;
// @Inject
private InventoryLocationRepostitory inventoryLocationRepostitory;
// Monadic approach, composed behaviors
// "The monad represents computations with a sequential structure: a monad defines what it means to chain operations together."
public class OptionalChaining {
public static void main(String[] args) {
OptionalChaining optionalChaining = new OptionalChaining();
optionalChaining.happyPath();
optionalChaining.nullInput();
optionalChaining.thereBeNullsHereOne();
optionalChaining.thereBeNullsHereTwo();
}
// Happy path with a pipeline that doesn't return nulls
public class OptionalChainingComposed {
public static void main(String[] args) {
OptionalChainingComposed optionalChaining = new OptionalChainingComposed();
optionalChaining.happyPath();
optionalChaining.nullInput();
}
public void happyPath() {
nonNullPipeline("happyPath")
@soverby
soverby / JavaLambdaSyntax
Created May 17, 2018 11:25
Basic Java Lambda Syntax Variants
import java.util.function.BiFunction;
import java.util.function.Consumer;
import java.util.function.Supplier;
public class JavaLambdaSyntax {
private Consumer<String> stringConsumer;
private Supplier<String> stringSupplier;
private BiFunction<String, String, String> stringBiFunction;