1

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?

2

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.

  • I was under a misconception, thank you! I need some time to think through your answer, but I will mark it as "correct" in a day or two. – DavidS Apr 13 '15 at 16:27
  • I'm not sure how "anything can be declared a resource" is incompatible with "what resources does a JavaEE container provide?". Especially with the recent focus on CDI (@Inject), and the abstract nature of JNDI (it's just a string to get "things" from the container) it's not clear at all how it all works as a whole. – marcus Apr 15 '15 at 13:44
  • The point I'm trying to communicate @marcus, is that @Resource is not a reserved category of components, like the TimerService is – kolossus Apr 15 '15 at 14:47
  • Yes, that was a misconception on my part. I thought Resources were a special type of component. My line of thinking was "TimerService is pretty useful, but what the heck is it? It looks like a utility class. Maybe 'Resources' are utility classes. Does JavaEE have 'utility classes'? Are there others I may not have heard of?" In my opinion, TimerService is still a bit of an oddball in JavaEE. – DavidS Apr 15 '15 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.