Newsletter sign-up
View all newsletters

Enterprise Java Newsletter
Stay up to date on the latest tutorials and Java community news posted on JavaWorld

Sponsored Links

Optimize with a SATA RAID Storage Solution
Range of capacities as low as $1250 per TB. Ideal if you currently rely on servers/disks/JBODs

Open source Java projects: TomEE

Scale to enterprise with the Java EE 6 Web container built on Tomcat

  • Print
  • Feedback

Page 5 of 6

Listing 1. Sample database configuration

<Resource id="moviesDatabase" type="DataSource">
  JdbcDriver com.mysql.jdbc.Driver    
  JdbcUrl jdbc:mysql:localhost:3306/moviesdb    
  UserName sa    
  Password secret    
  JtaManaged true    
</Resource>

This example creates a JDBC connection pool pointing to a MySQL data source named "moviesdb," which runs on localhost. Because the size parameters are not included, this connection pool will have a maximum of 20 connections and prepared statements will not be cached.

Once you have the resource defined you can import it into your code as follows:

Listing 2. Matching the variable name by its resource name

@Resource DataSource moviesDatabase

If needed, your resource reference could be more explicit about how the wiring is resolved:

Listing 3. Matching the variable name by its explicit name

@Resource(name = "moviesDatabase", type = javax.sql.DataSource.class) DataSource database;

Resource resolution

The purpose of resolving resources is to inject the correct resources, either defined in your container or in your application's configuration files, into components in your application. For example, if you define a database connection pool in your environment, that database connection pool gets injected into your classes via resource resolution. TomEE's process of resolving resources can be summarized as follows:

  • If the @Resource name attribute is not present then TomEE matches the variable name to a resource name (as shown in Listing 2)
  • If the @Resource name attribute is present then TomEE matches the resource by its explicit name (as shown in Listing 3)
  • The application can explicitly look-up the resource in JNDI using an InitialContext and <resource-ref> elements in its web.xml file.

Defining other types of resources

Now we can apply what we know about defining the database connection pool to to other resources:

  1. Define a <Resource> element in the conf/tomee.xml file with the type of resource you want to configure.
  2. Define the required name/value pairs to configure the resource (resource-type dependent) inside the body of the <Resource> node
  3. Wire the resource into your application using an @Resource annotation

Below is a sample listing of the valid resource types (see Resources for a complete listing):

  • ActiveMQResourceAdapter
  • javax.jms.ConnectionFactory
  • javax.jms.Queue
  • javax.jms.Topic
  • org.omg.CORBA.ORB
  • javax.mail.Session

Listing 4 shows the configuration for an ActiveMQ resource adapter, a JMS connection factory, and a Queue and Topic.

Listing 4. A sample JMS configuration

<Resource id="MyJmsResourceAdapter" type="ActiveMQResourceAdapter">
        BrokerXmlConfig =  xbean:file:conf/activemq.xml
        ServerUrl       =  tcp://someHostName:61616
    </Resource>

    <Resource id="MyJmsConnectionFactory" type="javax.jms.ConnectionFactory">
        ResourceAdapter = MyJmsResourceAdapter
    </Resource>

    <Container id="MyJmsMdbContainer" ctype="MESSAGE">
        ResourceAdapter = MyJmsResourceAdapter
    </Container>

    <Resource id="FooQueue" type="javax.jms.Queue"/>
    <Resource id="BarTopic" type="javax.jms.Topic"/>

Note that TomEE manages references and dependencies between resources. You can then wire these resources into your application using the @Resource annotation, as shown in Listing 5. Note that the rules for resource-resolution, defined above, hold true here.

  • Print
  • Feedback

Resources

More about deployment servers, containers, and platforms on JavaWorld

From Apache's OpenEJB/TomEE homepage

More from JavaWorld