问题
I have an Singleton Service deployed to a Weblogic cluster, i'd like to know the provider url (listen address and port) of the server on which the singleton service is deployed from the singleton itself (server side, not logs).
I found this (old) article from Oracle, and wrote this method to construct the provider url.
private static String getCurrentServerUrl() throws NamingException {
weblogic.management.MBeanHome home =
(weblogic.management.MBeanHome) new Environment().getInitialContext().lookup(weblogic.management.MBeanHome.LOCAL_JNDI_NAME);
Set mbeanSet = home.getMBeansByType("ServerRuntime");
Iterator mbeanIterator = mbeanSet.iterator();
while (mbeanIterator.hasNext()) {
ServerRuntimeMBean serverRuntime = (ServerRuntimeMBean) mbeanIterator.next();
if (serverRuntime.getState().equals(ServerStates.RUNNING)) {
return serverRuntime.getURL("t3");
}
}
return null;
}
The problem is that this code is deprecated (see MBeanHome, MBeanHome#getMBeansByType(String) and MBeanHome#LOCAL_JNDI_NAME).
What is the non-deprecated way to do this?
PS : I don't want to enter any address (whether it is the address for the server or the address for a JMX service since the application is designed to be migratable from server to server withtout recompile or reconfigure of the app it self).
Thanks for your help.
回答1:
You can lookup the runtime JMX which is the same for all application. No need to enter address, user or password.
1) Lookup the MBeanServer
InitialContext ctx = new InitialContext();
MBeanServer mBeanServer = (MBeanServer) ctx.lookup("java:comp/env/jmx/runtime");
2) Then you need to retrieve the name of the server running the application. Here there is two ways (maybe more).
The simplest
String serverName = System.getProperty("weblogic.Name");
But I don't know if it's a reliable way.
The other way to get the server name is to look for it in the runtime service attributes.
ObjectName serviceObjectName = new ObjectName("com.bea:Name=RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean");
String serverName = (String) mBeanServer.getAttribute(serviceObjectName, "ServerName");
3) Now we can create the runtime server ObjectName ...
ObjectName serverRuntime = new ObjectName("com.bea:Name="+serverName+",Type=ServerRuntime");
4) ... and get the attributes we need
String listenAddress = mBeanServer.getAttribute(serverRuntime, "ListenAddress");
Integer listenPort = mBeanServer.getAttribute(serverRuntime, "ListenPort");
rem: listenAddress is formatted as hostname
/IP
, you have to manipulate it to only get IP or host
From there you can access to a lot of information; like the address of the admin server that manage the runtime server, the runtime cluster, ... .
Here is a list I get from the WLST console (WL12.2.1), didn't test if we have access to all attributes/MBean's.
dr-- ApplicationRuntimes
dr-- AsyncReplicationRuntime
dr-- BatchJobRepositoryRuntime
dr-- ClassLoaderRuntime
dr-- ClusterRuntime
dr-- ConcurrentManagedObjectsRuntime
dr-- ConnectorServiceRuntime
dr-- DefaultExecuteQueueRuntime
dr-- EntityCacheCumulativeRuntime
dr-- EntityCacheCurrentStateRuntime
dr-- EntityCacheHistoricalRuntime
dr-- ExecuteQueueRuntimes
dr-- JDBCServiceRuntime
dr-- JMSRuntime
dr-- JTARuntime
dr-- JVMRuntime
dr-- JoltRuntime
dr-- LibraryRuntimes
dr-- LogBroadcasterRuntime
dr-- LogRuntime
dr-- MANAsyncReplicationRuntime
dr-- MANReplicationRuntime
dr-- MailSessionRuntimes
dr-- MaxThreadsConstraintRuntimes
dr-- MessagingBridgeRuntime
dr-- MessagingBridgeRuntimes
dr-- MinThreadsConstraintRuntimes
dr-- PartitionRuntimes
dr-- PathServiceRuntime
dr-- PathServiceRuntimes
dr-- PersistentStoreRuntimes
dr-- RequestClassRuntimes
dr-- SAFRuntime
dr-- SNMPAgentRuntime
dr-- ServerChannelRuntimes
dr-- ServerLogRuntime
dr-- ServerSecurityRuntime
dr-- ServerServices
dr-- SingleSignOnServicesRuntime
dr-- ThreadPoolRuntime
dr-- TimerRuntime
dr-- WANReplicationRuntime
dr-- WLDFRuntime
dr-- WTCRuntime
dr-- WebServerRuntimes
dr-- WorkManagerRuntimes
dr-- WseeClusterFrontEndRuntime
dr-- WseeWsrmRuntime
-r-- ActivationTime
-r-- AdminServer
-r-- AdminServerHost
-r-- AdminServerListenPort
-r-- AdminServerListenPortSecure
-r-- AdministrationPort
-r-- AdministrationPortEnabled
-r-- AdministrationURL
-r-- AsyncReplicationRuntime
-r-- ClusterRuntime
-r-- CurrentDirectory
-rw- CurrentMachine
-r-- DefaultExecuteQueueRuntime
-r-- DefaultURL
-r-- EntityCacheCumulativeRuntime
-r-- EntityCacheCurrentStateRuntime
-r-- EntityCacheHistoricalRuntime
-r-- HealthState
-r-- HealthStateJMX
-r-- JoltRuntime
-r-- ListenAddress
-r-- ListenPort
-r-- ListenPortEnabled
-r-- MANAsyncReplicationRuntime
-r-- MANReplicationRuntime
-r-- MessagingBridgeRuntime
-r-- MiddlewareHome
-r-- Name
-r-- OpenSocketsCurrentCount
-r-- OracleHome
-r-- OverallHealthState
-r-- OverallHealthStateJMX
-rw- Parent
-r-- PathServiceRuntime
-r-- RestartRequired
-r-- RestartsTotalCount
-r-- SSLListenAddress
-r-- SSLListenPort
-r-- SSLListenPortEnabled
-r-- ServerClasspath
-r-- SocketsOpenedTotalCount
-r-- State
-r-- StateVal
-r-- Type
-r-- WANReplicationRuntime
-r-- WeblogicHome
-r-- WeblogicVersion
-r-- WseeClusterFrontEndRuntime
来源:https://stackoverflow.com/questions/32220760/retrieve-current-servers-provider-url-at-runtime-on-weblogic-non-deprecated-wa