- 論壇徽章:
- 0
|
面討論建立、啟動和結(jié)束與 AS/400 的連接,并提供了一些代碼示例。要連接到 AS/400 系統(tǒng),Java 程序必須建立一個 AS400 對象。對于每一種 AS/400 服務(wù)器類型,AS400 對象最多包含一個套接字連接。在 AS/400 上,一個服務(wù)回應(yīng)一個作業(yè),它也是 AS/400 上的數(shù)據(jù)的接口。
注意:如果是在建立“企業(yè) Java Bean”,則需要遵從 EJB 規(guī)范,即連接期間不允許 AS/400 Toolbox for Java 線程。![]()
在 AS/400 上,與每個服務(wù)器的每個連接都有其各自的作業(yè)。有不同的服務(wù)器支持下列各項:
- JDBC
- 程序調(diào)用和命令調(diào)用
- 集成文件系統(tǒng)
- 網(wǎng)絡(luò)打印
- 數(shù)據(jù)隊列
- 記錄級存取
注意:如果應(yīng)用程序不嘗試同時執(zhí)行兩個都需要網(wǎng)絡(luò)打印服務(wù)器的任務(wù),則打印類將對每個 AS/400 對象使用一個套接字連接。 如果需要,一個打印類可建立與網(wǎng)絡(luò)打印服務(wù)器的多個附加套接字連接。在 5 分鐘之內(nèi)未使用的額外會話將斷開。
| Java 程序可控制與 AS/400 的連接數(shù)目。為了優(yōu)化通信性能,Java 程序可為同一個 AS/400 系統(tǒng)建立多個 AS400 對象,如圖 1 所示。這建立了與 AS/400 的多個套接字連接。
圖 1. 為同一個 AS/400 系統(tǒng)建立多個 AS400 對象和套接字連接的 Java 程序
為了節(jié)省 AS/400 資源,僅建立一個 AS400 對象,如圖 2 所示。此方法減少了連接的數(shù)目,從而減少了在 AS/400 系統(tǒng)上使用的資源量。
圖 2. 為同一個 AS/400 系統(tǒng)建立單個 AS400 對象和套接字連接的 Java 程序
下列示例顯示如何建立和使用 AS400 類:
示例 1:在下列示例中,將建立兩個 CommandCall 對象,它們向同一個 AS/400 系統(tǒng)發(fā)送命令。由于 CommandCall 對象使用同一個 AS400 對象,所以只建立了一個與 AS/400 系統(tǒng)的連接。
// Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two command call objects that use
// the same AS400 object.
CommandCall cmd1 = new CommandCall(sys,"myCommand1");
CommandCall cmd2 = new CommandCall(sys,"myCommand2");
// Run the commands. A connection is made when the
// first command is run. Since they use the same
// AS400 object the second command object will use
// the connection established by the first command.
cmd1.run();
cmd2.run();
| 示例 2:在下列示例中,將建立兩個 CommandCall 對象,它們向同一個 AS/400 系統(tǒng)發(fā)送命令。由于 CommandCall 對象使用不同的 AS400 對象,所以建立兩個與 AS/400 系統(tǒng)的連接。
// Create two AS400 objects to the same AS/400 system.
AS400 sys1 = new AS400("mySystem.myCompany.com");
AS400 sys2 = new AS400("mySystem.myCompany.com");
// Create two command call objects. They use
// different AS400 objects.
CommandCall cmd1 = new CommandCall(sys1,"myCommand1");
CommandCall cmd2 = new CommandCall(sys2,"myCommand2");
// Run the commands. A connection is made when the
// first command is run. Since the second command
// object uses a different AS400 object, a second
// connection is made when the second command is run.
cmd1.run();
cmd2.run();
| 示例 3:在下列示例中,通過使用同一個 AS400 對象,建立一個 CommandCall 對象和一個 IFSFileInputStream 對象。由于 CommandCall 對象和 IFSFileInput Stream 對象使用 AS/400 系統(tǒng)上的不同服務(wù),所以建立了兩個連接。
// Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create a command call object.
CommandCall cmd = new CommandCall(sys,"myCommand1");
// Create the file object. Creating it causes the
// AS400 object to connect to the file service.
IFSFileInputStream file = new IFSFileInputStream(sys,"/myfile");
// Run the command. A connection is made to the
// command service when the command is run.
cmd.run();
| 啟動和結(jié)束連接Java 程序可控制連接的啟動時間和結(jié)束時間。缺省情況下,如果需要 AS/400 上的信息,就會啟動一個連接。通過對 AS400 對象調(diào)用 connectService() 方法,從而預(yù)先與 AS/400 連接,就可以精確地控制何時建立連接。
下列示例顯示連接到 AS/400 及從 AS/400 斷開連接的 Java 程序。
示例 1:此示例顯示如何預(yù)先與 AS/400 連接:
// Create an AS400 object.
AS400 system1 = new AS400("mySystem.myCompany.com");
// Connect to the command service. Do it now
// instead of when data is first sent to the
// command service. This is optional since the
// AS400 object will connect when necessary.
system1.connectService(AS400.COMMAND);
| 示例 2:一個連接一旦啟動,便由 Java 程序負(fù)責(zé)斷開其連接,此操作由 AS400 對象隱式完成或由 Java 程序顯式完成。通過對 AS400 對象調(diào)用 disconnectService() 方法斷開與 Java 程序的連接。為了改進性能,Java 程序應(yīng)僅在程序完成一個服務(wù)后才斷開。若 Java 程序還未完成服務(wù)就斷開,當(dāng)需要來自服務(wù)的數(shù)據(jù)時,AS400 對象將重新連接(如果有可能重新連接的話)。
圖 3 顯示斷開第一個集成文件系統(tǒng)對象連接的連接將如何僅結(jié)束 AS400 對象連接的單個實例,而不是結(jié)束所有集成文件系統(tǒng)對象連接。
圖 3. 對 AS400 對象實例使用自已的服務(wù)的單個對象被斷開
該示例顯示 Java 程序如何斷開一個連接:
// Create an AS400 object.
AS400 system1 = new AS400("mySystem.myCompany.com");
// ... use command call to send several commands
// to the AS/400. Since connectService() was not
// called, the AS400 object automatically
// connects when the first command is run.
// All done sending commands so disconnect the
// connection.
system1.disconnectService(AS400.COMMAND);
| 示例 3:使用同一服務(wù)并共享同一 AS400 對象的多個對象共享一個連接。斷開將結(jié)束對 AS400 對象的每個實例使用同一服務(wù)的所有對象的鏈接,如圖 4 所示。
圖 4. 對 AS400 對象使用同一服務(wù)的所有對象都被斷開
例如,兩個 CommandCall 對象使用同一個 AS400 對象。當(dāng)調(diào)用 disconnectService() 時,兩個 CommandCall 對象的連接都結(jié)束。當(dāng)對第二個 CommandCall 對象調(diào)用 run() 方法時,AS400 對象必須重新與該服務(wù)連接:
// Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two command call objects.
CommandCall cmd1 = new CommandCall(sys,"myCommand1");
CommandCall cmd2 = new CommandCall(sys,"myCommand2");
// Run the first command
cmd1.run();
// Disconnect from the command service.
sys.disconnectService(AS400.COMMAND);
// Run the second command. The AS400 object
// must reconnect to the AS/400.
cmd2.run();
// Disconnect from the command service. This
// is the correct place to disconnect.
sys.disconnectService(AS400.COMMAND);
| 示例 4:并非所有 AS/400 Toolbox for Java 類都會自動重新連接。因為文件可能已更改,所以集成文件系統(tǒng)類中的某些方法調(diào)用不會重新連接。文件斷開時,其他某個進程可能已刪除該文件或更改了其中的內(nèi)容。在下列示例中,兩個文件對象使用同一個 AS400 對象。當(dāng)調(diào)用 disconnectService() 時,兩個文件對象的連接都結(jié)束。因為第二個 IFSFileInputStream 對象不再與 AS/400 連接,所以對它的 read() 失敗。
// Create an AS400 object.
AS400 sys = new AS400("mySystem.myCompany.com");
// Create two file objects. A connection to the
// AS/400 is created when the first object is
// created. The second object uses the connection
// created by the first object.
IFSFileInputStream file1 = new IFSFileInputStream(sys,"/file1");
IFSFileInputStream file2 = new IFSFileInputStream(sys,"/file2");
// Read from the first file, then close it.
int i1 = file1.read();
file1.close();
// Disconnect from the file service.
sys.disconnectService(AS400.FILE);
// Attempt to read from the second file. This
// fails because the connection to the file service
// no longer exists. The program must either
// disconnect later or have the second file use a
// different AS400 object (which causes it to
// have its own connection).
int i2 = file2.read();
// Close the second file.
file2.close();
// Disconnect from the file service. This
// is the correct place to disconnect.
sys.disconnectService(AS400.FILE);
|
[ 本帖最后由 wyyhzc 于 2009-9-6 17:44 編輯 ] |
|