A Resource with JCA Lifecycle
Resin 3.0

Features
Installation
Configuration
Web Applications
IOC/AOP
Resources
JSP
Servlets and Filters
Portlets
Databases
Admin (JMX)
CMP
EJB
Amber
EJB 3.0
Security
XML and XSLT
XTP
JMS
Performance
Protocols
Third-party
Troubleshooting/FAQ

Library
Tutorials
Resource FAQ

Basic Resource
Injection
JCA Lifecycle
JCA Work
JCA Timer
JCA Cron
JCA Connector
JNDI appconfig
Injection
Tutorials
JCA Work

Find this tutorial in: /usr/local/resin/webapps/resin-doc/resource/tutorial/jca-lifecycle
Try the Tutorial
Resources can receive lifecycle events by implementing javax.resource.spi.ResourceAdapter.

  1. ResourceAdapter
  2. Compatibility

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:

  1. The environment is created (server, host, web-app, etc.)
  2. The resources are created and configured:
    1. new test.TestResource() is called from that environment
    2. The configuration setters configure the resource (see Bean-style initialization ).
    3. The init() method is called if available (part of Bean-style initialization)
  3. The resources are started
    1. The start(ctx) method is called for the ResourceAdapters
  4. The load-on-startup servlets start (for web-app only)
  5. Requests are now allowed for the environment.
  6. The environment starts shutting down (web-app, host, server)
  7. The stop() method is called for any ResourceAdapters

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.

TestResource.java
package test;

import javax.resource.spi.BootstrapContext;
import javax.resource.spi.ResourceAdapterInternalException;

import com.caucho.jca.AbstractResourceAdapter;

public class TestResource extends AbstractResourceAdapter {
  private String _status = "new";

  /**
   * Called when the resource adapter starts.
   */
  public void start(BootstrapContext ctx)
    throws ResourceAdapterInternalException
  {
    _status = "started";
  }
  
  /**
   * Called when the resource adapter stops.
   */
  public void stop()
    throws ResourceAdapterInternalException
  {
    _status = "stopped";
  }

  public String toString()
  {
    return "TestResource[" + _status + "]";
  }
}

The web.xml configuration is identical to any other resource configuration. Resin detects that TestResource has implemented ResourceAdapter and adds the capabilities.

web.xml
<web-app xmlns="http://caucho.com/ns/resin">
  <resource jndi-name="jca/test" type="test.TestResource">
  </resource>
</web-app>

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.

demo.jsp
<%@ page import='javax.naming.InitialContext' %>
<%= new InitialContext().lookup("java:comp/env/jca/test") %>

TestResult[started]

Compatibility

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.)

Try the Tutorial


Injection
Tutorials
JCA Work
Copyright © 1998-2005 Caucho Technology, Inc. All rights reserved.
Resin® is a registered trademark, and HardCoretm and Quercustm are trademarks of Caucho Technology, Inc.