Show Table of Contents
第 2 章 使用软件开发套件
本章包括了一组展示如何使用 Java 软件开发套件的示例。除非特别声明,本章中的示例都使用软件开发套件的 V3 版本。
2.1. 使用 V3 连接到 Red Hat Enterprise Virtualization Manager
在 Java 软件开发套件的 V3 版本中,
Api 类是连接并处理 Red Hat Enterprise Virtualization 环境中的项的主要类。为了声明这个类的一个实例,您需要声明 ApiBuilder 类的一个实例,并使用 builder 方法把需要的参数传递到这个实例。build 方法会返回 Api 类的一个实例,然后您就可以为它分配变量,并执行相关的操作。
以下示例是一个简单的 Java SE 程序,它创建一个到 Red Hat Enterprise Virtualization 环境的连接,然后安全地关闭并断开连接。
例 2.1. 连接到 Red Hat Enterprise Virtualization Manager
package rhevm;
import org.ovirt.engine.sdk.Api;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.ovirt.engine.sdk.ApiBuilder;
import org.ovirt.engine.sdk.exceptions.ServerException;
import org.ovirt.engine.sdk.exceptions.UnsecuredConnectionAttemptError;
public class rhevm {
public static void main(String[] args) {
Api api = null;
try {
ApiBuilder myBuilder = new ApiBuilder()
.url("https://rhevm.example.com/api")
.user("admin@internal")
.password("p@ssw0rd")
.keyStorePath("/home/username/server.truststore")
.keyStorePassword("p@ssw0rd");
api = myBuilder.build();
api.shutdown();
} catch (ServerException | UnsecuredConnectionAttemptError | IOException ex) {
Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex);
} finally {
if (api != null) {
try {
api.close();
} catch (Exception ex) {
Logger.getLogger(Ovirt.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
}
}
这个示例创建了一个使用基础验证的连接,但您也可以使用其它方法。如需了解可以传递给
ApiBuilder 类实例的参数列表,请参阅 附录 A, ApiBuilder 的方法。
注意
请注意,
Api 类不采用 Autocloseable 接口。因此建议在 finally 块中关闭 Api 类实例,以保证可正常关闭与 Red Hat Enterprise Virtualization Manager 的连接。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.