4

I'm implementing an EJB-based system in JBoss.

One of my message driven beans will be responsible for sending emails. I want the email template to be stored externally (probably as XML) so that it can easily be changed without having to change the code/redeploy the bean, etc.

Where should this file be placed and how do I reference it?

1
  • Do note, that it is not allowed to access arbitrary files from an EJB (see How to access the file system from an EJB 3). From my point of view the answer of @duffymo is the best chioce (store the template in a database). Alternatively you can use JNDI as mentioned in an answer in the linked question.
    – Filou
    Dec 19, 2019 at 8:52

3 Answers 3

2
+100

The JBoss documentation specifies that the environment variable jboss.server.data.dir is the "location available for use by services that want to store content in the file system". See here for more details. You can get the value of the variable by

System.getProperty("jboss.server.data.dir");

And, as shown in the link, the location of the server/[config]/data directory will be returned. Store the template file there when you deploy your app, and instruct your admins to modify it there.

3
  • Just tried this successfully. It seems the best solution for us right now - however I'm a little confused as I thought it was against the EJB spec to be able to access the file system?
    – William
    Aug 6, 2009 at 18:04
  • Also, would this cause a problem if we were to get to a stage where we needed to run multiple JBoss servers in a cluster - i.e. would the same file need to be duplicated on each one? The alternative I'm now considering is storing the info on the JNDI tree, which seems more scaleable.
    – William
    Aug 6, 2009 at 21:46
  • Yes, there would be an issue with accessing the file system in a clustered deployment. In that case, I believe, file access is just too simplistic and another solution would be preferable. If you're running a system that merits a clustered environment, maybe you could consider a configuration service that would hide the details of accessing the template info.
    – Armadillo
    Aug 6, 2009 at 23:07
1

This question and this blog post refer to property files, but what is discussed also applies for other kind of files. One solution would be to place the XML file in a standard location and read it like this (code taken from the link above):

String path = System.getProperty("catalina.base")

                  + System.getProperty("file.seperator")

                  + "YOUR_FILE.properties";

FileInputStream fis = new FileInputStream(path);

Instead of catalina.base you can use user.dir or even define your own environmental variable (mypath_to_xml_file) and read it with System.getProperty.

Another solution would be to use JNDI to define the path to the file.

1

Or put it in a database where you can get at it via SQL client. Load it on startup or maybe poll to check for timestamp changes and reload it.

What kind of template do you mean? Is it a Velocity template that you populate in a mail merge fashion?
One thing that I like about the database design is that you can add so much more information to the schema that's over and above a mere file. You can include timestamp, user ID of the person who updated the template, version, meta data, system name, a boolean to indicate "live" or "dormant", etc. Your EJB can be really smart about which template it chooses. You might find yourself with even more options than you'll have if you simply park files on the server drive. They'll be available to other apps that way as well. Could be a nice design if you run with it a little bit.

If you add it to the WEB-INF/classes directory of an exploded deployment you can easily reference it using "getResourceAsStream()". Add a polling feature if you don't want to have to bounce the server.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.