Analysis of Spring Integration integration with other EIS systems (spring integration finale)

Note: The link to the complete content of the entire pring Integration integration is as follows

https://mp.weixin.qq.com/mp/appmsgalbum?action=getalbum&album_id=1467395879861698562&__biz=Mzg5OTA1OTExMQ==#wechat_redirect


background

J2EE provides JCA (Java Connector Architecture) specifications to standardize access to EIS. This specification is divided into several different parts:

  • SPI (Service provider interfaces) is the interface that the connector provider must implement. These interfaces form a resource adapter that can be deployed on a J2EE application server. In this case, the server manages connection pooling, transactions, and security (managed mode). The application server is also responsible for managing the configuration owned by the client. A connector can also be used without the application server. In this case, the application must directly configure it (non-managed mode).

  • CCI (Common Client Interface) is an interface used by applications to interact with connectors and communicate with EIS. It also provides APIs for local affairs demarcation.

The purpose of Spring's support for CCI is to provide classes for accessing CCI connectors in a typical Spring way, and to effectively use Spring's general resource and transaction management mechanisms.

note

The client side of the connector does not have to always use CCI. Certain connectors expose their own APIs to provide JCA resource adapters to use certain system contracts (connection pooling, global transactions, security) provided by the J2EE container. )). Spring does not provide special support for such connector-specific APIs.

https://documentation.help/j2EE-Spring-Framework/ch21.html



Spring support for JCA CCI

Spring's jca cci support is in the spring-tx module:

image.png

Configure CCI

  • Connector configuration

The basic resource for using JCA CCI is the ConnectionFactory interface. The connector used must provide an implementation of this interface.

In order to use the connector, you can deploy it to your application server and retrieve the ConnectionFactory from the server's JNDI environment (hosted mode). The linker must be packaged as a RAR file (resource adapte archive) and contains a deployment descriptor file ra.xml. The actual name of the resource needs to be specified during deployment. If you want to access it through Spring, just simply use Spring's JndiObjectFactoryBean to get the factory by JNDI name.

Another way to use the connector is to embed it in the application (non-managed mode) instead of deploying and configuring it in the application server. Spring provides the possibility to configure the connector as a bean through the provided FactoryBean (LocalConnectionFactoryBean). In this way, only the linker library needs to be placed in the classpath directory (the RAR file and ra.xml descriptor are not required). If necessary, you must extract the library from the RAR file of the linker.

Once the ConnectionFactory instance is accessed, it can be injected into the component. These components can be coded with simple CCI APIs, or they can use Spring classes that support CCI access (for example, CciTemplate).

note

When the connector is used in non-managed mode, the global transaction cannot be used because the resource is never added or deleted to the current global transaction of the current thread. The resource has no knowledge of any global J2EE transactions that may be running.

  • Configure ConnectionFactory in Spring

In order to create a connection to the EIS, if you are in managed mode, you need to obtain a ConnectionFactory from the application server, or directly from Spring when you are in non-managed mode.

In managed mode, you can access the ConnectionFactory from JNDI, and its properties will be configured in the application server.

<bean id="eciConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">

<property name="jndiName" value="eis/cicseci"/>

</bean>

In non-managed mode (non-managed mode), you must configure the ConnectionFactory you want to use as a JavaBean in the Spring configuration. The LocalConnectionFactoryBean class provides this configuration style, passing the ManagedConnectionFactory into your connector implementation, exposing the application-level CCI ConnectionFactory.

<bean id="eciManagedConnectionFactory" class="com.ibm.connector2.cics.ECIManagedConnectionFactory">

<property name="serverName" value="TXSERIES"/>

<property name="connectionURL" value="tcp://localhost/"/>

<property name="portNumber" value="2006"/>

</bean>

<bean id="eciConnectionFactory" class="org.springframework.jca.support.LocalConnectionFactoryBean">

<property name="managedConnectionFactory"ref="eciManagedConnectionFactory"/>

</bean>

note

You cannot directly instantiate a specified ConnectionFactory interface. You need to implement the corresponding ManagedConnectionFactory interface for the connector, which is part of the JCA SPI specification.

  • Configure CCI connection

JCA CCI允许开发者使用自己的连接器的 ConnectionSpec 接口实现来配置到 EIS 的连接。为了配置该连接的属性,需要用一个指定的 ConnectionSpecConnectionFactoryAdapter 适配器来封装目标连接工厂。因此,特定的 ConnectionSpec 接口可以用 connectionSpec 属性来配置(作为一个内部bean)。

这个属性不是必需的,因为CCI ConnectionFactory 接口定义了两个不同的方法来获取 CCI 连接。ConnectionSpec 的一些属性常常可以被配置在应用服务器中(托管模式(managed mode)) 或相关的本地 ManagedConnectionFactory 实现。

public interface ConnectionFactory implements Serializable, Referenceable {

...

Connection getConnection() throws ResourceException;

Connection getConnection(ConnectionSpec connectionSpec) throws ResourceException;

...

}

