Find this tutorial in:
Try the Tutorial
Resources can receive lifecycle events by implementing javax.resource.spi.ResourceAdapter.
Many resources can take advantage of the lifecycle events provided by the Java Connector Architecture (JCA). In Resin, any resource which implements javax.resource.spi.ResourceAdapter will automatically receive the JCA resource events and have access to the JCA capabilities. Resources will generally want to implement the start(ctx) and stop() methods.
The start method is called when the environment is started. In the case of a web-app, this will be before any servlet is initialized, but after all the resources have been added to JNDI.
The ctx argument provides some capabilities including timers, and work management. This demo will not use those capabilities.
The stop method is called when the environment closes, e.g. when the web-app is destroyed.
The complete lifecycle looks like the following:
The javax.resource.spi.ResourceAdapter has a few more methods which our resource does not need. To avoid cluttering up the example, our resource extends from com.caucho.jca.AbstractResourceAdapter, which provides stubs for those methods.
The web.xml configuration is identical to any other resource configuration. Resin detects that TestResource has implemented ResourceAdapter and adds the capabilities.
The demonstration page is simple. It just looks up the resource in JNDI and displays it. Because the toString() method contains the _status value, we actually see something useful.
Because the ResourceAdapter interface is part of the JCA spec, you can write ResourceAdapters in a portable manner. So if you were forced to use another application server for some reason, you could still use your architecture and your resource. (The configuration on the other app server would probably be more cumbersome and involve creating rar files and XML configuration, but that's why you're using Resin.)