使用 Websphere 读取 Java 中的环境变量

发布于 2024-11-28 13:23:09 字数 734 浏览 4 评论 0原文

我在使用 Websphere 应用程序服务器 7.0 (WAS7) 和环境变量的读取方面遇到了一些问题。

在 TomCat 中,我定义了一个变量,

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

并通过在initialContext 上查找来读取它:

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

它有效!

但是对于 Websphere,我在 GUI 上定义了一个环境变量,但无法在 java 代码中读取它。我有一个命名异常。

输入图片此处描述
(来源:fullahead.org

我该如何解决我的问题?

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. I've a NamingException.

enter image description here
(source: fullahead.org)

How can I do to fix my problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

初懵 2024-12-05 13:23:09

在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>

用java查看

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

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");
倾城花音 2024-12-05 13:23:09

你看错地方了。

您应该在环境 -> 命名 -> 命名空间绑定 -> 新建中添加变量。

如果您选择绑定类型 String、“绑定标识符”和“命名空间中的名称...”myVar,您可以通过以下方式获取变量的值:

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

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" );
绅士风度i 2024-12-05 13:23:09

在 WebSphere 上遵循此设置

在 WAS 上遵循上述设置,其中 name 是您的键,value 是您的属性值。在我的示例中,我使用名称:测试值:这是测试值。设置此值后重新启动应用程序服务器。在您的 Java 代码中调用 System.getProperty("TEST"),其中 test 是您的属性的名称,该值将显示

On WebSphere follow this settings

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

不疑不惑不回忆 2024-12-05 13:23:09

您可以在 web.xml 文件中放入类似以下内容,该文件应位于应用程序的 WEB-INF 目录中:

<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>

顺便说一下,这是标准语法,应该可以工作跨所有应用程序服务器。我将它与 WebSphere、JBoss 和 WebLogic 一起使用。可以像您在示例中一样进行查询。

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.

燕归巢 2024-12-05 13:23:09

如果您想要定义和管理自己的变量,请查看环境->命名->命名空间绑定。您可以在那里将 jndi 名称绑定到字符串常量。请参阅 字符串绑定设置

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

谜兔 2024-12-05 13:23:09

Websphere 7.0 - 8.5

设置变量
管理控制台---> Websphere 应用程序服务器----->您的服务器名称 ---> Java与进程管理--->进程定义-->Java虚拟机-->自定义属性

在 Java 中获取值
System.getProperty(“你的变量”)

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")

最单纯的乌龟 2024-12-05 13:23:09

您应该能够通过 WebSphere 的 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"});
}

请参阅 创建、编辑和删除 WebSphere 变量

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.

终难愈 2024-12-05 13:23:09

我只想详细说明如何在 WebSphere 中创建可供 Java 应用程序使用的变量,希望能够帮助其他人,因为我必须做一些额外的研究才能弄清楚这一点。

假设您要在 WebSphere 中创建一个名为 ENV 的变量,其中包含值 dev(或 int、或 prod,或任何其他值)。

  1. 在 WebSphere 管理控制台的左侧面板中,选择服务器 >
    服务器类型 > WebSphere 应用程序服务器
  2. 选择包含该应用程序的应用程序服务器。
  3. 展开Java 和流程管理并选择流程定义
  4. 选择Java 虚拟机
  5. 选择自定义属性
  6. 选择新建
  7. 创建变量的名称和值,然后选择确定
  8. 选择保存
  9. 重新启动应用程序服务器以使此更改生效。

在此示例中,创建了一个名为 ENV 且值为“dev”的变量。

输入图片此处描述

接下来,需要将 Java 应用程序配置为使用 WebSphere 中的 ENV 变量。在下面的标记中,Java 应用程序有一个名为“Environment”的类。该类创建一个名为 env 的变量。 System.getProperty("ENV") 是从 WebSphere 获取变量的魔法。值得注意的是,此 Java 代码还应该与其他应用程序服务器一起使用,例如 JBoss 或 Tomcat,因此您不需要针对特定​​平台定制 Java 代码。

虽然绝对不是必需的,但我也会返回 env。我这样做只是为了演示,以便我们可以在JSP页面中获取变量,以便我们可以在JSP页面中亲眼看到变量,以验证这是否按预期工作。

package com.example.main;

public class Environment {  
    public String env;

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

    public String getEnvironment(){
        return env;
    }
}

在 JSP 页面的标记内部,我添加以下标记以从 Environment 类获取 env 变量,该类又从 WebSphere 获取 ENV 变量。

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

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

现在,一旦应用程序部署到 WebSphere,就应该显示环境,这就是我知道我能够成功从应用程序服务器获取变量的方式。

输入图片此处描述

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.

enter image description here

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.

enter image description here

甜尕妞 2024-12-05 13:23:09

该线程有点旧,但只是想提供一些信息。这是在 WebSphere 8.5.5 中,

我尝试通过 [Environment > ] 在控制台中定义 WebSphere 变量。 WebSphere 变量] using

System.getProperty("Variable");

它没有给我变量。我在网上查了一下,发现了以下内容:
https://www.setgetweb.com/p/WAS855/ae/was2873。 html

下面列出的函数返回变量

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"});
}

然后调用

expandVariable("Variable");

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");
冰雪之触 2024-12-05 13:23:09

IBM 官方文档报道, WebSphere 中的环境变量应按如下方式设置:

选择服务器 > (展开服务器类型)> WebSphere应用程序服务器>服务器名称> (展开 Java™ 和进程管理)>流程定义>环境条目> New

然后您可以像常规应用程序参数一样访问该变量。

Spring 用户可以使用 @Value 注解访问该变量:

@Value("${YOUR_ENVIRONMENT_VARIABLE_NAME}") 
private String environmentVariable

As reported by the official IBM documentation, an environment variable in WebSphere should be set this way:

Select Servers > (Expand Server Types) > WebSphere application servers > server_name > (Expand Java™ and Process Management) > Process Definition > Environment Entries > New

Then you can access the variable like a regular application argument.

Spring users can access the variable with the @Value annotation:

@Value("${YOUR_ENVIRONMENT_VARIABLE_NAME}") 
private String environmentVariable
乱世争霸 2024-12-05 13:23:09

我没有看到任何内容表明这些条目可以通过 ctx.lookup( "java:comp/env/..." ); 读取

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文