views:

104

answers:

1

As part of a Java EE application I'm developing on JBoss, I need to persist the contents of a file on the JNDI tree. I'm doing this by reading the contents of the file into a by byte array, then binding it to JNDI as follows.

getInitialContext().rebind("customers_datafile", byteArray);

This works fine, but the binding is lost when the JBoss server is stopped/restarted. Is there a way to make it perist between restarts? Ideally I'd like to achieve this without having to rebind it using code similar to above upon each restart.

+1  A: 

Use a factory instead: Instead of loading the file into a byteArray, create a class that does this and tell JBoss to create an instance. This way, you can pass the filename in the config and load it when JBoss runs new.

Then, you can go to the initial context and ask for the class and invoke a method on it. In your case, the method would return the byte array.

See the docs for details.

[EDIT] So your real question is how to make a file available to all nodes of a JBoss cluster. JBoss isn't going to copy the instance around when you create more cluster nodes. All it does is sending a copy of the XML config to the nodes. So really your only option is to encode the file with, say, Base64 and put it in the XML config as a value.

If you don't want that, you must find a way to put the file in a place where all nodes can "see" it. The most simple solution: Upload the file to a HTTP server and put the URL into the config. Java can easily read data from an URL, so that would distribute the file to all nodes.

Aaron Digulla
My problem with this is that such a class would need to refer to the filesystem - which I'm guessing may cause problems if we end up going down the route of clustered JBoss servers (i.e. which filesystem would it refer to). That's why ideally I'd like to just deploy it using my utility (outside of JBoss), then have it persist on JNDI.
William
How is JBoss going to distribute your byteArray in the cluster?
Aaron Digulla
Well I presume that the JNDI tree would be shared amongst the cluster? Admittedly my knowledge on this subject is rather limited, so more than happy to be corrected if anything I'm saying doesn't make sense :)
William
Yes, probably. But as you already noticed, it's hard to put a byte array in the JNDI tree in such a way that it survives a JBoss restart.
Aaron Digulla
Thanks a lot for your input, much appreciated. Looks like one of the options you suggest above is going to be a better solution.
William