|
|
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
Page 5 of 6
<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:
@Resource DataSource moviesDatabase
If needed, your resource reference could be more explicit about how the wiring is resolved:
@Resource(name = "moviesDatabase", type = javax.sql.DataSource.class) DataSource database;
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:
@Resource name attribute is not present then TomEE matches the variable name to a resource name (as shown in Listing 2)
@Resource name attribute is present then TomEE matches the resource by its explicit name (as shown in Listing 3)
InitialContext and <resource-ref> elements in its web.xml file.
Now we can apply what we know about defining the database connection pool to to other resources:
<Resource> element in the conf/tomee.xml file with the type of resource you want to configure.
<Resource> node
@Resource annotation
Below is a sample listing of the valid resource types (see Resources for a complete listing):
Listing 4 shows the configuration for an ActiveMQ resource adapter, a JMS connection factory, and a Queue and Topic.
<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.
More about deployment servers, containers, and platforms on JavaWorld
From Apache's OpenEJB/TomEE homepage
More from JavaWorld