Here there are more questions that I kept being asked about jEmbedded:
* What's the best way to use jEmbedded in a web environment?
Depends on how are you planning to use the container as a singleton, prototype or instance by thread.
Singleton
If you are planning to use it as a one single container to serve the whole web app you can create and dispose the instance in a ServletContext listener:
import javax.servlet.ServletContextListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.*;
@Container(instanceType=SINGLETON_VM)
public class EmbeddedContainerListener implements ServletContextListener {
{
private ServletContext context = null;
private EmbeddedHandler handler = null;
public void contextDestroyed(ServletContextEvent event)
{
this.context = null;
handler.stop();
}
public void contextInitialized(ServletContextEvent event)
{
this.context = event.getServletContext();
handler = EmbeddedHandlerFactory.getInstance(EmbeddedContainerListener.class, *.class);
handler.start();
}
}
In order to access the container from any part of the web app:
EmbededHandler handler = ContainerHolder.getCurrentThreadContainer();
If you are using Spring, you can use an implementation of the ContainerListener to create the contaner.
As an instance per thread.
Let's say that you need a fresh container per request, in a MCV controller for instance. In order to do that just create and dispose the instance there.
Prototype instances.
You can create and dispose these instances anywhere but remember to dispose them!
* What are the differences between @Container and @Repository annotations.
@Container is just an annotation to control how the instances are being created when EmbeddedHandlerFactory.getInstance() is invoked.
@Repository is a way to create logical collections of services, components etc, for example different layers of services or components (as you can create them as a hierarchy).
This is useful even when you don't want to create complex collections (as a tree of services) just as an entry point for the container:
@Repository (id="baseServicesRepo", resources={*.class})
public class RepoEntryPoint {
handler = EmbeddedHandlerFactory.getInstance(RepoEntryPoint.class);
...
}
Now for instance lets say you need to create a different group of services that need to reference the former repository for composition:
@Repository (id="extendedServices", resources={*.class}, parent="myRepo")
Now the new container would have access to the to the parent repository as well.
I will be creating a full documentation in September, some web support out of the box and more examples (after my holidays).
Any feedback and comments are welcome.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment