How to restart tomcat application when a file change? - java

I have an application represented by an exploded .war:
/webapps/myWebapp
Usually, when the .war is present, I do the following via command line:
touch /webapps/myWebapp.war
This changes the last modification date on the war, making the tomcat redeploy the application inside of its folder: the old folder is deleted and a new one is created again.
Unfortunately I don't have the .war, but only the application folder and, sometimes, I need to change some files inside of it and a restart is requested in order for the changes to take effect.
Is there a similar way to accomplish the same without restarting the whole tomcat server?
Alternatively I could do that via the manager console...the problem is that I want my user to be able to only restart that application, not every single application deployed.
Can you give me some hints/suggestions? Thanks

You may want to look at the property WatchedResource
http://tomcat.apache.org/tomcat-6.0-doc/config/context.html
WatchedResource - The auto deployer will monitor the specified static resource of the web application for updates, and will reload the web application if is is updated. The content of this element must be a string.
Just add paths of the files that you want to see the changes.

Related

modify files in WEB-INF folder - tomcat as hot deployment

I have a application deployed in Tomcat7 server which deals with multiple database connections.
These database details are retrieved from 'database.properties' and it is referred in hibernate.xml file in WEB-INF folder. These database details are to be changed dynamically and the hibernate xml is also to be updated.
As for now, if I want to update the database details I need to modify the changes in the 2 files mentioned above and the war file needs to be deployed again.
I tried changing the files in tomcat without restarting. I verified that the 'reloadable' property in tomcat server.xml file is set to true. But still the changes are not reflected in the app without restarting the server.
Please let me know if any configurations to be done for modifying these files without restarting the tomcat server.....
--
Suriya
Look at WatchedResource tag. You need to add your files there.
WatchedResource - The auto deployer will monitor the specified static resource of the web application for updates, and will reload the web application if it is updated. The content of this element must be a string.
Look at the configuration in context.xml file
<!-- Default set of monitored resources -->
<WatchedResource>WEB-INF/web.xml</WatchedResource>
Add more files, which you want to monitor the changes to redeploy.

Editing Folder content in tomcat manually?

I have a WAR file called ROOT.war where I uploaded into Tomcat few days back. There I have few "public" HTML pages like index.html, about.html etc. These pages has no connection with the programatic part of the application, instead containing a link for login.jsp.
Now, I need to change the content of the index.html and about.html. What I was used to do is rebuild the entire WAR file and re-uplaod it, but now can't do it for small and iterative changes like this because the application is in use.
So, can I simply access the webapps -> ROOT folder in Tomcat and replace the index.html? Will it affect the process of the application?
It works but avoid such practices which lead to discrepancies.
Follow standard deployment process - rebuild the entire WAR file and re-upload it during deployment window.
You can, and It will work. You can even change the JSP code. The only issue you may have is that when you redeploy a new version of the war, changes made in the server will ve overriden by the new version, so you better change it in the original tool also.

Wildfly hot deploy of Static content (Javascripts, HTML, CSS) without restarting the server in Spring STS

I'm using Wildfly 9.0.0.Alpha1 with Spring STS 3.6.3 and JBoss Tools, i don't find the way for update my changes to static content inside my server without the need of use incremental deploy (I enabled exploded deployment in the wildfly server), so when i do a little change to my resources, I have to refresh the project in eclipse STS so then it makes an incremental deployment, this takes about 15 - 30 seconds which is annoying and a waste of time
There is a way for update a static content in a exploded war without incremental deploy?
You can edit your subsystem entry for undertow, adding a location and file for a path within standalone/tmp. And then you can add symbolic links to that path at runtime every time you want to share additional static content. You can use Files.createSymbolicLink(...) in Java. Just be careful that the attributes of your symbolic links let them be deleted without deleting the targets. Also, make a start script that recreates your subdirectory in tmp as needed before wildly starts. If you have an undertow location that points to a folder that doesn't exist, the location (context, like /static) will not be created. Once created, undertow will serve up any subfolder or file you add to it once location is created at startup. Make sure you add directory-browsing="false" attribute to the file element for the folder if you don't want people to be able to list contents of folder.

Locating created file with Java EE and Tomcat

