How To Expose And Inject A POJO ...Into An EJB 3
A POJO:
public class CustomResource{
public final static String JNDI_NAME = "theName";
public void doSomething(){
System.out.println("#Done !");
}
}
can be easily exposed (=bound to JNDI) with a singleton bean:
@Startup
@Singleton
public class ResourceBinder {
@PostConstruct
public void bindResources(){
try {
Context context = new InitialContext();
context.rebind(CustomResource.JNDI_NAME, new CustomResource());
System.out.println("Resource bound...");
System.out.println(" " + context.lookup(CustomResource.JNDI_NAME));
} catch (NamingException ex) {
throw new IllegalStateException("Cannot bind resource " +ex,ex);
}
}
}
...and injected into a Stateless, Stateful, Singleton Bean or other resources like e.g. Servlets, Backing Beans etc:
@Singleton
@Startup
@DependsOn("ResourceBinder")
public class CustomResourceClient {
@Resource(mappedName=CustomResource.JNDI_NAME)
private CustomResource resource;
@PostConstruct
public void invokeResource(){
this.resource.doSomething();
System.out.println("----Resource invoked");
}
}
You should see the following output in the log files (tested with Glassfish v3b70):
INFO: Portable JNDI names for EJB CustomResourceClient : [java:global/ResourceBinder/CustomResourceClient!com.abien.patterns.kitchensink.resourcebinder.CustomResourceClient, java:global/ResourceBinder/CustomResourceClient]
INFO: Portable JNDI names for EJB ResourceBinder : [java:global/ResourceBinder/ResourceBinder, java:global/ResourceBinder/ResourceBinder!com.abien.patterns.kitchensink.resourcebinder.ResourceBinder]
INFO: Resource bound...
INFO: com.abien.patterns.kitchensink.resourcebinder.CustomResource@50058560
INFO: #Done !
INFO: ----Resource invoked
The ResourceBinder project was tested with NetBeans 6.8beta and Glassfish v3b70. It was pushed into: http://kenai.com/projects/javaee-patterns/. Closing remarks:
- It is easier and better to declare a POJO as a Session Bean. You gain a lot with almost no trade-offs.
- You should be careful with the concurrency. Such an exposed POJO will be accessed by several threads / transactions concurrently.
- You easily inject POJOs with interceptors as well.
- You should never use System.out.println on the server in production, because of:
public void println(String x) {
synchronized (this) {
print(x);
newLine();
}
}
[See "Resource Binder" pattern, page 243, in "Real World Java EE Patterns Rethinking Best Practices" book for more in-depth discussion]
Adam, have you seen what JSR-299 can do in EE6?
Posted by Gavin on November 02, 2009 at 06:43 PM CET #
Hi Gavin,
sure. Even presented JSR-299 last week during a Java EE 6 workshop. Weld is great :-). However, you can implement 80 % of all use cases efficiently with vanilla EJBs.
Btw. JSR-330 is also interesting :-),
thanks for your comment!,
adam
Posted by Adam Bien on November 02, 2009 at 06:52 PM CET #
Adam, is it advisable to use this approach to pass technical user/context data from say a Servlet to an EJB (instead of ThreadLocal, you know what I mean ;-) )?
Is context.rebind an expensive operation since in my case I would do a lot of rebinds?
Posted by Detlef on November 02, 2009 at 11:31 PM CET #
Detlef,
it is absolutely not advisable. I forgot your email - sorry.
Gruss :-),
adam
Posted by Adam Bien on November 03, 2009 at 09:53 AM CET #