类 HttpTransportFactory
This factory provides:
- A lazily-initialized default HttpTransport singleton
- Registration of custom HttpTransport instances for lifecycle management
- Automatic cleanup via JVM shutdown hook
Usage examples:
// Get the default transport (lazily created)
HttpTransport transport = HttpTransportFactory.getDefault();
// Set a custom default transport
HttpTransport custom = OkHttpTransport.builder()
.config(HttpTransportConfig.builder()
.connectTimeout(Duration.ofSeconds(30))
.build())
.build();
HttpTransportFactory.setDefault(custom);
// Register a transport for automatic cleanup
HttpTransportFactory.register(myTransport);
All registered transports and the default transport will be closed
during JVM shutdown, orchestrated by AgentScopeJvmShutdownHook
after all agents have finished processing.
-
方法概要
修饰符和类型方法说明static HttpTransportGet the default HttpTransport instance.static intGet the number of managed transports.static booleanisManaged(HttpTransport transport) Check if a transport is currently managed.static voidregister(HttpTransport transport) Register an HttpTransport for lifecycle management.static voidsetDefault(HttpTransport transport) Set the default HttpTransport instance.static voidshutdown()Shutdown and close all managed transports.static booleanunregister(HttpTransport transport) Unregister an HttpTransport from lifecycle management.
-
方法详细资料
-
getDefault
Get the default HttpTransport instance.If no default has been set, a new JdkHttpTransport with default configuration will be created lazily. The default transport is automatically registered for cleanup on JVM shutdown.
- 返回:
- the default HttpTransport instance
-
setDefault
Set the default HttpTransport instance.The provided transport will be registered for automatic cleanup if not already registered. The previous default transport (if any) remains registered for cleanup.
- 参数:
transport- the transport to set as default (may be null to clear)
-
register
Register an HttpTransport for lifecycle management.Registered transports will be automatically closed when the JVM shuts down. This is useful for transports that are not set as the default but still need cleanup.
- 参数:
transport- the transport to register
-
unregister
Unregister an HttpTransport from lifecycle management.After unregistration, the transport will not be closed during shutdown. This does not close the transport immediately.
- 参数:
transport- the transport to unregister- 返回:
- true if the transport was unregistered, false if it was not registered
-
shutdown
public static void shutdown()Shutdown and close all managed transports.This method is called automatically by the JVM shutdown hook, but can also be called manually for testing or explicit cleanup. After calling this method, the default transport will be null and all managed transports will be cleared.
Errors during close are logged but do not prevent other transports from being closed.
-
getManagedTransportCount
public static int getManagedTransportCount()Get the number of managed transports.This method is primarily for testing purposes.
- 返回:
- the number of managed transports
-
isManaged
Check if a transport is currently managed.This method is primarily for testing purposes.
- 参数:
transport- the transport to check- 返回:
- true if the transport is managed
-