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]