配置和部署基本 JCA 资源适配器
您可配置和部署基本 Java™ EE 连接器体系结构 (JCA) 连接工厂和资源适配器。
有关此任务
ExampleRA.rar 的示例资源适配器,它提供了三种类型的资源:一个连接工厂和两种类型的受管对象。过程
- 在 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 功能部件。