目录
方式一、在JavaEE容器配置
方式二、 在应用程序中配置
在没有提供JCA容器的JavaEE实现中,比如Tomcat,可以在其context.xml中加入如下内容:
<Resource name="eis/cicseci" auth="Container" type="javax.resource.cci.ConnectionFactory" factory="com.console.util.cics.JndiConnectionFactory" serverName="TestSrv" connectionURL="127.0.0.1" portNumber ="2006" socketConnectTimeout ="30" tranName ="CPMI" userName ="CICSUSER" password ="CICSUSER" />com.console.util.cics.JndiEciConnectionFactory要实现javax.naming.spi.ObjectFactory接口,代码如下:
package com.console.util.cics; import java.util.Enumeration; import java.util.Hashtable; import javax.naming.Context; import javax.naming.Name; import javax.naming.RefAddr; import javax.naming.Reference; import javax.naming.spi.ObjectFactory; import com.ibm.connector2.cics.ECIManagedConnectionFactory; /** * 生成调用eci call用到的ConnectionFactory对象,此对象将提供给外部容器做JNDI绑定 * */ public class JndiEciConnectionFactory implements ObjectFactory { public static final String PARAM_USER_NAME = "userName"; public static final String PARAM_PASSWORD = "password"; public static final String PARAM_CONNECTION_URL = "connectionURL"; public static final String PARAM_PORT_NUMBER = "portNumber"; public static final String PARAM_SERVER_NAME = "serverName"; public static final String PARAM_TRAN_NAME = "tranName"; public static final String PARAM_SOCKET_CONNECT_TIMEOUT = "socketConnectTimeout"; public static final String PARAM_APPLID = "applid"; public static final String PARAM_TRACE_LEVEL = "traceLevel"; public static final String PARAM_TPN_NAME = "tpnName"; public static final String PARAM_APPLID_QUALIFIER = "applidQualifier"; public static final String PARAM_KEY_RING_CLASS = "keyRingClass"; public static final String PARAM_KEY_RING_PASSWORD = "keyRingPassword"; public static final String PARAM_CLIENT_SECURITY = "clientSecurity"; public static final String PARAM_SERVER_SECURITY = "serverSecurity"; public static final String PARAM_REQUEST_EXITS = "requestExits"; public static final String PARAM_CIPHER_SUITES = "cipherSuites"; /** * 将参数设置到ECIManagedConnectionFactory中,然后生成实现了javax.resource.cci. * ConnectionFactory接口的对象 */ public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable<?, ?> environment) throws Exception { ECIManagedConnectionFactory bean = new ECIManagedConnectionFactory(); Reference ref = (Reference) obj; Enumeration<RefAddr> addrs = ref.getAll(); while (addrs.hasMoreElements()) { RefAddr addr = addrs.nextElement(); String type = addr.getType(); String value = (String) addr.getContent(); if (type.equals(PARAM_USER_NAME)) { bean.setUserName(value); } else if (type.equals(PARAM_PASSWORD)) { bean.setPassword(value); } else if (type.equals(PARAM_CONNECTION_URL)) { bean.setConnectionURL(value); } else if (type.equals(PARAM_PORT_NUMBER)) { bean.setPortNumber(value); } else if (type.equals(PARAM_SERVER_NAME)) { bean.setServerName(value); } else if (type.equals(PARAM_TRAN_NAME)) { bean.setTranName(value); } else if (type.equals(PARAM_SOCKET_CONNECT_TIMEOUT)) { bean.setSocketConnectTimeout(value); } else if (type.equals(PARAM_APPLID)) { bean.setApplid(value); } else if (type.equals(PARAM_TRACE_LEVEL)) { bean.setTraceLevel(Integer.parseInt(value)); } else if (type.equals(PARAM_TPN_NAME)) { bean.setTPNName(value); } else if (type.equals(PARAM_APPLID_QUALIFIER)) { bean.setApplidQualifier(value); } else if (type.equals(PARAM_KEY_RING_CLASS)) { bean.setKeyRingClass(value); } else if (type.equals(PARAM_KEY_RING_PASSWORD)) { bean.setKeyRingPassword(value); } else if (type.equals(PARAM_CLIENT_SECURITY)) { bean.setClientSecurity(value); } else if (type.equals(PARAM_SERVER_SECURITY)) { bean.setServerSecurity(value); } else if (type.equals(PARAM_REQUEST_EXITS)) { bean.setRequestExits(value); } else if (type.equals(PARAM_CIPHER_SUITES)) { bean.setCipherSuites(value); } } return bean.createConnectionFactory(); }如果是在其它提供了JCA容器的JavaEE实现中,可以直接配置对ConnectionFactory的JNDI访问,而不需要使用上述代码。
spring中访问JNDI的方式:
<bean id="atii.eciConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean"> <property name="jndiName" value="eis/cicseci" /> <property name="resourceRef" value="true" /> </bean>此方式的好处是,与CICS相关的配置在JavaEE容器中完成,程序代码只需要使用JNDI找出注册在JavaEE容器中的ConnectionFactory,程序代码在开发、测试、生产等环境中部署时,不需要做任何修改。程序代码不需要关心实现CICS相关配置在什么地方完成的。
以spring为例进行说明:
<!-- cics --> <bean id="atii.eciManagedConnectionFactory" class="com.ibm.connector2.cics.ECIManagedConnectionFactory"> <property name="serverName" value="TestSrv" /> <property name="connectionURL" value="127.0.0.1" /> <property name="portNumber" value="2006" /> <property name="socketConnectTimeout" value="30" /> <property name="tranName" value="CPMI" /> <property name="userName" value="CICSUSER" /> <property name="password" value="CICSUSER" /> </bean> <bean id="atii.eciConnectionFactory" class="org.springframework.jca.support.LocalConnectionFactoryBean"> <property name="managedConnectionFactory" ref="atii.eciManagedConnectionFactory" /> </bean>此方式只适合在开发环境中使用,如果是要部署到其它环境,需要修改spring配置文件,不利于快速部署。