📄 connect the enterprise with the jca, part 2.htm
字号:
adapter is a complex task, I can only scratch the surface in this article.
Nevertheless, by the end, you will understand a basic JCA adapter's
construction, and grasp the effort required to build your own.
<P>To accomplish those goals, I first describe the sample adapter's
capabilities, as well as how to deploy and run it. I then introduce the
implementation classes, followed by what occurs when the adapter executes in the
container. Finally, I discuss the lessons learned from creating the sample
adapter.
<P><STRONG>Note:</STRONG> To download the <CODE>myjca.rar</CODE> source code
that accompanies this article, see <A
href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-jca2_p.html#resources">Resources</A>.
<P><FONT size=+1><STRONG>What the sample JCA adapter implementation does (and
doesn't) contain </STRONG></FONT><BR>First, it's important to frame the
sample-adapter discussion by describing its functionality. This article's sample
adapter doesn't actually hook up to an enterprise system; it merely implements
the interfaces required to deploy the adapter and look up a connection.
<P>Moreover, the sample adapter implements only those classes required for the
JCA specification's connection management section. Further, most of the
adapter's method implementations contain print statements that let you see the
method calls' order, without hooking up a debugger. The sample adapter does not,
however, address transaction and security contacts.
<P>This article's sample adapter does not use the CCI (Common Client Interface)
interfaces. It strictly demonstrates the classes necessary to connect to an
enterprise system.
<P><FONT size=+1><STRONG>What you need to deploy the sample adapter
</STRONG></FONT><BR>To use the adapter (or any JCA adapter for that matter), you
need a J2EE application server with JCA 1.0 specification support. I used BEA's
WebLogic 6.1 server to test the sample adapter; however, other application
servers should work.
<P>To deploy the sample adapter with BEA's WebLogic 6.1, you must:
<OL>
<LI>Navigate to the connector tree in the Administration Console
<LI>Click on the "Install new connector" link
<LI>Navigate to the <A
href="http://www.javaworld.com/javaworld/jw-02-2002/jw-0201-jca2_p.html#resources"><CODE>myjca.rar</CODE></A>
file, then upload it </LI></OL>
<P>In a later section, you'll find instructions on how to compile and build
<CODE>myjca.rar</CODE>.
<P><FONT size=+1><STRONG>The sample class files </STRONG></FONT><BR>Next, let's
delve into the Java classes required to implement the sample JCA adapter. The
adapter includes two class categories:
<P>
<UL>
<LI><STRONG>Managed classes:</STRONG> The application server calls managed
classes to perform the connection management. They're needed only if the
application server is managing the connection (via a connection pool), a
likely situation.
<LI><STRONG>Physical connection classes:</STRONG> These required classes,
which the aforementioned managed classes may call, establish the connection to
the EIS (enterprise information systems). </LI></UL>
<P><STRONG>MyManagedConnectionFactory </STRONG><BR>With the
<CODE>MyManagedConnectionFactory</CODE> class, which implements the
<CODE>ManagedConnectionFacytory</CODE> interface, you create the
<CODE>MyConnectionFactory</CODE> and <CODE>MyManagedConnection</CODE> classes.
The <CODE>MyManagedConnectionFactory</CODE> class acts as the main entry point
for the application server to call into the adapter:
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.io.PrintWriter;<BR>import
java.io.Serializable;<BR>import java.sql.DriverManager;<BR>import
java.util.Iterator;<BR>import java.util.Set;<BR>import
javax.resource.ResourceException;<BR>import javax.resource.spi.*;<BR>import
javax.security.auth.Subject;<BR><BR>public class
MyManagedConnectionFactory<BR> implements
ManagedConnectionFactory,
Serializable<BR>{<BR><BR> public
MyManagedConnectionFactory()
{<BR> System.out.println("In
MyManagedConnectionFactory.constructor");<BR> }<BR><BR> public
Object createConnectionFactory(ConnectionManager cxManager) throws
ResourceException
{<BR> System.out.println("In
MyManagedConnectionFactory.createConnectionFactory,1");<BR> return
new MyDataSource(this,
cxManager);<BR> }<BR><BR> public
Object createConnectionFactory() throws ResourceException
{<BR><BR> System.out.println("In
MyManagedConnectionFactory.createManagedFactory,2");<BR> return
new MyDataSource(this,
null);<BR> }<BR><BR> public
ManagedConnection createManagedConnection(Subject subject, ConnectionRequestInfo
info)
{<BR> System.out.println("In
MyManagedConnectionFactory.createManagedConnection");<BR> return
new MyManagedConnection(this,
"test");<BR> }<BR><BR> public
ManagedConnection matchManagedConnections(Set connectionSet, Subject subject,
ConnectionRequestInfo
info)<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnectionFactory.matchManagedConnections");<BR> return
null;<BR> }<BR><BR> public void
setLogWriter(PrintWriter out) throws ResourceException
{<BR> System.out.println("In
MyManagedConnectionFactory.setLogWriter");<BR> }<BR><BR> public
PrintWriter getLogWriter() throws ResourceException
{<BR> System.out.println("In
MyManagedConnectionFactory.getLogWriter");<BR> return
DriverManager.getLogWriter();<BR> }<BR><BR> public
boolean equals(Object obj)
{<BR> if(obj ==
null)<BR> return
false;<BR> if(obj instanceof
MyManagedConnectionFactory)<BR> {<BR> int
hash1 =
((MyManagedConnectionFactory)obj).hashCode();<BR> int
hash2 =
hashCode();<BR> return
hash1 ==
hash2;<BR> }<BR> else<BR> {<BR> return
false;<BR> }<BR> }<BR><BR> public
int
hashCode()<BR> {<BR> return
1;<BR> }<BR>}<BR></CODE>
<P><STRONG>MyManagedConnection </STRONG><BR>The <CODE>MyManagedConnection</CODE>
class implements the <CODE>ManagedConnection</CODE> interface.
<CODE>MyManagedConnection</CODE> encapsulates the adapter's physical connection,
in this case the <CODE>MyConnection</CODE> class:
<P><CODE>
<P><BR>package myjca;<BR><BR>import java.io.PrintWriter;<BR>import
java.sql.Connection;<BR>import java.sql.SQLException;<BR>import
java.util.*;<BR>import javax.resource.NotSupportedException;<BR>import
javax.resource.ResourceException;<BR>import javax.resource.spi.*;<BR>import
javax.security.auth.Subject;<BR>import
javax.transaction.xa.XAResource;<BR><BR><BR>public class
MyManagedConnection<BR> implements
ManagedConnection<BR>{<BR> private
MyConnectionEventListener myListener;<BR> private String
user;<BR> private ManagedConnectionFactory
mcf;<BR> private PrintWriter
logWriter;<BR> private boolean
destroyed;<BR> private Set
connectionSet;<BR><BR> MyManagedConnection(ManagedConnectionFactory
mcf, String
user)<BR> {<BR> System.out.println("In
MyManagedConnection");<BR> this.mcf =
mcf;<BR> this.user =
user;<BR> connectionSet = new
HashSet();<BR> myListener = new
MyConnectionEventListener(this);<BR> }<BR><BR> private
void throwResourceException(SQLException ex)<BR> throws
ResourceException<BR> {<BR> ResourceException
re = new ResourceException("SQLException: "
+<BR>ex.getMessage());<BR> re.setLinkedException(ex);<BR> throw
re;<BR> }<BR><BR> public Object getConnection(Subject
subject,
ConnectionRequestInfo<BR>connectionRequestInfo)<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.getConnection");<BR> MyConnection
myCon = new
MyConnection(this);<BR> addMyConnection(myCon);<BR> return
myCon;<BR> }<BR><BR> public void
destroy()<BR> {<BR> System.out.println("In
MyManagedConnection.destroy");<BR> destroyed
= true;<BR> }<BR><BR> public void
cleanup()<BR>
{<BR> System.out.println("In
MyManagedConnection.cleanup");<BR> }<BR><BR> public void
associateConnection(Object connection)<BR>
{<BR> System.out.println("In
MyManagedConnection.associateConnection");<BR> }<BR><BR> public
void addConnectionEventListener(ConnectionEventListener
listener)<BR> {<BR> System.out.println("In
MyManagedConnection.addConnectionEventListener");<BR> myListener.addConnectorListener(listener);<BR> }<BR><BR> public
void removeConnectionEventListener(ConnectionEventListener
listener)<BR> {<BR> System.out.println("In
MyManagedConnection.removeConnectionEventListener");<BR> myListener.removeConnectorListener(listener);<BR> }<BR><BR> public
XAResource getXAResource()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.getXAResource");<BR> return
null;<BR> }<BR><BR> public LocalTransaction
getLocalTransaction()<BR> {<BR>
System.out.println("In
MyManagedConnection.getLocalTransaction");<BR> return
null;<BR> }<BR><BR> public ManagedConnectionMetaData
getMetaData()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.getMetaData");<BR> return new
MyConnectionMetaData(this);<BR> }<BR><BR><BR> public void
setLogWriter(PrintWriter out)<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.setLogWriter");<BR> logWriter =
out;<BR> }<BR><BR> public PrintWriter
getLogWriter()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.getLogWriter");<BR> return
logWriter;<BR> }<BR><BR> Connection
getMyConnection()<BR> throws
ResourceException<BR> {<BR> System.out.println("In
MyManagedConnection.getMyConnection");<BR> return
null;<BR> }<BR><BR> boolean
isDestroyed()<BR> {<BR> System.out.println("In
MyManagedConnection.isDestroyed");<BR> return
destroyed;<BR> }<BR><BR> String
getUserName()<BR> {<BR> System.out.println("In
MyManagedConnection.getUserName");<BR> return
user;<BR> }<BR><BR> void sendEvent(int eventType,
Exception
ex)<BR> {<BR> System.out.println("In
MyManagedConnection.sendEvent,1");<BR> myListener.sendEvent(eventType,
ex, null);<BR> }<BR><BR> void sendEvent(int eventType,
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -