Hi,

Jay SenSharma

Jay SenSharma

Here is a Somple JMX code using this we can dynamically change the Server Ports… Here we are going to change AdminServer Port. This is a Very Basic and common sample which demonstrates How we can use the Most important MBean “weblogic.management.mbeanservers.edit.EditServiceMBean“. In order to make any Configuration related Changes in WebLogic first of all we must get the instance of this EditServiceMBean.

Using WLST also it is very easy to do it  like: http://forums.oracle.com/forums/thread.jspa?threadID=1037611&tstart=0. For this kind of operations it is always better to chose the WLST option which is more handy and simple.

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.Hashtable;

import javax.management.Attribute;
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;

public class ChangeAdminServerPort {
private static MBeanServerConnection connection;
private static JMXConnector connector;
private static final ObjectName service;

static
{
try {
service = new ObjectName("com.bea:Name=EditService,Type=weblogic.management.mbeanservers.edit.EditServiceMBean");
}
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.edit";

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 ObjectName startEditSession() throws Exception
{
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");
ObjectName domainConfigRoot = (ObjectName)

connection.invoke(cfgMgr,"startEdit", new Object[] { new Integer(60000),new Integer(120000) }, new String[] { "java.lang.Integer","java.lang.Integer" });

if (domainConfigRoot == null)
{
throw new Exception("Somebody else is editing already");
}
return domainConfigRoot;
}

public void editAdminServerPort(ObjectName cfgRoot,Integer newPort) throws Exception
{
Attribute adminport = new Attribute("ListenPort", newPort);
connection.setAttribute(cfgRoot, adminport);
System.out.println("Changed the Admin Console context path to " +"secureConsoleContext");
}

public ObjectName activate() throws Exception
{
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");
ObjectName task = (ObjectName) connection.invoke(cfgMgr, "activate",new Object[] { new Long(120000) }, new String[] { "java.lang.Long" });
return task;
}

public static void main(String[] args) throws Exception
{
String hostname = args[0];
String portString = args[1];
String username = args[2];
String password = args[3];

ChangeAdminServerPort ewb = new ChangeAdminServerPort();
initConnection(hostname, portString, username, password);
ObjectName cfgMgr = (ObjectName) connection.getAttribute(service,"ConfigurationManager");

ObjectName cfgRoot = ewb.startEditSession();

Integer newPort=new Integer("9001");
ewb.editAdminServerPort(new ObjectName("com.bea:Name=AdminServer,Type=Server"),newPort);
connection.invoke(cfgMgr, "save", null, null);
ewb.activate();
connector.close();
}
}

.

.

Thanks

Jay SenSharma

If you enjoyed this post, please consider leaving a comment or subscribing to the RSS feed to have future articles delivered to your feed reader.