Setting the multiple JREs in the same application - java

I have implemented one application using JDK 1.6 64-bit, JSF, Tomcat server 64-bit etc at user system.I am integrating some devices(i.e. finereader, scanner, etc) into this application. Some of devices libraries are supported only on JRE 32-bit Only.
So, device integrated screens to be run on JRE 32-bit and non device screens should be run on a JRE 64-bit. Can I configure multiple JRE's in the same application? If possible, how?

A Java application runs in a JVM. The JVM is part of one single JRE. You could split your application into two parts, which each run in their own JVM. These applications then would have to communicate with each other to coordinate the user experience. This does not look like a good solution to me.

It all depends on how you define "application."
First, in regard to your question, each JVM instance runs a single kind of Java--32 or 64 bit--Java version and such.
Sometimes, an application consists of a single JVM running a single java executable, usually a jar and some stuff on the classpath with a single 'main'
Sometimes, an application consists of multiple JVMs running on one or more boxes. In this case, each JVM is running a single java executable. But there has to be some sort of communication between these executable parts to make it function as an application.
Alternatively, the same executable might run on multiple JVMs and we still call it one application. In this case, there would be some sort of outside stuff that decides how to allocate the work of the application amoung the multiple JVMs. For example, you could run 18 instances of Tomcat on 9 boxes with a hardware load balancer dividing up the network requests and assigning each one to one of the Tomcat instances. In this case, though, part of the application is probably running on 1000's of user's computers inside a browser.
Sometimes, we say multiple applications are running under another application. In this case, we might call the master application a container. One example is Tomcat. In this case, Tomcat manages the load of requests for each of the separate applications because the HTTP requests come in off the network with information in the header indicating which one handles that request.
You already say that you have some code running under Tomcat. Tomcat is a single executable (It runs in one JVM and has one variety of Java) and it manages running your one Java executable supplied as a .war file, usually. There could be other Java applications running in other JVMs that communicate through that Tomcat and with your executable. Or there might not be other JVMs that your executable communicates with running somewhere else.
So, you can see, the real answer is "it depends." If you have multiple JVM communicating in some way, you could have different Java varieties. If it's all running under a single Tomcat instance, then you have a single variety of Java.

Related

Confused about Java Memory [duplicate]

Is the same JVM used by all Java applications running or, does 'one JVM per Java application' apply? (say the applications are IntelliJ IDEA, a server and NetBeans for example)
Further, is there any connection between JVMs assigned and processes used by each Java application?
Generally speaking, each application will get its own JVM instance and its own OS-level process and each JVM instance is independent of each other.
There are some implementation details such as Class Data Sharing, where multiple JVM instances might share some data/memory but those have no user-visible effect to the applications (except for improved startup time, hopefully).
A common scenario however is a single application server (or "web server") such as Glassfish or Tomcat running multiple web applications. In this case, multiple web applications can share a JVM.
There's one JVM per Java application. There shouldn't be any connection between them unless you establish one, e.g. with networking. If you're working inside of an IDE, the code you write generally runs in a separate JVM. The IDE will typically connect the separate JVM for debugging. If you're dealing with multiple web applications they could share the same JVM if they're deployed to the same web container.
In theory you can run multiple applications in a JVM. In practice, they can interfere with each other in various ways. For example:
The JVM has one set of System.in/out/err, one default encoding, one default locale, one set of system properties, and so on.
If one application changes these, it affects all applications.
Any application that calls System.exit() kills all applications.
If one application thread goes wild, and consumes too much CPU or memory it will affect the other applications too.
Short answer: often, yes, you'll get one application per JVM.
Long answer: the JVM can be used that way, and that may be the best option, but it doesn't have to be.
It all depends on what you consider to be an 'application'. An IDE is a good example of an application which is presented to its end users (i.e. us) as a single entity but which is actually comprised of multiple underlying applications (compilers, test runners, static analysis tools, packagers, package managers, project / dependency management tools, etc). In that case there are a variety of tricks which the IDE uses to ensure that the user experiences an integrated experience while also being shielded (to some extent) from the individual vagaries of the underlying tools. One such trick is to do some things in a separate JVM, communicating either via text files or via the application-level debugging facilities.
Application servers (Wildfly, Glassfish, Websphere, Weblogic, etc) are applications whose raison d'etre is to act as containers for other applications to run in. In that case, from one perspective, there's a single JVM per application (i.e. one JVM is used to run the entire application server) but there are actually multiple applications contained within that JVM in their own right, each logically separated from each other in their own classloader (reducing the possibility of accidental in-process crosstalk).
So, it all really depends on what you consider an application to be. If you're purely talking about "the thing which runs when 'main()' is called", then you're looking at one application per JVM - when the OS starts the JVM, the JVM runs a single class's public static void main() method.
But once your applications start getting more complicated your boundaries become more blurred. An IDE such as Intellij or Eclipse will reuse much of the same stuff as 'javac', either in the same JVM or a different one, as well as doing different work (such as repainting the screen). And users of a web application on a (shared JVM) application server may actually be using much the same 'core' application as could be used locally via the command line.
Number of JVMs running is the number of executables invoked.
Each such application invokes its own java executable (java.exe/ javaw.exe etx for windows) which means each is running in a separate JVM.
Any application which has shared libraries will share the same copy of those libraries. Java has a fair amount of shared libraries. However, you won't notice the difference except for some memory saved.
Little late here however this info may be useful for somebody. In a Linux system, if you want to know how many JVMs are running you can try this command
$ ps -ef | grep "[j]ava" | wc -l
ps to list process, grep to search process containing "java" and wc to count lines returned
Actually this is one question that can have very confusing answers. To keep it real short:
Yes per java process, per JVM.
Runtime and ProcessBuilder follow this rule.
Loading jars using reflection and then executing the main won't spawn new JVM.

