检查和重启 IronPdfEngine

import com.ironsoftware.ironpdf.*;

// Recover automatically if IronPdfEngine was interrupted (host restart, crash, or kill)
if (!IronPdfEngineManager.isEngineActive()) {
    IronPdfEngineManager.restartEngine();
}

// Continue as normal; the engine is now guaranteed to be connected
PdfDocument pdf = PdfDocument.renderHtmlAsPdf("<h1>Hello World</h1>");
pdf.saveAs("output.pdf");
mvn install
检查和重启 IronPdfEngine

检查和重启 IronPdfEngine

IronPdfEngine 为 IronPDF for Java 执行每个 PDF 操作,并自动管理其生命周期:它在第一次调用时启动,并在应用程序退出时停止。长时间运行的服务有时需要介入,例如当远程主机重启、操作系统 kill 或本机崩溃中断引擎时。IronPdfEngineManager 类提供静态、线程安全的方法来检查引擎的健康状况以及启动、停止或重启引擎。

代码示例使用了看门狗模式。IronPdfEngineManager.isEngineActive() 报告引擎是否已连接,而 IronPdfEngineManager.restartEngine() 在未连接时强制进行干净的重新连接,在下次渲染之前清除陈旧的连接状态。restartEngine() 是推荐的恢复操作。大多数应用程序不需要它,因为生命周期是自动的,但对于必须保持可用的服务来说,它是非常有价值的。

当您通过 IronPdfEngineConnection.withCustomGrpcConnection 提供自己的 gRPC 通道时,有一个限制:在该模式下,stopEngine() 和 restartEngine() 不可用,因为调用者拥有通道,而 isEngineActive() 和 startEngine() 在任何地方都可以使用。

在设置 IronPdfEngine 指南中了解更多。

准备开始了吗?

版本:2026.10刚刚发布