类 HttpTransportFactory

java.lang.Object
io.agentscope.core.model.transport.HttpTransportFactory

public final class HttpTransportFactory extends Object
Factory for managing HttpTransport instances.

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.

  • 方法详细资料

    • getDefault

      public static HttpTransport 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

      public static void setDefault(HttpTransport transport)
      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

      public static void register(HttpTransport transport)
      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

      public static boolean unregister(HttpTransport transport)
      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

      public static boolean isManaged(HttpTransport transport)
      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