Others pointed out some of the pros, so here are some cons if you deploy the background process to the same jvm as your web application.
- Starting and stopping the server running the web module means that you start and stop background processes, this may or may not be a problem for you.
- You are sharing a bunch with all three applications, if background processes consume a lot of memory or processor, which can affect your web application or if a web application consumes a lot of resources, this can affect background processes.
- Perhaps the web application should be deployed in a way that is accessible over the Internet, but background processes may be happy to work without any access to the network. So why put background processes on the Internet if you donโt need it.
- When updating the application server or frameworks or configuration, this means three things to check, if the background processes work independently, you can update them in a separate release cycle from the web application.
- Itโs easier to develop and test code outside the container. Starting background processes inside a container means a more complex development environment for background processes, you need to wait for the server to start, you will start depending on the resources of the container, which you then have to mock unit tests ... etc.
JPA is the same inside and outside the container. The only difference is how you get the EntityManager, it can be configured with Spring the same both in the container and outside it. CDI must be running outside the container.
The main areas of difference will be how you execute transactions with db, for example using Spring transactions or ejb transactions.
Update: To answer your question, write a comment: EntityManager is not thread safe in JPA, so there will be one entity manager per unit for each thread on the Java EE server. Creating and closing an Entity Manager is managed by the application server for you. Each entity administrator has a cache in it. You can configure a second-level cache that spans multiple entity managers. When starting outside the container, you will have to manage the number of JPA entity managers yourself, this will depend on the number of threads in the background process and the boundaries of the transactions that you want to have. If you look at a book called "Pro JPA2", there is a section that talks about the details of working inside or outside the container.
In my application, I don't have a background process, but every class that needs an entity manager just enters it using @PersistenceContext EntityManager em; and Spring makes sure that all of this works inside and outside the container. Spring 3.1 has a function called profiles, which makes it trivial to execute the same code inside our external container without changing one line of code. I am not a CDI user, so I donโt know if CDI has the equivalent of Spring 3.1 profile function.
ams
source share