Hi,
Here is another demonstration of using Java Management Extension API (JMX)….
As the earlier version of the same program was not able to display the state informations of the Servers which are down … so i modified the ServerState Program a little bit to get the State informations of those Servers as well which are down.
So the current page contains the Modified version of the program mentioned in :
Getting All Server State Info in 9.x JMX Style
import weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean; import weblogic.management.runtime.ServerRuntimeMBean; import javax.management.MBeanServerConnection; import javax.management.MalformedObjectNameException; import javax.management.ObjectName; import javax.management.remote.JMXConnector; import javax.management.remote.JMXConnectorFactory; import javax.management.remote.JMXServiceURL; import javax.naming.Context; import weblogic.management.jmx.MBeanServerInvocationHandler; import java.util.Hashtable; import java.io.IOException; import java.net.MalformedURLException; import weblogic.management.runtime.JDBCDataSourceRuntimeMBean; import javax.management.*; import javax.naming.*; public class DomainConfigurationMbeanTest { private static MBeanServerConnection connection; private static JMXConnector connector; private static final ObjectName service; private static String combea = "com.bea:Name="; private static String service1 = "DomainRuntimeService,Type=weblogic.management.mbeanservers.domainruntime.DomainRuntimeServiceMBean"; private static String service2 = "RuntimeService,Type=weblogic.management.mbeanservers.runtime.RuntimeServiceMBean"; static { try { service =new ObjectName(combea + service1); } catch (MalformedObjectNameException e) { throw new AssertionError(e.getMessage()); } } public static void initConnection(String hostname, String portString, String username, String password) throws IOException,MalformedURLException { String protocol = "t3"; Integer portInteger = Integer.valueOf(portString); int port = portInteger.intValue(); String jndiroot = "/jndi/"; String mserver = "weblogic.management.mbeanservers.domainruntime"; JMXServiceURL serviceURL = new JMXServiceURL(protocol, hostname, port, jndiroot + mserver); Hashtable h = new Hashtable(); h.put(Context.SECURITY_PRINCIPAL, username); h.put(Context.SECURITY_CREDENTIALS, password); h.put(JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES,"weblogic.management.remote"); connector = JMXConnectorFactory.connect(serviceURL, h); connection = connector.getMBeanServerConnection(); } public static ObjectName[] getServerRuntimes() throws Exception { return (ObjectName[]) connection.getAttribute(service,"ServerRuntimes"); } public void printNameAndState() throws Exception { ObjectName arr[]=getServerRuntimes(); for(ObjectName temp : arr) System.out.println("nt servers: "+temp); ObjectName domain = (ObjectName) connection.getAttribute(service,"DomainConfiguration"); System.out.println("nt Domain: " + domain.toString()); ObjectName[] servers = (ObjectName[]) connection.getAttribute(domain,"Servers"); for (ObjectName server : servers) { String serverState="UNKNOWN"; String aName = (String) connection.getAttribute(server,"Name"); try{ ObjectName ser= new ObjectName("com.bea:Name="+aName+",Location="+aName+",Type=ServerRuntime"); serverState=(String) connection.getAttribute(ser,"State"); System.out.println("nt Server: "+aName+"t State: "+serverState); } catch(Exception e) { System.out.println("nt Server: "+aName+"t State: SHUTDOWN (or) In State : "+ serverState); } } } public static void main(String[] args) throws Exception { String hostname = args[0]; String portString = args[1]; String username = args[2]; String password = args[3]; DomainConfigurationMbeanTest s = new DomainConfigurationMbeanTest(); initConnection(hostname, portString, username, password); s.printNameAndState(); connector.close(); } }
Output …will be like following:
July 6th, 2010 on 1:51 pm
Hi Jay,
it is very useful but what if i wanna get the server states when the admin server is down, there will be a “destination unreachable” error.
Is there any way for getting states of all managed servers een if the admin server is down?
July 6th, 2010 on 9:06 pm
hi jay;
it is very useful and profitable knowledge.I don’t start to job before to view your site.
thanks.
when I run the above code gives some errors
GetServerState.java:18: class DomainConfigurationMbeanTest is public, shou
ld be declared in a file named DomainConfigurationMbeanTest.java
public class DomainConfigurationMbeanTest {
^
1 error
can you help me?
regards…
July 6th, 2010 on 9:25 pm
Hi Erdem_Ustun,
Please rename the program name from “GetServerState.java” to “DomainConfigurationMbeanTest.java”.
Now recompile the program as :
javac DomainConfigurationMbeanTest.java
Then run the program as
java DomainConfigurationMbeanTest
Java Standard rule says that the class name and the Program name both must be same …ONLY if you have declared the class as ‘public’ inside the program.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
July 6th, 2010 on 9:29 pm
excuse me
I realized wrong class name but I sent message
July 6th, 2010 on 9:37 pm
C:Basic-Jmx>javac DomainConfigurationMbeanTest.java
C:Basic-Jmx>java DomainConfigurationMbeanTest
Exception in thread “Main Thread” java.lang.ArrayIndexOutOfBoundsException: 0
at DomainConfigurationMbeanTest.main(DomainConfigurationMbeanTest.java:8
0)
July 6th, 2010 on 9:43 pm
Hi Erdem_Ustun,
You are not passing the Command Line Arguments to the program as mentioned at the Post …there is an Image …http://middlewaremagic.com/weblogic/wp-content/uploads/2010/07/ServerState.jpg
u need to run like:
C:Basic-Jmx>java DomainConfigurationMbeanTest localhost 7001 weblogic weblogic
Where
localhost = is the host address / host name of your AdminServer
7001 = is the Port of your Admin Server
weblogic = is the username of your Admin Server
weblogic= is the password of your AdminServer
Above 4 values may be different in your environment …so you need to provide the correct values for those 4 arguments.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
July 6th, 2010 on 10:17 pm
thanks for all your helping.
I want to ask a question off-topic .
you have written codes in java for weblogic useful and profitable.
thanks without end to you.
is it possible to collect all codes in a application?
because I could not these knowledge at oracle sites so very important knowledge ,trick,..etc.
sory for my english is very bad.
best regards..
July 6th, 2010 on 10:40 pm
Hi Erdem_Ustun,
It is quite possible to assemble all the JMX code inside a Simple WebApplication for Complete WebLogic Domain/Servers/Clusters/JMS/JDBC system analysis. Any developer can easily use the same codes to put in his web application to monitor the complete WebLogic Domain.
You can try this. It will be a best tool. Let us know if you find any difficulty in that.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
July 7th, 2010 on 5:43 pm
Hi Jay,
did you have a chance to look my problem?
July 7th, 2010 on 8:27 pm
Hi Yusuf,
Actually DomainRuntimeMBean is available only on the AdminServer …so we can easily get the information of all the Managed Servers/AdminServer running on the Domain. But if AdminServer is down then to get the Managed Server’s State information ….. The ONLY Condition is required that “ALL Managed Servers Should be started Using NodeManager”.. Because NodeManager also can get the State Information of Servers Associated with it.
I will give it a try..But i m waiting for your confirmation …that Are u running all your Managed Servers Using NodeManager? And Please specify the Exact version of your WebLogic sothat i can try developing the Demo using that perticular Servers MBean API.
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
July 8th, 2010 on 1:49 pm
In my environment, some domains have a node manager, so i start/stop my MS by a NM. Also i have domains without nodemanager.
the version of our weblogic servers are upper 10.0 (most of them 10.3R3)
So i am expecting two different solutions to my problem depending on using NM.
best regards
July 9th, 2010 on 2:11 pm
Hi Yusuf,
It is not possible from WebLogic Side to get the Server State (When No NodeManager is associated with Servers) When Admin Server is Not Running.
Reason—-In your Scenario
1). AdminServer is Already Down
2). No NodeManager is Associated with ManagedServers.
3). Managed Server Went Down then Who will send Notification…? ( No One )
So in above scenario you need to use some OS related utilities like ps -ef | grep 7002
Grep the Port number of Managed Servers Manually…to check if it is running or Not…
Or from Remote Box…try doing telnet to your Managed Servers One – by -One to see if it is Listening on a perticular Port or Not.
telnet ManagedServerHost ManagedServerPort
Example: telnet 10.10.19.122 7003
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
June 17th, 2011 on 6:00 pm
Hi Jay,
Thanks for your post but I think you got me wrong, this is not what i meant.The above JMX code is also not showing the intermittent state of the server.
Let me try to explain the scenario…
for eg: If the server is currently shut down and i am performing the start operation on it, then at this point of time i want to get the server state…at this time the server should be in ‘Starting’ state but i am unable to fetch it through the above JMX code…since i am able to see the appropriate result through the admin console of that server.
My question is – Is it possible to fetch the server state attribute while the server itself is not up or not in Running state?
I think I should not hard code the ‘Shutdown’ state of the server when it throws InstanceNotFoundException.
There would be some way to show the intermittent of the server.
Thanks in Advance,
Vibhor Rastogi
TopMagicUsers
June 20th, 2011 on 12:32 am
Hi Vibhor,
An Article is developed based on your query to get the Server Life cycle State even when it is not completely Running. Please refer to the following link:
http://middlewaremagic.com/weblogic/?p=6910
.
.
Keep Posting 🙂
Thanks
Jay SenSharma
June 20th, 2011 on 7:17 pm
Thanks that worked….
Vibhor
November 30th, 2012 on 4:16 pm
Hi While trying to run the above Program in Eclipse i am getting above Error Can you Help me
Exception in thread “main” java.io.IOException
at weblogic.management.remote.common.ClientProviderBase.makeConnection(ClientProviderBase.java:196)
at weblogic.management.remote.common.ClientProviderBase.newJMXConnector(ClientProviderBase.java:84)
at javax.management.remote.JMXConnectorFactory.newJMXConnector(JMXConnectorFactory.java:338)
at javax.management.remote.JMXConnectorFactory.connect(JMXConnectorFactory.java:247)
at wls.SS.DomainConfigurationMbeanTest.initConnection(DomainConfigurationMbeanTest.java:50)
at wls.SS.DomainConfigurationMbeanTest.main(DomainConfigurationMbeanTest.java:91)
Caused by: javax.naming.CommunicationException [Root exception is java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException]
at weblogic.jrmp.Context.lookup(Context.java:189)
at weblogic.jrmp.Context.lookup(Context.java:195)
at javax.naming.InitialContext.lookup(InitialContext.java:392)
at weblogic.management.remote.common.ClientProviderBase.makeConnection(ClientProviderBase.java:179)
… 5 more
Caused by: java.rmi.ConnectIOException: error during JRMP connection establishment; nested exception is:
java.io.EOFException
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:286)
at sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:184)
at sun.rmi.server.UnicastRef.newCall(UnicastRef.java:322)
at weblogic.jrmp.BaseRemoteRef.invoke(BaseRemoteRef.java:221)
at weblogic.jrmp.RegistryImpl_Stub.lookup(Unknown Source)
at weblogic.jrmp.Context.lookup(Context.java:185)
… 8 more
Caused by: java.io.EOFException
at java.io.DataInputStream.readByte(DataInputStream.java:250)
at sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:228)
… 13 more