Java 如何在servlet中获取WebSphereConfigService?
我在WebSphere application Server 7上的企业应用程序中有一个servlet。Java 如何在servlet中获取WebSphereConfigService?,java,servlets,websphere,Java,Servlets,Websphere,我在WebSphere application Server 7上的企业应用程序中有一个servlet。 我希望servlet从自定义资源环境提供程序读取配置参数 这个特定的环境不是WebSpherePortal,我也没有使用Spring,但我正在尝试修改本页中关于在Spring中使用资源环境提供程序的代码: 我的测试方法的第一行有问题: com.ibm.websphere.management.configservice.ConfigService service =com.ibm.
我希望servlet从自定义资源环境提供程序读取配置参数 这个特定的环境不是WebSpherePortal,我也没有使用Spring,但我正在尝试修改本页中关于在Spring中使用资源环境提供程序的代码:
我的测试方法的第一行有问题:
com.ibm.websphere.management.configservice.ConfigService service
=com.ibm.websphere.management.configservice.ConfigServiceFactory.getConfigService();
此行中的getConfigService()
方法在我的servlet中总是返回null
。该方法不会抛出任何异常,服务器日志中也不会出现错误;它只返回null
(注意:为了清晰起见,我在上述代码中显示了包名。在实际代码中,我导入相关类。) 如何在servlet中获取
ConfigService
对象
ConfigServiceFactory
类还有一个createConfigService(boolean enable,java.util.Properties props)
方法,但是Javadoc没有说明其参数的预期值,我也找不到任何使用它的示例
编辑:
我曾尝试按照Magic Wand的建议使用ConfigServiceProxy
,但也无法实现。问题的细节如下。有人知道怎么做吗 页面底部关于使用
ConfigServiceProxy
的第5点说:
修改属性connectProps.setProperty(AdminClient.CONNECTOR_HOST,“localhost”);和connectProps.setProperty(AdminClient.CONNECTOR_PORT,“8880”),如果需要
但它没有说明如何确定正确的主机和端口,我在别处也没有找到这些信息,所以我只是猜测
为了查找主机名和端口,我登录到WebSphere Integrated Solutions控制台,转到服务器->服务器类型->WebSphere应用程序服务器,单击我的servlet运行的应用程序服务器,然后单击“端口”
下面是我尝试获取ConfigServiceProxy
的代码部分,其中我仅根据尝试的端口更改主机名和端口号:
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST,"localhost");
connectProps.setProperty(AdminClient.CONNECTOR_PORT,"9634");
AdminClient adminClient=AdminClientFactory.createAdminClient(connectProps);
ConfigService service=new ConfigServiceProxy(adminClient);
首先,我尝试了唯一一个主机为“localhost”的端口,因为链接示例使用的就是这个端口。其端口名为“IPC\U连接器\U地址”,端口号为9634。这会在调用
AdminClientFactory.createAdminClient
时产生异常。堆栈跟踪从以下内容开始:
com.ibm.websphere.management.exception.ConnectorException: ADMC0016E: The system cannot create a SOAP connector to connect to host localhost at port 9634.
at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:634)
at com.ibm.websphere.management.AdminClientFactory.access$000(AdminClientFactory.java:126)
at com.ibm.websphere.management.AdminClientFactory$1.run(AdminClientFactory.java:209)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:63)
at com.ibm.websphere.management.AdminClientFactory.createAdminClient(AdminClientFactory.java:205)
at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:113)
com.ibm.websphere.management.exception.ConnectorException: ADMC0016E: The system cannot create a SOAP connector to connect to host server.company.net.au at port 9062.
at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:634)
at com.ibm.websphere.management.AdminClientFactory.access$000(AdminClientFactory.java:126)
at com.ibm.websphere.management.AdminClientFactory$1.run(AdminClientFactory.java:209)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:63)
at com.ibm.websphere.management.AdminClientFactory.createAdminClient(AdminClientFactory.java:205)
at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:113)
这在尝试构造ConfigServiceProxy
时产生了异常。堆栈跟踪从以下行开始,没有原因:
javax.management.InstanceNotFoundException: WebSphere:process=InfraCluster_server1,type=ConfigService,*
at com.ibm.websphere.management.configservice.ConfigServiceProxy.(ConfigServiceProxy.java:67)
at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.getConfigService(ResourceEnvironmentProviderPlaceHolderConfigurer.java:114)
at com.isw.ResourceEnvironmentProviderPlaceHolderConfigurer.loadEnvironmentProviderProperties(ResourceEnvironmentProviderPlaceHolderConfigurer.java:205)
at com.isw.insight.client.REPTest.doGet(REPTest.java:50)
嵌套的原因是:
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:456)
... 38 more
Caused by: com.ibm.websphere.management.exception.ConnectorNotAvailableException: [SOAPException: faultCode=SOAP-ENV:Client; msg=Connection reset; targetException=java.net.SocketException: Connection reset]
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:422)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.(SOAPConnectorClient.java:222)
... 43 more
Caused by: [SOAPException: faultCode=SOAP-ENV:Client; msg=Connection reset; targetException=java.net.SocketException: Connection reset]
at org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:479)
at org.apache.soap.rpc.Call.WASinvoke(Call.java:451)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient$4.run(SOAPConnectorClient.java:372)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:365)
... 44 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
at java.lang.reflect.Constructor.newInstance(Constructor.java:527)
at com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:456)
... 36 more
Caused by: com.ibm.websphere.management.exception.ConnectorNotAvailableException: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error opening socket: java.net.ConnectException: Connection refused; targetException=java.lang.IllegalArgumentException: Error opening socket: java.net.ConnectException: Connection refused]
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:422)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.(SOAPConnectorClient.java:222)
... 41 more
Caused by: [SOAPException: faultCode=SOAP-ENV:Client; msg=Error opening socket: java.net.ConnectException: Connection refused; targetException=java.lang.IllegalArgumentException: Error opening socket: java.net.ConnectException: Connection refused]
at org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:475)
at org.apache.soap.rpc.Call.WASinvoke(Call.java:451)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient$4.run(SOAPConnectorClient.java:372)
at com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
at com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:365)
... 42 more
原因:java.lang.reflect.InvocationTargetException
位于sun.reflect.NativeConstructorAccessorImpl.newInstance0(本机方法)
位于sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:56)
在sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:39)
位于java.lang.reflect.Constructor.newInstance(Constructor.java:527)
位于com.ibm.websphere.management.AdminClientFactory.createAdminClientPrivileged(AdminClientFactory.java:456)
... 36多
原因:com.ibm.websphere.management.exception.ConnectorNotAvailableException:[SOAPException:faultCode=SOAP-ENV:Client;msg=打开套接字时出错:java.net.ConnectException:连接被拒绝;targetException=java.lang.IllegalArgumentException:打开套接字时出错:java.net.ConnectException:连接被拒绝]
在com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:422)上
位于com.ibm.ws.management.connector.soap.SOAPConnectorClient。(SOAPConnectorClient.java:222)
... 41多
原因:[SOAPException:faultCode=SOAP-ENV:Client;msg=打开套接字时出错:java.net.ConnectException:连接被拒绝;targetException=java.lang.IllegalArgumentException:打开套接字时出错:java.net.ConnectException:连接被拒绝]
位于org.apache.soap.transport.http.SOAPHTTPConnection.send(SOAPHTTPConnection.java:475)
位于org.apache.soap.rpc.Call.wasvoke(Call.java:451)
位于com.ibm.ws.management.connector.soap.SOAPConnectorClient$4.run(SOAPConnectorClient.java:372)
位于com.ibm.ws.security.util.AccessController.doPrivileged(AccessController.java:118)
在com.ibm.ws.management.connector.soap.SOAPConnectorClient.reconnect(SOAPConnectorClient.java:365)上
... 42更多我无法确定为什么
ConfigServiceFactory.getConfigService()
总是返回null,但我已经成功地使用AdminClient
和ConfigServiceProxy
基于中的代码获取了一个工作的ConfigService
实例
基本代码如下,主机名和端口号可能会根据服务器配置而变化:
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST,"localhost");
connectProps.setProperty(AdminClient.CONNECTOR_PORT,"8880");
AdminClient adminClient=AdminClientFactory.createAdminClient(connectProps);
ConfigService service=new ConfigServiceProxy(adminClient);
IBM支持文档没有说明如何确定正确的主机和端口。以下是一些细节:
当websphereapplicationserver被配置为没有部署管理器的单个服务器时,主机名“localhost”和端口号8880是默认值。在集群环境中,每个应用程序服务器的端口号至少是不同的
在集群测试环境中,我首先检查了WebSphereIntegratedSolutionsConsole(ISC)中为每个应用程序服务器列出的端口。这些都不正确
我随后发现(通过谷歌搜索),每台服务器都有几个文件控制实际使用的端口。这些文件位于${USER\u INSTALL\u ROOT}/properties中的每个应用程序服务器的文件系统中。
${USER\u INSTALL\u ROOT}的值可以在ISC的“环境->Websphere变量”中找到。
Linux上的完整目录路径通常是/opt/IBM/WebSphere/AppServer/profiles/AppSrv01/properties。
在该目录中,可以在文件“wsadmin.properties”中找到正确的主机名和端口<
Properties connectProps = new Properties();
connectProps.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
connectProps.setProperty(AdminClient.CONNECTOR_HOST,"localhost");
connectProps.setProperty(AdminClient.CONNECTOR_PORT,"8880");
AdminClient adminClient=AdminClientFactory.createAdminClient(connectProps);
ConfigService service=new ConfigServiceProxy(adminClient);
connectProps.setProperty(AdminClient.USERNAME,"username");
connectProps.setProperty(AdminClient.PASSWORD,"password");
com.ibm.SOAP.securityEnabled=true
com.ibm.SOAP.loginUserid=username
com.ibm.SOAP.loginPassword=password