使用本地 ECI 资源适配器链接到 CICS 中的程序
使用 JCA 本地 ECI 资源适配器在 CICS® 中运行程序是通过使用 ECIInteraction 类的 execute () 方法完成的。
关于此任务
此任务显示应用程序开发者如何使用 JCA 本地 ECI 资源适配器来运行使用 JCA 记录传入 COMMAREA 的 CICS 程序。 有关如何扩展记录接口以表示 CICS COMMAREA 的更多详细信息,请参阅 将 JCA 本地 ECI 资源适配器与 COMMAREA 配合使用 以及有关如何链接到使用通道和容器的 CICS 程序的详细信息,请参阅 将 JCA 本地 ECI 资源适配器与通道和容器配合使用。
过程
- 使用 JNDI 来查找名为 eis/defaultCICSConnectionFactory的 ConnectionFactory 对象。
- 从 ConnectionFactory获取 Connection 对象。
- 从连接获取交互对象。
- 创建新的 ECIInteractionSpec 对象。
- 使用 ECIInteractionSpec 上的 set 方法来设置执行的属性,例如程序名和 COMMAREA 长度。
- 创建记录对象以包含输入数据 (请参阅 COMMAREA/Channel 主题) 并填充数据。
- 创建记录对象以包含输出数据。
- 对交互调用执行方法,传递 ECIInteractionSpec 和两个记录对象。
- 从输出记录中读取数据。
package com.ibm.cics.server.examples.wlp; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import javax.annotation.Resource; import javax.resource.cci.Connection; import javax.resource.cci.ConnectionFactory; import javax.resource.cci.Interaction; import javax.resource.cci.Record; import javax.resource.cci.Streamable; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.ibm.connector2.cics.ECIInteractionSpec; /** * Servlet implementation class JCAServlet */ @WebServlet("/JCAServlet") public class JCAServlet extends HttpServlet { private static final long serialVersionUID = 4283052088313275418L; // 1. Use JNDI to look up the connection factory @Resource(lookup = "eis/defaultCICSConnectionFactory") private ConnectionFactory cf; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { try { // 2. Get the connection object from the connection factory Connection conn = cf.getConnection(); // 3. Get an interaction object from the connection Interaction interaction = conn.createInteraction(); // 4. Create a new ECIInteractionSpec ECIInteractionSpec is = new ECIInteractionSpec(); // 5. Use the set methods on ECIInteractionSpec // to set the properties of execution. // Change these properties to suit the target program is.setCommareaLength(20); is.setFunctionName("PROGNAME"); is.setInteractionVerb(ECIInteractionSpec.SYNC_SEND_RECEIVE); // 6. Create a record object to contain the input data and populate data // Change the contents to suit the data required by the program RecordImpl in = new RecordImpl(); byte[] commarea = "COMMAREA contents".getBytes(); ByteArrayInputStream inStream = new ByteArrayInputStream(commarea); in.read(inStream); // 7. Create a record object to contain the output data RecordImpl out = new RecordImpl(); // 8. Call the execute method on the interaction interaction.execute(is, in, out); // 9. Read the data from the output record ByteArrayOutputStream outStream = new ByteArrayOutputStream(); out.write(outStream); commarea = outStream.toByteArray(); } catch (Exception e) { // Handle any exceptions by wrapping them into an IOException throw new IOException(e); } } // A simple class which extends Record and Streamable representing a commarea. public class RecordImpl implements Streamable, Record { private static final long serialVersionUID = -947604396867020977L; private String contents = new String(""); @Override public void read(InputStream is) { try { int total = is.available(); byte[] bytes = null; if (total > 0) { bytes = new byte[total]; is.read(bytes); } // Convert the bytes to a string. contents = new String(bytes); } catch (Exception e) { // Log the exception e.printStackTrace(); } } @Override public void write(OutputStream os) { try { // Output the string as bytes os.write(contents.getBytes()); } catch (Exception e) { // Log the exception e.printStackTrace(); } } @Override public String getRecordName() { // Required by Record, unused in this sample return ""; } @Override public void setRecordName(String newName) { // Required by Record, unused in this sample } @Override public void setRecordShortDescription(String newDesc) { // Required by Record, unused in this sample } @Override public String getRecordShortDescription() { // Required by Record, unused in this sample return ""; } @Override public Object clone() throws CloneNotSupportedException { // Required by Record, unused in this sample return super.clone(); } } }