Question

How can I find what "Resources" a Java EE container provides? Can I discover this programmatically, or is there documentation I should refer to? (Note that the Java EE tutorial is vague on this subject.)

Here is an example of what I'm talking about:

I was reading the Java EE 6 documentation for Using the Timer Service, and I noticed that it doesn't really explain what the TimerService technically is. If you read the documentation, you eventually see an example that a TimerService is obtainable using the @Resource annotation.

From this I take it that a TimerService is a Resource provided by the container. If it weren't for the example, I never would have realized this without reading a third-party blog post: at no point does the official tutorial say that "TimerService is a container-provided Resource". TimerService is very useful, so I would like to know what other Resources are available.

To be clear, I'm not looking for a third-party blog post, and the official Java EE tutorial is just that, a tutorial: it's not comprehensive. And I briefly checked the Java EE 7 Platform JSR, but I couldn't find a reference to TimerService.

Is there a way to programmatically find the Resources a container provides, or is there a documented list of Resources a Java EE container must provide?

Was it helpful?

Solution

You're under a bit of misconception here. First, divorce the @Resource annotation from javax.ejb.TimerService. Now read the following excerpt from the JavaEE api:

The Resource annotation marks a resource that is needed by the application. This annotation may be applied to an application component class, or to fields or methods of the component class. When the annotation is applied to a field or method, the container will inject an instance of the requested resource into the application component when the component is initialized. If the annotation is applied to the component class, the annotation declares a resource that the application will look up at runtime.

What that implies is that anything can be declared a resource, to be injected by @Resource. The annotation can be used to inject anything, ranging from primitive <env-entry/> definitions in the web.xml, to various connection managers (Java Mail, Datasources e.t.c) and it is done on-demand

Typically, it's items that are eligible for JNDI-based access, that are injected using @Resource. What this means is that, you need to have a JNDI name, to address a resource before going to look for it (the default value of the name element is the field name qualified by the class name). JSR-250, which defines the @Resource annotation lists all the viable targets for that annotation.

If however, you're confident you really want to know everything that the container might be able to inject, you could try listing all available JNDI entries. I don't recommend it.


As for TimerService in specific, a container must provide an implementation of TimerService in order to be JavaEE compliant. This is a quote from JSR 345:

The container must provide the implementation of the TimerService, Timer, and TimerHandle interfaces.

If you look at the table of contents of JSR 345, you'll see that TimerService gets a chapter to itself, at the same level as topics like "Security Management". This just goes to show that while TimerService may feel like a utility class, it's arguably at the same level of abstraction as any other container-provided service

If you're looking for an authoritative list of mandatory container-provided services, the spec is the place to look. Knowing which spec to look at can be tricky, but as TimerService is in the javax.ejb package, the EJB spec is a good guess.

Licensed under: CC-BY-SA with attribution
scroll top