I'm working on a project where we need to build several 'standalone' modules connecting to one database. These modules are mainly background business processes, so not much frontend. Except for one web module showing the data and allowing basic CRUD functions. For this we are planning to use the following techniques:
The initial plan was to just create a jar file (with a main method) per module and install it as a (windows) service via a service wrapper. For our web module we would use Glassfish or JBoss to run it. However, lately Java EE came to our minds. We could run all our modules in a Java EE container like Glassfish or JBoss, not only our web module. Some questions for the case that we go for Java EE:
Key Differences Between Java and Java EE The Java EE platform provides an API and runtime environment for developing and running large-scale applications. Java SE platform consists of a virtual machine, development tools, deployment technologies, and other libraries commonly used in Java.
Almost 4 out of 10 people use the latest version of Java EE while Java EE 7 still remains quite popular.
The Java EE platform is built on top of the Java SE platform. The Java EE platform provides an API and runtime environment for developing and running large-scale, multi-tiered, scalable, reliable, and secure network applications.
The report claims that Java EE is not lightweight, has become obsolete and hasn't kept pace with modern architectural trends. These statements are made very confidently, yet are rather surprising, especially coming from such a well-known advisory company!
Others have pointed out some of the pros, so here are some cons if you deploy the background process into the same jvm as your web app.
JPA is the same inside and outside the container. The only difference is how you obtain an EntityManager, this can be configured with Spring to be the same both in and out of the container. CDI should be runnable outside the container.
Major areas of difference will be how you do transactions with the db for example using Spring transactions vs. ejb transactions.
Update: To answer your question form the comment: in JPA the EntityManager is not thread safe so in a Java EE server there will be one entity manager per persistence unit per thread. The Entity manager creation and closure is managed by the app server for you. Every entity manager has a cache within it. It is possible to configure a second level cache that spans multiple entity managers. In running outside the container you will have to manage the number of JPA entity managers yourself, this will depend on the number of threads in your background process and transaction boundaries that you want to have. If you look at a book called "Pro JPA2" there is a section that talks about the details of running inside or outside the container.
In my application I don't have a background process but every class that needs an entity manager just gets it injected using @PersistenceContext EntityManager em;
and spring takes care of making it all work inside and outside the container. Spring 3.1 has a feature called profiles that makes it trivial to have the same code run inside our outside a container without changing a single line of code. I am not a CDI user so I don't know if CDI has an equivalent of the spring 3.1 profiles feature.
If all of the modules (batch + real time) relate to one product then bundling them together is a good approach. So here is my suggestion
Answers to your specific questions
CDI can be used without EJB as well. Anyway get rid of spring as I doesn't see a value add for your simple project.
There is no difference except the fact that you can get the DataSource from JNDI.
Yes it does make sense to bundle together batch and real time aspects of a single product as long as you doesn't see any performance issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With