Skip to content

Instantly share code, notes, and snippets.

View soverby's full-sized avatar

Sean Overby soverby

  • US
View GitHub Profile
// 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 {
// 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)
@soverby
soverby / ClientHttpPoolConfiguration.java
Last active December 31, 2024 14:22
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 {
@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 / 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 / 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 {