技术开发 频道

J2EE连接器开发实践之: J2EE连接器的开发

  资源适配器的开发

  开发资源适配器,我们从最基本的连接(Connection)类开始。

  DemoConnection

  DemoConnection扩展了CCI的Connection接口,它由客户端程序使用,代表了到EIS的"虚拟"连接,通过这个"虚拟"的连接客户端可以调用EIS。需要注意的是,虚拟连接关闭时,物理连接不一定关闭。DemoConnection定义了资源适配器所实现的业务方法。如例程2所示。

  例程2 DemoConnection的代码

1 package com.hellking.jca;
2 import javax.resource.cci.Connection;
3 import javax.resource.ResourceException;
4 public interface DemoConnection extends Connection
5 {
6     //业务方法
7     public String sayHello(String name)throws ResourceException;
8 }
9

 

  DemoConnectionImpl

  DemoConnectionImpl是DemoConnection的实现类,它通过ManagedConnection来完成具体的业务方法。ManagedConnection是代表到EIS的物理连接,将在后面介绍。

  例程3 DemoConnectionImpl大代码

1 package com.hellking.jca;
2 import javax.resource.*;
3 import javax.resource.spi.*;
4 import javax.resource.cci.*;
5 import javax.security.auth.*;
6 import java.util.*;
7 import java.io.*;
8 //连接实现类,它通过DemoManagedConnection来完成具体的任务
9 public class DemoConnectionImpl implements DemoConnection
10 {
11     protected PrintWriter out;//logOut
12     protected DemoManagedConnection demoManagedConnection;
13     
14    //关闭连接,释放资源
15    public void close()
16    {
17       if (demoManagedConnection == null) return;  
18       demoManagedConnection.removeConnection(this);
19       demoManagedConnection.connectionClosedEvent();
20       demoManagedConnection = null;
21    }
22   //返回和这个连接关联的被管理连接
23   public DemoManagedConnection getManager()
24   {
25       return demoManagedConnection;
26   }
27   //设置和这个连接关联的被管理连接
28   public void setManager (DemoManagedConnection manager)
29   {
30       this.demoManagedConnection =manager;
31   }
32   
33   //业务方法,它通过调用被管理的连接来实现。
34   public String sayHello(String name)throws ResourceException
35   {
36         return demoManagedConnection.sayHello (name);
37    }
38   
39   //使连接无效
40   public void invalidate()
41   {
42       demoManagedConnection = null;
43   }  
44     public void setLogWriter(PrintWriter out)
45     {
46        this.out = out;
47      }
48     
49     public PrintWriter getLogWriter()
50     {
51       return out;
52     }
53         
54    public ConnectionMetaData getMetaData()  
55    {
56         return null;
57    }
58    public ResultSetInfo getResultSetInfo()  
59    {
60         return null;
61    }
62    public javax.resource.cci.LocalTransaction getLocalTransaction()  
63    {
64           return null;
65    }
66    public  Interaction createInteraction()
67    {
68          return null;
69    }
70
71 }
72
0
相关文章