开发者

Read a Environment Variable in Java with Websphere

开发者 https://www.devze.com 2023-03-26 08:58 出处:网络
I\'ve a little problem with Websphere application server 7.0 (WAS7) and the reading of Environment Varaibles.

I've a little problem with Websphere application server 7.0 (WAS7) and the reading of Environment Varaibles.

With TomCat, I've defined a variable as

<Environment name="myVar" type="java.lang.String" value="myVarOnServeur"

and I read it with a lookup on the initialContext :

Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "java:comp/env/myVar" );

and it works!

But with Websphere, I define a environment variable on the GUI but I can't read it in my java code.开发者_C百科 I've a NamingException.

Read a Environment Variable in Java with Websphere

(source: fullahead.org)

How can I do to fix my problem?


to define inside web.xml

<env-entry>
   <env-entry-name>varName</env-entry-name>
   <env-entry-value>56</env-entry-value>
   <env-entry-type>java.lang.String</env-entry-type>
</env-entry>

to see with java

Context envEntryContext = (Context) new InitialContext().lookup("java:comp/env");
String mydata = (String)envEntryContext.lookup("varName");


You are looking at the wrong place.

You should add the variable in Environment->Naming->Name space bindings->New.

If you choose Binding type String, "Binding identifier" and "Name in namespace..." myVar, you can get variable's value with:

Context ctx = new InitialContext();
String myVar = (String) ctx.lookup( "cell/persistent/myVar" );


Read a Environment Variable in Java with Websphere

On WAS follow the above setting where name is your key and value is your property value. in my example i used Name : Test Value : This is the test value. After setting this values restart your application server. on your Java code call System.getProperty("TEST") where test is the name for your property and the value will show


You can put something like the following in your web.xml file, which should be in your application's WEB-INF directory:

<env-entry>
    <env-entry-name>myVar</env-entry-name>
    <env-entry-type>java.lang.String</env-entry-type>
    <env-entry-value>myVarOnServeur</env-entry-value>
</env-entry>

By the way this is a standard syntax and should work across all the application servers. I'm using it with WebSphere, JBoss and WebLogic. It can be queried exactly as you do in your example.


If what you want is to define and manage your own variables, have a look at Environment->Naming->Name space bindings. You can bind jndi names to String constants there. see String binding settings


You should be able to resolve these via WebSphere's AdminOperations MBean:

//sample code from WAS 7 Infocenter
private String expandVariable(String s) throws
                         javax.management.JMException {  
  com.ibm.websphere.management.AdminService as = 
     com.ibm.websphere.management.AdminServiceFactory.getAdminService();  

  String server = as.getProcessName();
  String mBeanName = "*:*,type=AdminOperations,process=" + server;

  java.util.Set result = as.queryNames(
     new javax.management.ObjectName(mBeanName) , null);  

   return (String) as.invoke((javax.management.ObjectName)
                             result.iterator().next(),
                             "expandVariable",
                             new Object[]{"${"+s+"}"},
                             new String[]{"java.lang.String"});
}

See Creating, editing and deleting WebSphere variables.


Websphere 7.0 - 8.5

Set Variable Admin Console ---> Websphere Application servers -----> Your_sever_name ---> Java and process management ---> Process definition -->Java Virtual Machine --> Custom properties

Get Value in Java System.getProperty("Your_Variable")


I would just like to elaborate on creating a variable in WebSphere that can be used by a Java app, to hopefully help others, as I had to do a bit of additional research to figure this out.

Let's say you want to create a variable in WebSphere named ENV which contains a value of dev (or int, or prod, or any other value).

  1. In the left panel of the WebSphere admin console, select Servers > Server Types > WebSphere application servers.
  2. Select the application server that contains the app.
  3. Expand Java and Process Management and select process definition.
  4. Select Java Virtual Machines.
  5. Select Custom properties.
  6. Select New.
  7. Create the name and value of the variable and select OK.
  8. Select Save.
  9. Restart the application server for this change to take effect.

In this example, a variable named ENV with a vaule of "dev" was created.

Read a Environment Variable in Java with Websphere

Next, the Java app will need to be configured to use the ENV variable in WebSphere. In the below markup, the Java app has a class named "Environment". This class creates a variable named env. System.getProperty("ENV") is the magic that gets the variable from WebSphere. It is noteworthy that this Java code should also work with other application servers, such as JBoss or Tomcat, so you don't need to customize the Java code to a particular platform.

While definitely not required, I also am returning env. I am just doing this for demonstration, so that we can get the variable in a JSP page, so that we can see the variables with our own eyes in a JSP page, for validation that this works as expected.

package com.example.main;

public class Environment {  
    public String env;

    public Environment() {
        env = System.getProperty("ENV");
    } 

    public String getEnvironment(){
        return env;
    }
}

Inside of the tags of a JSP page, I add the following markup to get the env variable from the Environment class, which in turn gets the ENV variable from WebSphere.

<%@page import="com.sample.main.Environment"%>

<%
  Environment foo = new Environment();
  String env = foo.getEnvironment();
  out.print("Environment : " + env;
%>

Now, once the app has been deployed to WebSphere, the environment should be displayed, which is how I know that I was able to successfully get the variable from the application server.

Read a Environment Variable in Java with Websphere


The thread is kind of old but just wanted to provide some info. This is with WebSphere 8.5.5

I tried getting WebSphere variables defined in the console via [Environment > WebSphere variables] using

System.getProperty("Variable");

It did not give the variable to me. I looked around a bit on the web and came across the following: https://www.setgetweb.com/p/WAS855/ae/was2873.html

The following function listed there returns the variables

private static String expandVariable(String s) throws
javax.management.JMException 
{   
    com.ibm.websphere.management.AdminService as = com.ibm.websphere.management.AdminServiceFactory.getAdminService();
    String server = as.getProcessName();
    java.util.Set result = as.queryNames(new javax.management.ObjectName("*:*,"
                     + "type=AdminOperations,process=" + server), null);
    return (String)as.invoke((javax.management.ObjectName) result.iterator().next(),"expandVariable",
            new Object[] {"${"+s+"}"}, new String[] {"java.lang.String"});
}

Then call

expandVariable("Variable");


I don't see anything there that says that those entries can be read via ctx.lookup( "java:comp/env/..." );

0

精彩评论

暂无评论...
验证码 换一张
取 消