2

I am running java batch(jsr352) using liberty server. The datasource configured in server.xml. I would like to load server.xml based on the region(like dev, sit, prod). How can I pass arguments to start liberty server and load the datasource dynamically There could be possiblity with server.env file and bootstrap.properties. since new to this.. can anyone help on this.

Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
user3540722
  • 175
  • 1
  • 2
  • 11

2 Answers2

2

An easy way to do this is to use variables in your server.xml like this:

<dataSource jndiName="jdbc/db2">
    <jdbcDriver libraryRef="DB2JCCLib"/>
    <properties.db2.jcc databaseName="${evn.db2_name}" 
                        serverName="${env.db2_server}" 
                        portNumber="${env.db2_port}"/>
</dataSource>

Then, you could can set the variables in your server.env like this:

db2_name=mydb
db2_server=whatever.com
db2_port=50000

Alternatively, if you use any sort of scripting to start your Liberty servers, you can export them in the bash env like this:

$ export db2_name=mydb
$ etc...
$ wlp/bin/server start myServer
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • The Liberty Batch [Bonus Payout sample](https://github.com/WASdev/sample.batch.bonuspayout) shows another variant of the same basic idea of using variables. In this sample, we use the Liberty Maven plugin and pass the "db.url" property from the command line, through bootstrap.properties. – Scott Kurz Nov 16 '18 at 21:37
2

If you have configuration differences that go beyond attribute values, variables may not be sufficient. For example, suppose you use an in-memory database in dev (like Derby embedded) and a more robust database in production (like DB2).

In your primary server.xml, you can include another config xml file using a variable like this:

<server>
  <include location="dbconfig-${env.ENV_LOCATION}.xml"/>

  <!-- rest of common config here -->
</server>

Then you can have dev-only config in dbconfig-dev.xml like this:

<server>
  <dataSource id="db" jndiName="jdbc/db">
    <jdbcDriver libraryRef="DerbyLib"/>
    <properties.derby.embedded databaseName="memory:testdb" createDatabase="create"/>
  </dataSource>
  <library id="DerbyLib">
    <fileset dir="/path/to/derby.jar"/>
  </library>
</server>

And production-only config in dbconfig-prod.xml like this:

<server>
  <dataSource id="db" jndiName="jdbc/db">
    <jdbcDriver libraryRef="DB2JCCLib"/>
    <properties.db2.jcc databaseName="PRODUCTION_DB" 
                        serverName="serious.business.com" 
                        portNumber="50000"/>
  </dataSource>
  <library id="DB2JCCLib">
    <fileset dir="/path/to/db2.jar"/>
  </library>
</server>

Then, based on which value is set for ENV_LOCATION, either dbconfig-dev.xml or dbconfig-prod.xml will be included in your primary server.xml config.

Scott Kurz
  • 4,985
  • 1
  • 18
  • 40
Andy Guibert
  • 41,446
  • 8
  • 38
  • 61
  • Exactly, this is what i expected. But how do we pass values to the variable ${env.ENV_LOCATION}. Is that using server.env file. Or is there any run time parameter can be passed while starting the liberty server. – user3540722 Nov 27 '18 at 13:51
  • you can run `export ENV_LOCATION=prod` in your shell, then when you subsequently run `server start` it will be picked up as an environment variable – Andy Guibert Nov 27 '18 at 14:34