I have created a dynamic web project, and use Apache Tomcat as a server.
In my servlet I'm creating a text file and want to reuse that in a JSP. However they are by default created in the installation folder of Eclipse when I do something as simple as the following:
File f = new file("test.txt").
I don't know why this happens. Is there a way to create the file in the WebContent directory as I want to make that file available for download in my JSP.
Java has a concept of the "current directory". When you start an application via Eclipse, this may indeed point to your installation directory. If you don't specify any path, a file will be created in this current directory. Hence the reason why your test.txt ends up there.
The WebContent directory is a something that is specific to Eclipse. Your code should not depend on putting anything there. You only start your application via Eclipse when you're developing it, not when you're deploying it to a live server.
The content of this directory will become the root of your .war, which is a well known location independent of how you start & deploy you app, BUT you still cannot depend on writing anything to this location at run-time. You might deploy your application as a packaged .war (likely for live deployments) or you may deploy your application unpackaged but then your application server may simply not pick up any changes done at run-time.
What you can do if you are sure your application only runs on a single server is writing the files to a well known location on your file system, such as /tmp, or /var/yourapp/files, etc. The code serving up those files can then pick them up from that location.
If you want to play it 100% safe according to the Java EE rules, you'd store your files on something like an FTP server that has a configurable address. Technically your war could be shipped between nodes on a cluster and requests could end up going to different machines, so depending on a local filesystem wouldn't work then.
Executing this statement this.getServletContext().getRealPath (""), you'll obtain the path where Tomcat WebServer is pointing at at runtime. You could add a folder "MyFolder" and call this statement:
new File(this.getServletContext().getRealPath ("") + "/MyFolder/test.txt");
Anyway, the default path looks something like:
...\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\<NameOfYourProject>
Note that when you create a new file, it won't appear in your immediate workspace (check the .metadata path), unless you change the runtime location tomcat should point at.

Tomcat and Eclipse Zero Turnaround Deployment

I want to be able to deploy code changes to Tomcat (near instantly), while I'm developing in Eclipse.
So far, I have my output from Eclipse placing the built classes in the WEB-INF/classes folder of my web application.
I also have a reloadable context, with the web.xml as a watched resource. Any edit / save to this file does reload my web app, taking just over one second - much quicker than building a new war file and deploying it in full.
However, what I'd like to do is trigger the redeploy when I edit any source file. As the .class files are being modified in Tomcat, it seems I just need to monitor any changes in the WEB-INF/classes folder and it's children.
I've read that I can add additional watched resources in Tomcat's context.xml but this doesn't seem to be quite what I need - unless it's possible to specify a directory that will be watched (including recursively monitoring sub folders and files)?
<Context>
<WatchedResource>WEB-INF/web.xml</WatchedResource>
<WatchedResource>WEB-INF/someother.file</WatchedResource>
<Manager pathname=""/>
</Context>
So essentially, my question is can I watch the entire classes folder (without including each WatchedResource explicitly) to trigger a redeploy in Tomcat?
If not, can I configure Eclipse to touch the web.xml file, whenever I save a source file in that project? I'm developing on a Windows system. :(
PS I'm not interested in the JRebel product. Any answer should be a free solution.
Update
According to the Tomcat documentation, the classes folder should be monitored by setting the context to reloadable:
Set to true if you want Catalina to
monitor classes in /WEB-INF/classes/
and /WEB-INF/lib for changes, and
automatically reload the web
application if a change is detected.
Only changes to the web.xml seem to trigger a reload. Is this a bug or is my setup incorrect?
Also, I've read about setting the docBase attribute for a given context:
docBase="webapps/someExample"
This appears to be close to what I need, as I could then republish in Eclipse quickly. My only problem is that I require several web apps / servlets to be running in Tomcat concurrently, on the same port etc.
For these cases I set the eclipse build output to WEB-INF/classes as you have done and create a context file with the docBase set to the webapp folder (parent of WEB-INF) in the project. This is manually placed in conf/Catalina/localhost (assuming default configs for some elements of server.xml). End result is tomcat ends up serving from the development directory. So, change a servlet or other class and it is updated to the classes folder. Save a jsp and it is immediately available.
If project structured like:
src
|-main
|-webapp
|-images
|-js
|-WEB-INF
|-jsp
|-classes
Then context would be like:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/path" reloadable="true"
docBase="<pathtoproject>/src/main/webapp" />
Maybe the Web Tools Project of Eclipse with auto-redeploy enabled will help you? Add a server, open properties and under Publishing you will see a radiobutton saying "Automatically publish when resources changes". This will result in a redeploy if classes changes otherwise just overwrites resources. You can install WTP via a built in update site (Eclipse only), so check out your software updates. It is free for most servers but it does not support certain Websphere features?
Try the Spring Loaded JVM agent I've described in the following answer:
https://stackoverflow.com/a/37064672/1034436
While that has worked for my Spring web application, this should work with vanilla Eclipse + WTP + Tomcat + Dynamic Web Applications since Spring Loaded works on the JVM/classloading level.
You will still need to use the "Automatically publish when resources changes" as mentioned by #toomasr in his answer. However, you must also disable "Module auto reload by default" option as well. If you already added/published modules from Eclipse to Tomcat, then disable "Auto Reload" for each web module (via the Tomcat config page's Modules tab). That should prevent Tomcat from reloading the application when a single class file is updated, which I suspect is what all that reload/wait time is.

Categories