Spring提供了 ConnectionSpecConnectionFactoryAdapter 适配器, 允许你指定一个 ConnectionSpec 接口的实例, 供给定工厂的所有操作使用。如果指定了适配器的 connectionSpec 属性,适配器就使用没有参数的 getConnection 变量, 而不是有 ConnectionSpec 参数的变量。

<bean id="managedConnectionFactory"

class="com.sun.connector.cciblackbox.CciLocalTxManagedConnectionFactory">

<property name="connectionURL" value="jdbc:hsqldb:hsql://localhost:9001"/>

<property name="driverName" value="org.hsqldb.jdbcDriver"/>

</bean>

<bean id="targetConnectionFactory"

class="org.springframework.jca.support.LocalConnectionFactoryBean">

<property name="managedConnectionFactory" ref="managedConnectionFactory"/>

</bean>

<bean id="connectionFactory"

class="org.springframework.jca.cci.connection.ConnectionSpecConnectionFactoryAdapter">

<property name="targetConnectionFactory" ref="targetConnectionFactory"/>

<property name="connectionSpec">

<bean class="com.sun.connector.cciblackbox.CciConnectionSpec">

<property name="user" value="sa"/>

<property name="password" value=""/>

</bean>

</property>

</bean>

  • 使用一个 CCI 单连接

如果想使用一个 CCI 单连接,Spring提供一个额外的 ConnectionFactory 适配器来管理它。SingleConnectionFactory 适配器类将延迟打开一个单独的连接并在应用程序销毁这个bean的时候关闭它。这个类将暴露出特殊 Connection 的相应代理,并共享同一个底层的物理连接。

<bean id="eciManagedConnectionFactory"

class="com.ibm.connector2.cics.ECIManagedConnectionFactory">

<property name="serverName" value="TEST"/>

<property name="connectionURL" value="tcp://localhost/"/>

<property name="portNumber" value="2006"/>

</bean>

<bean id="targetEciConnectionFactory"

class="org.springframework.jca.support.LocalConnectionFactoryBean">

<property name="managedConnectionFactory" ref="eciManagedConnectionFactory"/>

</bean>

<bean id="eciConnectionFactory"

class="org.springframework.jca.cci.connection.SingleConnectionFactory">

<property name="targetConnectionFactory" ref="targetEciConnectionFactory"/>

</bean>

注意

这个 ConnectionFactory 适配器不能直接用 ConnectionSpec 配置。如果需要特定 ConnectionSpec 的单一连接,那么可以用 SingleConnectionFactory 与之通信的中间 ConnectionSpecConnectionFactoryAdapter 适配器。


CciTemplate类

CciTemplate 类是 CCI 核心支持包(org.springframework.jca.cci.core)中主要的类。它简化了CCI的使用,因为它会处理资源的创建和释放。这有助于避免常见的错误,比如总是忘记关闭连接。它关注连接和交互对象的生命周期,从而使应用程序的代码可以专注于处理从应用程序数据中生成输入记录和从输出记录中提取应用程序数据。

JCA CCI规范定义了两个不同的方法来在EIS上调用操作。CCI Interaction 接口提供两个 execute 方法的签名:

public interface javax.resource.cci.Interaction {

...

boolean execute(InteractionSpec spec, Record input, Record output) throws ResourceException;

Record execute(InteractionSpec spec, Record input) throws ResourceException;

...

}

依赖于模板方法的调用,CciTemplate 类可以知道 interaction上的哪个 execute 方法被调用。在任何情况下,都必须有一个正确初始化过的 InteractionSpec 实例。

CciTemplate.execute(..)可以以下列两种方式使用:

  • 在提供直接的 Record 参数的情况下,只需要传递输入记录给 CCI , 而返回的对象就是对应的 CCI 输出记录。

  • 在提供使用记录映射的应用对象的情况下,你需要提供相应的 RecordCreator 和 RecordExtractor 实例。

第一种方法将使用下面的模板方法。这些模板方法将直接对应到 Interaction 接口。

public class CciTemplate implements CciOperations {

public Record execute(InteractionSpec spec, Record inputRecord)

throws DataAccessException { ... }

public void execute(InteractionSpec spec, Record inputRecord, Record outputRecord)

throws DataAccessException { ... }

}

第二种方法需要我们以参数的方式指定创建记录和记录提取的策略。使用前面记录转化一节中描述的接口。对应的 CciTemplate 方法如下:

public class CciTemplate implements CciOperations {

public Record execute(InteractionSpec spec, RecordCreator inputCreator)

throws DataAccessException { ... }

public Object execute(InteractionSpec spec, Record inputRecord, RecordExtractor outputExtractor)

throws DataAccessException { ... }

public Object execute(InteractionSpec spec, RecordCreator creator, RecordExtractor extractor)

throws DataAccessException { ... }

}

