Java EE vs Standalone

I am working on a project where we need to create several "stand-alone" modules connecting to the same database. These modules are mostly background business processes, so there are not many interfaces. With the exception of one web module that displays data and allows you to perform basic CRUD functions. To do this, we plan to use the following methods:

  • JPA2 (using hibernate-jpa implementation)
  • CDI (using spring implementation)
  • JSF2 + surfaces (for our web module)

The initial plan was to simply create a jar file (with the main method) for each module and install it as a service (windows) through the service shell. For our web module, we will use Glassfish or JBoss to launch it. However, recently Java EE came to mind. We could run all of our modules in a Java EE container, such as Glassfish or JBoss, and not just in our web module. Some questions for the case that we are going to Java EE:

  • Can / use / CDI with spring? Or should we switch to EJB3?
  • What are the consequences for JPA when we use it from a container instead of stand-alone modules? Is there any difference?
  • Since most of our modules are not connected to the Internet, does it still make sense to run them in a Java EE container?
+8
spring java-ee hibernate jpa cdi
source share
3 answers

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.

+3
source share

If all modules (batch or real time) belong to one product, then combining them together is a good approach. So here is my suggestion

  • Combine all your modules together in one ear.
  • Use Java EE 6 and get rid of spring. CDI is intended for use in Java EE. For batch type operations, try using Asynchronous EJBs or MDB.

Answers to your specific questions

  • Can / use / CDI with spring? Or should we switch to EJB3?

CDI can be used without EJB. In any case, get rid of spring, because I do not see the extra value for your simple project.

  • What are the consequences for JPA when we use it from a container instead of stand-alone modules? Is there any difference?

There is no difference besides the fact that you can get a DataSource from JNDI.

  • Since most of our modules are not connected to the Internet, does it still make sense to run them in a Java EE container?

Yes, it makes sense to combine the batch and real aspects of a single product if you don't see performance issues.

+3
source share

It makes sense to start the application server rather than a separate java program for common reasons such as

1) You can use CDI with spring, as EJB3 is also based on a similar concept. 2) There is no difference with JPA, except that if you need to add more volume to the application later, loading can be added by adding more machines that run the same application. However, please note that this is a trivial amount of work and therefore it depends on the business requirements to make a choice 3) Application servers win stand-alone applications for reasons of built-in security, reliability, management and scalability over stand-alone Java applications.

+1
source share

All Articles