配置及部署基本 JCA 資源配接器
您可以配置及部署基本 Java™ EE 連接器架構 (JCA) Connection Factory 和資源配接器。
關於這項作業
ExampleRA.rar 的資源配接器範例,其提供 3 種類型的資源:一個 Connection Factory,以及兩種類型的受管理物件。程序
- 在 server.xml 檔中啟用 JCA 特性。server.xml 檔案位於 [path_to_liberty\wlp\usr\servers\server_name]
<server> <featureManager> <feature>jca-1.6</feature> <feature>servlet-3.0</feature> </featureManager> </server>已穩定特性:jca-1.6特性已穩定。 您可以繼續使用jca-1.6特性。 不過,請考量使用較新的 JCA 特性。 - 將資源配接器 RAR 檔 (ExampleRA.rar) 放在伺服器的 dropins 資料夾中。如果您的伺服器正在執行,您會在主控台日誌中看到下列訊息,指出已安裝資源配接器:
[AUDIT ] J2CA7001I: Resource adapter ExampleRA installed in 1.306 seconds.
- 檢查資源配接器的部署描述子、註釋和其他說明文件,以識別配接器提供哪些類型的資源,以及各配接器所接受的配置內容為何。資源配接器範例 ExampleRA.rar 的
ra.xml部署描述子中有提供此資訊。 ra.xml 檔位於 [path_to_ExampleRA\ExampleRA\META-INF.] 。部署描述子會識別您可以配置的 3 種資源類型。<connection-definition> <managedconnectionfactory-class>com.ibm.example.jca.adapter.ManagedConnectionFactoryImpl</managedconnectionfactory-class> <config-property> <config-property-name>tableName</config-property-name> <config-property-type>java.lang.String</config-property-type> </config-property> <connectionfactory-interface>javax.resource.cci.ConnectionFactory</connectionfactory-interface> ... </connection-definition> <adminobject> <adminobject-interface>javax.resource.cci.ConnectionSpec</adminobject-interface> <adminobject-class>com.ibm.example.jca.adapter.ConnectionSpecImpl</adminobject-class> <config-property> <config-property-name>readOnly</config-property-name> <config-property-type>java.lang.Boolean</config-property-type> <config-property-value>false</config-property-value> </config-property> </adminobject> <adminobject> <adminobject-interface>javax.resource.cci.InteractionSpec</adminobject-interface> <adminobject-class>com.ibm.example.jca.adapter.InteractionSpecImpl</adminobject-class> <config-property> <description>Function name. Supported values are: ADD, FIND, REMOVE</description> <config-property-name>functionName</config-property-name> <config-property-type>java.lang.String</config-property-type> </config-property> </adminobject> - 在 server.xml 檔案中,配置可用資源類型的實例。
<server> <featureManager> <feature>jca-1.6</feature> <feature>servlet-3.0</feature> </featureManager> <connectionFactory jndiName="eis/conFactory"> <properties.ExampleRA tableName="TABLE1"/> </connectionFactory> <adminObject jndiName="eis/conSpec"> <properties.ExampleRA.ConnectionSpec/> </adminObject> <adminObject jndiName="eis/iSpec_ADD"> <properties.ExampleRA.InteractionSpec functionName="ADD"/> </adminObject> <adminObject jndiName="eis/iSpec_FIND"> <properties.ExampleRA.InteractionSpec functionName="FIND"/> </adminObject> </server> - 使用資源注入來存取 Servlet 中的資源,例如:
@Resource(lookup = "eis/conFactory") private ConnectionFactory conFactory; @Resource(lookup = "eis/conSpec") private ConnectionSpec conSpec; @Resource(lookup = "eis/iSpec_ADD") private InteractionSpec iSpec_ADD; @Resource(lookup = "eis/iSpec_FIND") private InteractionSpec iSpec_FIND; ... MappedRecord input = conFactory.getRecordFactory().createMappedRecord("input"); input.put("city", "Rochester"); input.put("state", "Minnesota"); input.put("population", 106769); Connection con = conFactory.getConnection(conSpec); try { Interaction interaction = con.createInteraction(); interaction.execute(iSpec_ADD, input); interaction.close(); } finally { con.close(); }附註: 如果您想要從名稱空間查閱資源,而不是使用注入,則必須在 server.xml 檔中啟用 JNDI 特性。