Java - Tomcat: Reload context.xml without restarting server - java

I'm using spring mvc and tomcat as a server. I want to be able to change a jndi field that is Autowired(as String):
<jee:jndi-lookup id="someMessage" jndi-name="someMessage"/>
in one of the my services, that is referenced to conf/context.xml of Tomcat, that looks something like this:
<Environment name="someMessage" value="Change this." type="java.lang.String" />.
However, when I change the value on context.xml, this change is not reflected on my service managed by spring, unless I restart server. Is there anyway to reflect this change without restarting or redeploying war? I know there is a solution to include such a dynamic field in one of properties file and then use commons configuration library to reload the changes, but I'm looking for a solution to keep this field on my conf/context.xml...

I think that is not possible. Why don't use a property file or a static class?

As far as I know, it's impossible if you put it into the conf/context.xml of your tomcat home as the following doc shows:
StandardContext.html#reload(): If the context.xml has changed, you should stop this Context and create (and start) a new Context instance instead. -- i.e. can't achieve by original context
Reload Existing App: Reload an existing web application, to reflect changes in the contents of /WEB-INF/classes or /WEB-INF/lib. -- i.e. not reflect the change of context.xml
But you can define your app's /META-INF/context.xml, which will be packed into war and you can replace war without restart server.

Related

Configure specific SFSB stateful-timeout in standalone.xml (JBOSS)

For my JEE 3.1 application I have a SFSB for which I have set a specific timeout. I need to have easy access to this value to allow it to be easily configurable.
First I had the annotation in my SFSB class:
#StatefulTimeout(value = 2, unit = TimeUnit.HOURS)
This works as expected. But I need this value to be (easily) configurable. Therefore I removed the annotation and placed the following xml in my ejb-jar.xml. Again this works as expected.
<session>
<ejb-name>MyStatefulSessionBeanName</ejb-name>
<ejb-class>foo.bar.MyStatefulSessionBean</ejb-class>
<session-type>Stateful</session-type>
<stateful-timeout>
<timeout>2</timeout>
<unit>Hours</unit>
</stateful-timeout>
</session>
But the ejb-jar.xml is packaged in a .jar which is within my .ear application deployment. Therefore my question is, can this setting be placed in the standalone.xml file of jboss?
There is no easy way to do this without an application restart.
If that is OK then you can define a system property in your standalone.xml. I recommend using the CLI or the admin console for this purpose.
You can then use property values in your ejb-jar.xml:
<stateful-timeout>
<timeout>${my.session.timeout.value}</timeout>
<unit>${my.session.timeout.units}</unit>
</stateful-timeout>
The property values can then be updated via the web console or CLI and your application restarted.

off-the-shelf web application working directory

I've just started working with Spring/java web. I'm wondering how to accomplish following scenario:
Let's say that I'm creating an application which supports file upload, uses a database connection and maybe a web service. This is an off-the-shelf system, so all the settings are customer specific and should be configured by customer's IT people on the deployment time.
More general in the web.xml file I would like to point the application working directory containing uploaded files, license key file, configuration files, other customer specific resources and maybe even fragments of spring context.
<context-param>
<param-name>workdir</param-name>
<param-value>/var/r2/</param-value>
</context-param>
In my application I would like use the workdir value in order to include configuration files ...
<import resource="wordir_param_value/settings.properties" />
context config fragments
<import resource="wordir_param_value/security.xml"/>
And how may I later use these values in the java code? What is "the best" approach in case like this anyway (off-the-shelf application config)?
Best Regards,
Alek
You can use Spring's property support for that. This allowed two different approaches:
Having a property file outside the application (at a fixed location), the admin can edit it, and the application loads it
In a Tomcat you can write properties in the application specific context.XML file
In the code use #value annotation to inject the properties in a variable.
In the spring XML file you use it with ${name}
Of course you need to configure the proprtyPlaceholderConfigurer

How to use in Tomcat a datasource without having to edit context.xml?

I have to deploy my web app to a shared server where I can't edit the context.xml file to set a new resource factory. Is there a way I could place ALL my hibernate datasource configurations in a file outside my .war, but still inside the deployment folder (the one I have access to).
My ultimate goal is to develop a "portable" web app, so I can easily migrate from one server to another, just editing manually my database configurations in a datasource file. Does anyone knows a functional way to do it?
Ps. I imagine Spring would help with this matter. But I don't feel like to set up it for a simple web app or just for this purpose.
According to http://tomcat.apache.org/tomcat-6.0-doc/jndi-resources-howto.html#context.xml_configuration you can have a per-application context file as well, however that must of course be deployed inside your war file. Your best bet might be to modify your build file so that it uses the correct context.xml when creating the war.

Short url or alias for deployed application in tomcat 6

I have a web application project which is deployed in tomcat 6.
I can access my application using the url:
http://localhost:8082/MyApplication
I also wan't to be able to access this application by another url like:
http://localhost:8082/myapp
Is this possible ? if yes what alternatives do i have ?
Off course, I don't want to change the original name of the application('MyApplication').
Thanks,
Abhishek.
If you add the Context within server.xml it will work as you want. Give the path attribute you wish.
<Context docBase="MyApplication" path="/myapp" />
Though it works, this approach is not recommended by the Tomcat docs, since any changes to server.xml means restarting the server disturbing all the web apps.
But, on the flip side, the practice of keeping this in Catalina_Home/conf/Catalina/localhost/context.xml (which is recommended by the docs) has some unreliabilities as others have reported - when you redeploy the war you can lose the context.xml too
See Why-does-tomcat-replace-context-xml-on-redeploy and
Why does tomcat like deleting my context.xml file?

JBoss automatic redeploying

As I understand it, JBoss* monitors a variety of file types in /deploy and performs certain actions when the file changes. For example, JBoss will redeploy an EAR when its last-modified time changes.
Therefore, I could use some really nasty code to make an EAR redeploy itself, like this:
URL url = this.getClass().getClassLoader().getResource("../RavenWeb.ear");
String path = url.getPath();
File ear = new File(path);
ear.setLastModified(System.currentTimeMillis());
But what I really want to do is just have JBoss redeploy the webapp when an external config file changes. Say the config file lives at C:/foo/bar.properties.
Is there an MBean or some other way getting this done that won't get me mauled by velociraptors?
*I'm using JBoss 5.1.0, if it matters.
I think your best shot is having a MBean that will reload your config file whenever you call a function on it. If you wan't this to happen automatically, you can also consider having a MBean handling the configuration file for you. This way, you can just update MBean properties instead of changing a configuration file.
I've copied the Log4JService implementation and put it in a .sar.
It just simply polls the configuration file, parse it and put a Configuration object in the JNDI, so that I can retrieve it in my application. In this way you don't have to redeploy the entire application, and you can use the new configuration in the app.
If you have to do something else than simply reloading the app, you can do it in this service.

Categories