Comprehensive Java utility library providing infrastructure and helper classes for the Eclipse Jetty web server
npx @tessl/cli install tessl/maven-org-eclipse-jetty--jetty-util@12.0.0Eclipse Jetty Utilities is a comprehensive Java utility library that provides essential infrastructure and helper classes for the Eclipse Jetty web server. It offers a wide range of functionality including thread management, data structures, I/O utilities, security components, resource management, and networking utilities designed for high-performance, low-latency web applications.
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>12.0.21</version>
</dependency>// Core utilities
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.util.StringUtil;
import org.eclipse.jetty.util.URIUtil;
import org.eclipse.jetty.util.IO;
import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.Promise;
// Component lifecycle
import org.eclipse.jetty.util.component.LifeCycle;
import org.eclipse.jetty.util.component.ContainerLifeCycle;
// Threading
import org.eclipse.jetty.util.thread.ThreadPool;
import org.eclipse.jetty.util.thread.QueuedThreadPool;
import org.eclipse.jetty.util.thread.Scheduler;
// Data structures
import org.eclipse.jetty.util.Fields;
import org.eclipse.jetty.util.MultiMap;
import org.eclipse.jetty.util.Pool;
import org.eclipse.jetty.util.ConcurrentPool;
// Resources
import org.eclipse.jetty.util.resource.Resource;
import org.eclipse.jetty.util.resource.ResourceFactory;
import org.eclipse.jetty.util.resource.PathResourceFactory;import org.eclipse.jetty.util.BufferUtil;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
// Create and manipulate ByteBuffers
ByteBuffer buffer = BufferUtil.allocate(1024);
BufferUtil.append(buffer, "Hello World".getBytes(StandardCharsets.UTF_8));
String content = BufferUtil.toString(buffer, StandardCharsets.UTF_8);import org.eclipse.jetty.util.component.AbstractLifeCycle;
public class MyComponent extends AbstractLifeCycle {
@Override
protected void doStart() throws Exception {
// Component startup logic
super.doStart();
}
@Override
protected void doStop() throws Exception {
// Component shutdown logic
super.doStop();
}
}
// Usage
MyComponent component = new MyComponent();
component.start();
// ... use component
component.stop();import org.eclipse.jetty.util.thread.QueuedThreadPool;
// Create and configure a thread pool
QueuedThreadPool threadPool = new QueuedThreadPool();
threadPool.setMinThreads(5);
threadPool.setMaxThreads(50);
threadPool.start();
// Submit tasks
threadPool.execute(() -> {
// Task implementation
System.out.println("Task executing in: " + Thread.currentThread().getName());
});
threadPool.stop();import org.eclipse.jetty.util.Callback;
import org.eclipse.jetty.util.FutureCallback;
import org.eclipse.jetty.util.FuturePromise;
// Using callbacks for async operations
public void performAsyncOperation(Callback callback) {
// Simulate async work
new Thread(() -> {
try {
Thread.sleep(1000);
callback.succeeded();
} catch (Exception e) {
callback.failed(e);
}
}).start();
}
// Usage with Future-based callback
FutureCallback callback = new FutureCallback();
performAsyncOperation(callback);
try {
callback.get(); // Wait for completion
System.out.println("Operation completed successfully");
} catch (Exception e) {
System.out.println("Operation failed: " + e.getMessage());
}Eclipse Jetty Utilities is built around several key architectural components:
LifeCycle interface for consistent start/stop semanticsCallback and Promise interfacesResource interfacePool interfaceDumpable interface for runtime introspectionEssential utility classes for common operations including buffer manipulation, string processing, URI handling, and I/O operations.
public class BufferUtil {
public static ByteBuffer allocate(int capacity);
public static String toString(ByteBuffer buffer, Charset charset);
public static byte[] toArray(ByteBuffer buffer);
public static void writeTo(ByteBuffer buffer, OutputStream out) throws IOException;
}
public class StringUtil {
public static boolean isBlank(String str);
public static boolean isEmpty(String str);
public static String[] split(String str, char delimiter);
public static List<String> csvSplit(String str);
}Specialized data structures for high-performance web applications including multi-maps, tries, pools, and concurrent collections.
public class Fields implements Iterable<Fields.Field> {
public void add(String name, String value);
public String get(String name);
public List<String> getValues(String name);
public boolean remove(String name);
}
public interface Pool<P> {
Pool.Entry<P> acquire();
boolean release(Pool.Entry<P> entry);
int getMaxEntries();
int size();
}Comprehensive callback and promise APIs for building non-blocking, asynchronous applications with various callback implementations.
public interface Callback {
void succeeded();
void failed(Throwable x);
static Callback from(Runnable success, Consumer<Throwable> failure);
}
public interface Promise<C> extends Callback {
void succeeded(C result);
}
public class FuturePromise<C> implements Future<C>, Promise<C> {
public FuturePromise();
public C get() throws InterruptedException, ExecutionException;
public C get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException;
}Robust component lifecycle management with container support, event notifications, and graceful shutdown capabilities.
public interface LifeCycle {
void start() throws Exception;
void stop() throws Exception;
boolean isRunning();
boolean isStarted();
boolean isStopped();
boolean isFailed();
void addLifeCycleListener(LifeCycle.Listener listener);
void removeLifeCycleListener(LifeCycle.Listener listener);
}Advanced threading utilities including thread pools, schedulers, execution strategies, and concurrency primitives optimized for server applications.
public interface ThreadPool extends Executor, LifeCycle {
void join() throws InterruptedException;
int getThreads();
int getIdleThreads();
boolean isLowOnThreads();
}
public interface Scheduler extends LifeCycle {
Scheduler.Task schedule(Runnable task, long delay, TimeUnit unit);
Scheduler.Task scheduleWithFixedDelay(Runnable task, long initialDelay, long delay, TimeUnit unit);
}Unified resource management API supporting various resource types with automatic cleanup and efficient access patterns.
public interface Resource {
boolean exists();
boolean isDirectory();
boolean isFile();
Resource resolve(String subUriPath);
InputStream newInputStream() throws IOException;
Path getPath();
}
public interface ResourceFactory {
Resource newResource(String uriOrPath);
Resource newResource(Path path);
Resource newResource(URI uri);
}Comprehensive security utilities including SSL/TLS context management, credential providers, and certificate utilities.
public abstract class SslContextFactory extends AbstractLifeCycle {
public abstract SSLEngine newSSLEngine();
public abstract SSLEngine newSSLEngine(String host, int port);
public abstract void customize(SSLEngine sslEngine);
}
public interface CredentialProvider {
Credential getCredential(String credential);
}Built-in statistics collection and monitoring capabilities for performance tracking and system diagnostics.
public class CounterStatistic {
public void increment();
public void decrement();
public void add(long value);
public long getCurrent();
public long getMax();
public void reset();
}// Lifecycle states
enum LifeCycle.State {
STOPPED, STARTING, STARTED, STOPPING, FAILED
}
// Invocation types for threading
enum Invocable.InvocationType {
BLOCKING, NON_BLOCKING, EITHER
}
// Pool entry interface
interface Pool.Entry<P> {
P getPooled();
boolean release();
boolean remove();
}
// Callback exception handling
class CallbackWrapper implements Callback {
public CallbackWrapper(Callback callback);
// Wraps another callback with additional functionality
}