mercredi 7 décembre 2016

Dynamically setting resources of a Java web app

Specifically, I'm talking about a GlassFish server and a MySQL database.

I am creating the JDBC data source for my application via asadmin on deployment. Since I want to be able to run multiple instances of my application at the same time, I want the data source (and also authentication realm) identifiers to be variable and configurable using Docker environment variables.

Basically, I want each application instance to have its own database, but at the same time I want all instances to share the same application and MySQL server.

So I need to be able configure entries in the persistence.xml and web.xml at deploy time (specifically the jta-data-source of my persistence unit and realm-name of my login config).

This sounds like an easy enough task but turns out to be incredibly difficult. There are a few solutions that I found but are very hacky in my opinion:

  • Modify the deployment descriptors using Maven and build a different .war per instance. Obviously I don't want to build a new application per instance if they all share the same code.
  • Modify the deployment descriptors after deployment in the GlassFish domain file system. This would require a server restart and two deployments for each instance.
  • Make all instances share the same database. Is this a common practice? Seems like unnecessary overhead.
  • Use system properties and ${reference} them in the descriptors. I simply cannot find a proper way to set application-scoped system properties using GlassFish.

So how would I go about this?

On another note: when modifying the authentication realm at runtime, I always need to restart the server, correct? Is there any way around it? Are there any better ways to approach this general use case (answer is probably yes)?

Aucun commentaire:

Enregistrer un commentaire