Define variable in JBOSS application server level - java

I searched for way to define variable in JBOSS application and use it in my web application, but I did not find any tutorial,
so I want to ask, can I define variable in the JBOSS server level?
and use it in multiple application deployed in server?

You can define the server-wide system properties.
For example, to define the system properties in standalone.xml, add them right after the extensions section:
<system-properties>
<property name="my.property.first" value="Some value 1"/>
<property name="my.property.second" value="Some value 2"/>
</system-properties>
Then you can use the properties in any deployed application, for example:
System.getProperty("my.property.first") // Returns "Some value 1"

There are a number of approaches one could take, one of them being creating a custom module and each project that needs access to your variables (properties) will depend on that module. See this reference https://developer.jboss.org/wiki/HowToPutAnExternalFileInTheClasspath
Hope that helps

Related

BridgePropertyPlaceholderConfigurer camel bean

I use the following bean to manage properties in camel as below :
<bean id="ilePropertiesConfigurer"
class="org.apache.camel.spring.spi.BridgePropertyPlaceholderConfigurer">
<property name="properties" ref="allProperties" />
</bean>
allproperties is a java class. it works very well when starting the application.
But now, I want to update properties without restarting my application. I update allproperties but it still takes the old values.
Can you help me?
This is not supported in Apache Camel with that Spring property placeholder bridge. You need to restart your application.
OSGi Blueprint has a concept of allowing to reload/restart your application when properties are changed, but it does a full bundle restart command.

Passing the JNDI name dynamically

I have a lot of Websphere servers with different JNDI name of my DB connection so I have to build many ears files for each server. I'd like to do something like this:
<bean id="dbDataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="${SPECIFIC_JNDI_NAME}"/>
</bean>
How can I create this SPECIFIC_JNDI_NAME variable?
The proper Java EE way to do it, is using resource references in your code like: java:comp/env/jdbc/myDSRef, then this resource reference is bind to actual JNDI name during the installation process.
You either define references via #Resource tag, or entry in the deployment descriptor (web.xml or ejb-jar.xml).
You map it to the JNDI name via admin console, wsadmin installation script, or ibm-web-bnd.xml file placed in the WEB-INF folder.
It is possible to use references with Spring.
This is the wrong way to go about it. One advantage of JNDI is that you can bind objects (in this case a datasource) under one JNDI name without a care for where it came from, how it was instantiated, etc. as long as it was there at the time it was first accessed.
You (or whoever configures the JNDI names) are basically trying to take away that advantage by binding different datasources on different JNDI names.
A workaround could be to bind the 'custom' name to a 'standard' JNDI name such that your application can still refer to the 'standard' name and the onus for providing the right bean is on those who configure the JNDI but really, if you go that far you can also just give the datasource the standard name. Also, I'm not sure that is even possible in JNDI, I just know that it used to be possible in Spring's own configuration.

db.properties vs persistence.xml which one is better?

Recently I started a maven project to build an application for integrating
Spring, JPA, JSF
But in the automatically generated folder structure I can see one file named as
db.properties
and also I have one
persistence.xml
Now my question is that Database connection can be defined in either of these files, Can anybody tell me
1. Which way is better and why ?
2. Why there is db.properties file automatically generated while I already have persistence.xml ?
db.properties file is like messages.properties which is used to define key value pair. And after that we will use keys in expression language. So configurations will only be done in
persistence.xml or dataSource.xml
whichever is preferred choice but the values we will take from db.properties in the form of expression language eg.
driverClassName=com.mysql.jdbc.Driver
this is an entry in your db.properties. and you will use it in persistence.xml as follows.
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${driverClassName}" />
I assume, from the fact that you mention JSF, that you are building a web application for deployment to an application server. I also caveat this question in that I don't know about db.properties or where it comes from.
When deploying to an application server, it is always best to configure your database connections in the container and expose them to the application via JNDI. This allows the container to manage connection pooling and credentials and keeps this information out of your WAR/EAR files. It also ensures that your WAR/EAR files are agnostic to the particular database instance, so can be deployed to a container in any environment without modification.
Therefore, I recommend against configuring your datasource in persistence.xml.
See also Difference between configuring data source in persistence.xml and in spring configuration files which is a similar question- the accepted answer there expresses the solution in more detail.

How can I create global context variables in JBoss?

This is a follow-up to a question I posted a while back: "Can I use a single WAR in multiple environments?". I was able to create a single-war solution in Tomcat, but now we are migrating our app to JBoss 4.2 and I can't figure out how to set up global environment variables.
In Tomcat 6 this was pretty straightforward: I simply put the following snippet in tomcat/conf/Catalina/myappname.xml:
<Context ...>
<Environment name="TARGET_ENV" value="DEV" type="java.lang.String" override="false"/>
</Context>
Then in my app I was able to resolve the environment name with the following:
Context context = (Context) InitialContext().lookup("java:comp/env");
String targetEnvironment = (String) context.lookup("TARGET_ENV");
The problem is that I can't find out where/how to place global variables in JBoss. I've tried putting the <Environment> tag in the following files to no avail:
server/all/deploy/jboss-web.deployer/context.xml
server/default/deploy/jboss-web.deployer/context.xml
I know that I can put environment variables in my app's web.xml but that defeats the purpose of having a unified war - I'd still need custom .war's for dev, qa and prod.
I'm a JBoss newbie so if there's any additional information that would help just let me know and I'll append to this question.
I use somehing similar to PropertiesService for database url, and other environment related things.
Therefore I'm relieved from the burden to provide different environment related atrifacts.

Custom configuration for JBoss applications?

I've built a simple alert monitor to display the health of various applications. This is configured by XML, as each monitor instance needs to show different metrics. An example configuration may be:
<machine>
<monitors>
<check type="connectivity" name="Production Server">
<property key="host" value="ops01.corp" />
<alarm />
</check>
</monitors>
</machine>
Currently, I'm storing this in the root of the C:\ drive of the server. What would be nice is if I could put it in the deploy directory of the JBoss server, and could somehow get a reference to it. Is this possible? I looked at MBeans but it didn't seem to support complex XML structures.
Robert
Try JOPR - http://www.jboss.org/jopr .
For custom metrics, you can write your own plug-in.
You can get an input stream for any file in the classpath by using the ClassLoader#getResourceAsStream(String name) method. Just pass the location of the file relative to the classpath.

Categories

Resources