Retrieve current server's provider URL at runtime on weblogic (non-deprecated way)
Asked Answered
L

1

0

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.

Leapfrog answered 26/8, 2015 at 7:45 Comment(6)
What exactly do you need to know? The hostname? The port? The context root? Determining what host you are on is as simple as InetAddress.getLocalHost().getHostName()Previous
I need to know the listen address and the listen port of the current server in a portable way (and I do not want this listen address to be the local host address). Actually i want to retrieve an RMI object stored on the JNDI of a specific server (in a cluster), to do so, I store the URL of the server on the shared database so I can get the context of the server from other servers of the cluster. If you have a better option to do this , i'm willing to try.Leapfrog
You might have already seen this (or other examples): middlewaremagic.com/weblogic/?p=210 I think you're going to want to connect to the admin server and get the DomainRuntimeServiceMBean. All of the newer weblogic examples point to something like this: docs.oracle.com/cd/E23943_01/web.1111/e13728/… It should be portable as long as you reference environment variables, etc for the admin urlPrevious
Here we have to have the admin url, port, hostname and password, which i do not have.Leapfrog
Admin url should automatically be set in the environment variable ADMIN_URL when your managed server is started via the startManagedWebLogic script. How are you supposed to run a server without knowing any of this information?Previous
I do not start the server, I just have access to one application deployed in itLeapfrog
B
3

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
Bureaucratize answered 5/7, 2017 at 13:34 Comment(1)
I don't think clapsus has try my solution since there is 2 years between his request and my answer.Bureaucratize

© 2022 - 2024 — McMap. All rights reserved.