How jvm distributes between tomcat and deployed Java applications

We have Tomcat deployed with two Java based web applications. How can I tune the performance of one of the applications without affecting the other one if they are running in the same JVM?
Your tomcat run in a JVM with both your applications. All 3 run in the same JVM. So if you tune the JVM (like max memory usage), everything will be affected by it. As far as I know there is no ways to indicate that you want more resources allocated to 1 of your applications.
The two web applications are deployed within the same JVM (including tomcat). So there is just one JVM that tomcat and the two web applications reside in. This is your current scenario.
On the other hand if you would like to tune the web applications separately - say in terms of performance etc. then the easiest thing would be to deploy them on two different hosts / machines. This way the two web applications would be running in their own JVM and would be completely isolated from each other and can be tuned independently. Note: you would need to think this through in terms of requirements of another host and yet another for a reverse proxy. See EJP's comment below.
If that is not possible then you could have two different JVMs brought up each with a tomcat and one of the web applications. This way you could tune the two JVMs separately: you could tune the tomcat servers running separately within the two different JVMs, you could attempt to run them with different allocations of OS resources etc. But that is an involved topic for another discussion. How would you achieve two different JVMs running a tomcat and one of the web application each? To achieve that you utilize the CATALINA_BASE approach wherein the two web applications are deployed to two differently located "webapps" folders. I will not go into details on this but leave you with a link. Note: you might need a reverse proxy as well in this case.
With the two different JVM approach (two different java processes), the respective JVMs can be tuned separately. I prefer this approach as this way it is easier to have separate things like log configurations, restart one without affecting the other and so on.
Since you are talking about performance, let me also mention in passing that whatever approach you might take, keep in mind that that in certain Operating Systems especially linux, you could restrict or allocate resource such as cpu, memory etc. to a particular process (JVM in your case). You could also utilize containers such as Docker to do the same. Maybe you are already aware of this, but I mention for completeness.
you can refer to: tomcat - CATALINA_BASE and CATALINA_HOME variables

Multi-tomcat servers deployed on a single Linux server, does each of tomcats has an independent JVM?

Today I've encountered a problem during my java job interview as below:
Multi-tomcat servers deployed on a single Linux server, does each of tomcats have an independent JVM?
I think there is only one JDK installed on a server, there is only one JVM on server, is it right?
Thanks in advance.
No, one JDK means one copy of a program on disk.
If you run the java program (one component of the JDK) twice, with the first copy remaining in memory while the second copy is also running, you will have two JVMs on the server, both running off the same installed JDK.
As far as your Tomcat goes, it is possible for one Tomcat to host many web applications, or to have one Tomcat per each "user". You have to inspect the installation and configuration, and perhaps observe the environment directly (if possible) to really know what is going on. You cannot just look at the number of installed JDKs.

Java Application Servers and JVM

When you deploy many applications to a java application server, do those applications all run in the same JVM i.e. the JVM that's started when the application server starts up?
Do you have the option to run each of those applications in a separate JVM? If so why would you want to do this?
java application server runs in a single JVM, so every app deployed under java application server instance runs in the same VM as every other application while every app has a different class loader
Go through this questions's answer..hope all queries will be answered :
Why have one JVM per application?
I am afraid you can't run in different JVMs because the appserver have to manage the objects life cycle. That's what JEE is all about. Also, that's why JEE states that you should not use threads in your app, because you want the container to take care of the concurrency for you.
Of course, in a clustered environment, you can have several JVMs, but still be the same for the app server + container.
Yes if the application server is not clustered.
Otherwise it could work on different host machine and jvm.

Running webapps in separate processes

I'd like to run a web container where each webapp runs in its own process (JVM). Incoming requests get forwarded by a proxy webapp running on port 80 to individual webapps, each (webapp) running on its own port in its own JVM.
This will solve three problems:
Webapps using JNI (where the JNI code changes between restarts) cannot be restarted. There is no way to guarantee that the old webapp has been garbage-collected before loading the new webapp, so when the code invokes System.loadLibrary() the JVM throws: java.lang.UnsatisfiedLinkError: Native Library x already loaded in another classloader.
Libraries leak memory every time a webapp is reloaded, eventually forcing a full server restart. Tomcat has made headway in addressing this problem but it will never be completely fixed.
Faster restarts. The mechanism I'm proposing would allow near-instant webapp restarts. We no longer have to wait for the old webapp to finish unloading, which is the slowest part.
I've posted a RFE here and here. I'd like to know what you think.
Does any existing web container do this today?
I'm closing this question because I seem to have run into a dead end: http://tomcat.10.n6.nabble.com/One-process-per-webapp-td2084881.html
As a workaround, I'm manually launching a separate Jetty instance per webapp.
Can't you just deploy one app per container and then use DNS entries and reverse proxies to do the exact same thing? I believe Weblogic has something like this in the form of managed domains.
No, AFAIK, none of them do, probably because Java web containers emphasize following the servlet API - which spins off a thread per http request. What you want would be a fork at the JVM level - and that simply isn't a standard Java idiom.
If I understand correctly you are asking for the standard features for enterprise quality servers such IBM's WebSphere Network Deployment (disclaimer I work for IBM) where you can distribute applications across many JVMs, and those JVMs can in fact be distributed across many physical machines.
I'm not sure that your fundamental premise is correct though. It's not necessary to restart a whole JVM in order to deploy a new version of an application. Many app servers will use a class-loader strategy that allows them to discard a version of an app and load a new one.

Categories

Resources