I was trying to understand how the Web Services work and I came across this tutorial.
Now, I've seen Spring being used in enterprise applications and always wondered where the main method was and how everything worked? And whenever I would go to a Spring tutorial they'll start with beanFactory and Contexts and what not, all in a main Java method and from there just keep getting beans as required. This is totally different from what I see in the applications.
How exactly does Spring work in this case? What is the sequence of calls? I guess there will be some hidden main method somewhere, but I am not sure of that.
Normally if I were to run a simple Java project from command line, I'd do java mainClass. Now how would it happen in this case?
There is still a main method. It's just not written by the developer of the application, but by the developer of the container.
You can still see the main method being called by using the debugger like this:
Put a breakpoint in some initialization method, such as the init method of some servlet Servlet.init()
When the breakpoint hits, scroll down the call trace and the main method should be at the bottom.
This is an example with Jetty:
To see this we need to put the breakpoint in an initialization method so that we get the main thread of the application.
Putting the breakpoint in the processing of a request instead of an initialization method would show Thread.run() at the bottom of the stack trace and not main().
Thread.run() is the equivalent of the main method for threads other than the main thread.
So the main method still exists. It's just being handled at the level of the container.
Web applications don't have a main; the 'program' that is running is actually the web container (Apache Tomcat, Glassfish, JBoss, Weblogic, whatever) and that program will service the web application(s) you deploy into it. You might want to read the JEE tutorial to learn and understand what a Java web environment is.
https://docs.oracle.com/javaee/7/tutorial/
You don't see any explicit main method just because it is a Web project. This project is built into a web application archive (WAR) file which is deployed into a web server / servlet container, e.g., Tomcat in this tutorial.
Web applications do not have to contain main methods. This is because you don't need to explicitly start any Java process from within your web application. Somewhere in its depths, Tomcat calls a main method of the code it has been built from. This happens at server startup time.
Then, it will bind your code to incoming HTTP calls, but it will not start new processes for that. It will rather start new threads.
Web applications are not stand-alone applications. They run on some applications what we call a servletContainer in a Java context so there aren't any "main method or Java process (OS)" for any web application. They are just deployed on those containers that have a main method and Java process in OS runtime.
If you've created a basic program in Java then you must know that every Java program has a main() method, which is the starting point of the program. So, how come servlets don't have a main()? That is because servlets are served using Web containers.
A Web container will perform all the underlying work on behalf of the servlet so the programmer can focus on the business logic. When a client requests a servlet, the server hands requests to a Web container where the servlet is deployed.
Related
I was trying to understand how the Web Services work and I came across this tutorial.
Now, I've seen Spring being used in enterprise applications and always wondered where the main method was and how everything worked? And whenever I would go to a Spring tutorial they'll start with beanFactory and Contexts and what not, all in a main Java method and from there just keep getting beans as required. This is totally different from what I see in the applications.
How exactly does Spring work in this case? What is the sequence of calls? I guess there will be some hidden main method somewhere, but I am not sure of that.
Normally if I were to run a simple Java project from command line, I'd do java mainClass. Now how would it happen in this case?
There is still a main method. It's just not written by the developer of the application, but by the developer of the container.
You can still see the main method being called by using the debugger like this:
Put a breakpoint in some initialization method, such as the init method of some servlet Servlet.init()
When the breakpoint hits, scroll down the call trace and the main method should be at the bottom.
This is an example with Jetty:
To see this we need to put the breakpoint in an initialization method so that we get the main thread of the application.
Putting the breakpoint in the processing of a request instead of an initialization method would show Thread.run() at the bottom of the stack trace and not main().
Thread.run() is the equivalent of the main method for threads other than the main thread.
So the main method still exists. It's just being handled at the level of the container.
Web applications don't have a main; the 'program' that is running is actually the web container (Apache Tomcat, Glassfish, JBoss, Weblogic, whatever) and that program will service the web application(s) you deploy into it. You might want to read the JEE tutorial to learn and understand what a Java web environment is.
https://docs.oracle.com/javaee/7/tutorial/
You don't see any explicit main method just because it is a Web project. This project is built into a web application archive (WAR) file which is deployed into a web server / servlet container, e.g., Tomcat in this tutorial.
Web applications do not have to contain main methods. This is because you don't need to explicitly start any Java process from within your web application. Somewhere in its depths, Tomcat calls a main method of the code it has been built from. This happens at server startup time.
Then, it will bind your code to incoming HTTP calls, but it will not start new processes for that. It will rather start new threads.
Web applications are not stand-alone applications. They run on some applications what we call a servletContainer in a Java context so there aren't any "main method or Java process (OS)" for any web application. They are just deployed on those containers that have a main method and Java process in OS runtime.
If you've created a basic program in Java then you must know that every Java program has a main() method, which is the starting point of the program. So, how come servlets don't have a main()? That is because servlets are served using Web containers.
A Web container will perform all the underlying work on behalf of the servlet so the programmer can focus on the business logic. When a client requests a servlet, the server hands requests to a Web container where the servlet is deployed.
I want my java web application run a program automatically when i start or deploy the program in glassfish server. My web application need to run a mail program inside glassfish when just deployed.
You can set up a servlet to run on application startup, or possibly use the #Singleton annotation, depending on whether you want EJB involved. Or if you're using Spring, there are more ways. You could give some more background for better answers.
What does the servlet <load-on-startup> value signify
http://java.sun.com/developer/technicalArticles/JavaEE/JavaEE6Overview_Part3.html#singles
You can just set some flag type attribute in your database and change the value after run the program.
I'm looking for a way to properly shutdown (Undeploy) an Java Web Application within the application itself. I've tried System.exit(), but this not only shutdown the webApp but also messes up Glassfish.
I know about the contextlistener, I'm just wondering how to start the shutdown procedure.
Deploying of web applications is the responsibility of application server. I do know how is it in Glassfish but other app. servers (e.g. JBoss, Tomcat etc) have web based management application that allow to do this.
If you want to undeploy application programmatically from the application itself you can use JMX. Refer to the glassfish JMX implementation to know which bean to call. But IMHO I do not think that you really need this.
The usual approach is to use the asadmin undeploy command.
The general form for the command is:
as-install/bin/asadmin undeploy war-name
For war-name, use the literal hello, not the full hello.war name.
For the hello.war example, the command is:
as-install/bin/asadmin undeploy hello
See the following references:
http://docs.oracle.com/cd/E19798-01/821-1757/geyvr/index.html
http://docs.oracle.com/docs/cd/E19798-01/821-1758/undeploy-1/index.html
To undeploy programmatically, you can use JSR-88 though i cant really see a reason as to why you would want to do this. See
http://blogs.oracle.com/japod/entry/using_jsr_88_for_web
http://www.jcp.org/en/jsr/detail?id=88
I want to initialize a global instance of a class before my Tomcat server completes startup and begins to offer my servlets. If this service somehow fails initialization, I'd like the entire startup sequence to fail as well (or as close to this as possible; it would be pointless for the server to be running otherwise). What's the best way I can accomplish this?
Each web application has a ServletContext associated with it. The ServletContext object is created when the application is started and is destroyed when the application is shut down. A ServletContext has a global scope and is similar to a global variable in an application.
http://www.javabeat.net/tips/178-servletcontextlistener-example.html
complete explanation here
http://onjava.com/pub/a/onjava/2001/04/12/listeners.html
One thing you can do portably is to implement a servlet that initializes everything you need in its init() method (and maybe call System.exit() if it fails, i do not know if you have permission to do this in Tomcat). Then you would load it using <load-on-startup> in your web.xml to specify initialization order.
I have two web applications running in same Servlet container (Tomcat), A and B. These two connect to each other using Spring Remoting. On startup, B needs to call A to open a connection automatically, it's basically a really simple authentication call.
However since B is (in my case) loaded before A, B's application context blocks until the entire application is started up. What this means is that the B application will be stuck until timeouts etc. occur and only then A is allowed to start, however at this point B is now incapable of connecting to A and the required connection between two web applications won't be created.
So, how do I work around this? I'm currently hooking the connection command using InitializingBean and the application context is initialized using a listener.
Two options come to my mind:
Run two instances of Tomcat, on different ports.
perform the authentication in a new thread (preferably using an ExecutorService). Thus the "main" thread won't block and the deployment will continue.