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.
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 know this is a weird question but is it possible to somehow run a JAR with a main class on Tomcat like a WAR?
I'm aware that won't automatically give it any website capability.
I just want it to execute it inside the web server JVM with the isolation that it gives
I would like Flyway to run whenever I deploy a new war to my server.
Does flyway automatically get run when a server is deployed? Do I have to always automate a script which would then the flyway migration command? Or what is the best way to do this?
Server:
The server is a Java Tomcat Server running on Elastic Beanstalk (AWS) that is connected to a MySQL database.
Deployment Process
We run our sql migration scripts on the database manually. Then we upload a new war of the server to Elastic Beanstalk.
This can be useful:
Auto-migration on startup : https://flywaydb.org/documentation/api/
So for Java all it takes is to create scripts (eg. V1__initial_schema.sql, ...), put them under /src/main/resources/db/migration/
and then:
Flyway flyway = new Flyway();
flyway.setDataSource(...);
flyway.migrate();
As the comments said, there may be multiple ways to do this.
ServletContextListener
One common way is to use the hook defined by the Java Servlet spec for being notified when your web app is launching and shutting-down. That hook is the ServletContextListener interface. Add a class to your project implementing the two methods in this interface, one for launch and one for shutdown. In the launch method, run your Flyway code.
The word “context” is the technical term meaning your web app.
contextInitializedYour web app is launching. No incoming web request has yet been handled, and will not be handled until your implementation of this method completes. Run your Flyway migrations here.
contextDestroyedYour web app is shutting down. The last remaining web request has been serviced, and no more will be accepted.
Annotating this class with #WebListener is the easiest of multiple ways to get your Servlet container to register an instance.
Pretty easy.
Your ServletContextListener is guaranteed to be called and run to completion before the first execution of any Servlet (or Filter) in your web app. So this is the perfect place to do setup work that you want finished before your servlets go to work. Flyway seems like a natural fit to me.
Search Stack Overflow for “ServletContextListener” to learn more and see examples, such as my own Question & Answer.
Handling failure
Be aware that stopping a web app’s deployment when something goes wrong (when your ServletContextListener encounters an Exception) is not well-defined in the Servlet spec.
An example might be your Flyway migrations failing for some reason, such as not able to connect to database. At that point you might want to halt deployment of your web app.
See my own Question and Answer and the group of related questions I list in that answer. Tomcat 8.0.33 halts the deployment, and un-deploys the web app, but unfortunately does not report the offending Exception (or at least I could not find any such report in the logs nor in the IDE console while in development mode). The behavior of other Servlet containers may vary.
This is a weird situation. I run a main application in an application server(say WAS) and a sub application in another server (say JBoss). Now , the sub application needs to use the configuration made in the main application server(for example , sub application needs to use the ObjectPoolManager configuration made in WAS from JBoss). Is it possible?
No. You can of course write some kind of adapter, which reads the WAS configuration and translates it to the JBoss format and sets the JBoss configuration. However, i think the cost-benefit-factor for such a development is very very bad.
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