除非在模板上设置 outputRecordCreator 属性(参见下一部分), 不然每个方法都将调用CCI Interaction 中相应的含有两个参数:InteractionSpec 和输入 Record的 execute 方法, 并接收一个输出 Record 作为返回值。

通过 createIndexRecord(..) 和 createMappedRecord(..) 方法, CciTemplate 在 RecordCreator 实现类外部也提供了创建 IndexRecord 和 MappedRecord。还可以用来在DAO实现内创建记录实例并传入到相应的 CciTemplate.execute(..) 方法。

public class CciTemplate implements CciOperations {

public IndexedRecord createIndexedRecord(String name) throws DataAccessException { ... }

public MappedRecord createMappedRecord(String name) throws DataAccessException { ... }

}

  • DAO支持

Spring的 CCI 支持为 DAO 提供了一个抽象类,支持 ConnectionFactory 或 CciTemplate 实例的注入。这个类的名字是 CciDaoSupport:它提供了简单的 setConnectionFactory 和 setCciTemplate 方法。在内部,该类将为传入的 ConnectionFactory 创建一个 CciTemplate 实例, 并把它暴露给子类中具体的数据访问实现使用。

public abstract class CciDaoSupport {

public void setConnectionFactory(ConnectionFactory connectionFactory) { ... }

public ConnectionFactory getConnectionFactory() { ... }

public void setCciTemplate(CciTemplate cciTemplate) { ... }

public CciTemplate getCciTemplate() { ... }

}

  • 自动输出记录生成

如果所用的连接器只支持以输入输出记录作为参数的 Interaction.execute(..) 方法 (就是说,它要求传入期望的输出记录而不是返回适当的输出记录), 你可以设定 CciTemplate 类的 outputRecordCreator 属性来自动生成一个输出记录, 当接收到响应时JCA连接器(JCA connector)将填充该记录并返回给模板的调用者。

因为这个目的,这个属性只持有 RecordCreator 接口的一个实现。RecordCreator 接口已经在 第 21.3.1 节 “记录转换” 进行了讨论。outputRecordCreator 属性必须直接在 CciTemplate 中指定,可以在应用代码中做到这一点。

cciTemplate.setOutputRecordCreator(new EciOutputRecordCreator());

或者如果 CciTemplate 被配置为一个专门的bean实例,那么outputRecordCreator还可以在Spring文件中配置(推荐的做法):

<bean id="eciOutputRecordCreator" class="eci.EciOutputRecordCreator"/>

<bean id="cciTemplate" class="org.springframework.jca.cci.core.CciTemplate">

<property name="connectionFactory" ref="eciConnectionFactory"/>

<property name="outputRecordCreator" ref="eciOutputRecordCreator"/>

</bean>

注意

因为 CciTemplate 类是线程安全的,所以它通常被配置为一个共享实例。


拓展tomcat Connector

Connector用于接受请求并将请求封装成Request和Response,然后交给Container进行处理,Container处理完之后再交给Connector返回给客户端。

image.png

Tomcat源码中与connector相关的类位于org.apache.coyote包中,Connector分为以下几类:

  • Http Connector, 基于HTTP协议,负责建立HTTP连接。它又分为BIO Http Connector与NIO Http Connector两种,后者提供非阻塞IO与长连接Comet支持。默认情况下,Tomcat使用的就是这个Connector。

  • AJP Connector, 基于AJP协议,AJP是专门设计用来为tomcat与http服务器之间通信专门定制的协议,能提供较高的通信速度和效率。如与Apache服务器集成时,采用这个协议。

  • APR HTTP Connector, 用C实现,通过JNI调用的。主要提升对静态资源(如HTML、图片、CSS、JS等)的访问性能。现在这个库已独立出来可用在任何项目中。Tomcat在配置APR之后性能非常强劲。

tomcat中实现了以下几种Connector:

#以下三种Connector实现都是直接处理来自客户端Http请求

  • org.apache.coyote.http11.Http11Protocol : 支持HTTP/1.1 协议的连接器。

  • org.apache.coyote.http11.Http11NioProtocol : 支持HTTP/1.1 协议+New IO的连接器。

  • org.apache.coyote.http11.Http11AprProtocol : 使用APR(Apache portable runtime)技术的连接器,利用Nativ


#以下三种实现方法则是与web server打交道

  • org.apache.coyote.ajp.AjpProtocol:使用AJP协议的连接器,实现与web server(如Apache httpd)之间的通信

  • org.apache.coyote.ajp.AjpNioProtocol:SJP协议+ New IO

  • org.apache.coyote.ajp.AjpAprProtocol:AJP + APR

总结

Spring provides support for jca cci, which can be used after the ConnectionFactory is configured through the template class CciTemplate. In addition, the Connector mode is widely used, such as tomcat.

The protocol of jca is JSR 322. If you want to learn more, you can certify and learn this spec.



Guess you like

Origin blog.51cto.com/15015181/2556224