使用WebSphere MBean更改JVM属性

时间:2018-08-13 03:01:00

标签: java jvm websphere

我正在尝试创建一个Java程序,该程序将在WebSphere上编辑JVM的类路径属性。现在,我可以将程序连接到服务器并找到该节点,但是我不知道下一步该怎么做。我应该使用哪个MBean来查找JVM Classpath并对其进行配置?我知道我可以在AdminConsole本身上对其进行编辑,但是对于此任务,我确实必须使用Java程序来完成。

我也不允许使用wsadmin和jacl / jython脚本。

到目前为止,这是我的代码:

public class Expi 
{
    private AdminClient adminClient;
    private ObjectName nodeAgent;
    private AdminService adminSF;

    String hostName = "localhost";
    String hostPort = "8880";
    String nodeName = "node1";
    String servName = "server1";

    public static void main(String [] args) throws NamingException, AdminException
    {
        Expi xp = new Expi();

        xp.createAdminClient();
        xp.getNodeAgentMBean(nodeName);
        xp.testMB();
    }

    private void createAdminClient()
    {
         // Set properties for the connection 
        Properties connectProps = new Properties();
        connectProps.setProperty(
        AdminClient.CONNECTOR_TYPE, AdminClient.CONNECTOR_TYPE_SOAP);
        connectProps.setProperty(AdminClient.CONNECTOR_HOST, hostName);
        connectProps.setProperty(AdminClient.CONNECTOR_PORT, hostPort);
        connectProps.setProperty(AdminClient.USERNAME, hostName);
        connectProps.setProperty(AdminClient.CONNECTOR_SECURITY_ENABLED, "false");
        // Connection Status Update
        try
        {
            adminClient = AdminClientFactory.createAdminClient(connectProps);
            System.out.println("Connected to SOAP Connection!");
        }
        catch (java.lang.Exception e)
        {
            System.out.println("Exception creating admin client: " + e);
            System.exit(-1);
        }       
    }

     private void getNodeAgentMBean(String nodeName) throws NamingException
        {
            // Finding the specified node
            try
            {
                String query = "WebSphere:type=Server,node="+ nodeName + ",*";
                ObjectName queryName = new ObjectName(query);
                Set s = adminClient.queryNames(queryName, null);
                if (!s.isEmpty())
                {
                    nodeAgent = (ObjectName)s.iterator().next();   
                    System.out.println("Specified Node Found:" + nodeName);
                }
                else
                {
                    System.out.println("Node agent MBean was not found");
                    System.exit(-1);
                }
            }
            catch (MalformedObjectNameException e)
            {
                System.out.println(e);
                System.exit(-1);
            }
            catch (java.lang.Exception e)
            {
                System.out.println(e);
                System.exit(-1);
            }
        }

    private void testMB() //find the correct MBean and use it to edit the values
    {
        AdminServiceFactory.getAdminService();
        AdminServiceFactory.getMBeanFactory();

    }

}

任何帮助将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

您可以使用AdminClient API执行admin命令。很难找到具体的动作,因为它没有像JACL或JYTHON那样有据可查。

下面的示例可用于简单的“ listNodes ”命令。您可以对“ listServers ”使用相同的方法,并获取要修改的特定服务器对象。获取服务器对象后,您可以查看以下相同文档,以在特定目标对象上执行命令。

AdminCommand cmd = cmdMgr.createCommand("listNodes");
cmd.setConfigSession(session);

AsyncCommandClient  asyncCmdClientHelper = 
new AsyncCommandClient(session, null);
asyncCmdClientHelper.execute(cmd);

CommandResult result = cmd.getCommandResult(); 
if (result.isSuccessful()) {
    System.out.println("Successfully executed the command");
    System.out.println("Result: ");
    Object resultData = result.getResult();
    if (resultData instanceof Object[]) {
        Object[] resDataArr = (Object[])resultData;
        for (Object resData : resDataArr) {
            System.out.println(resData);
        }   
    } else {
        System.out.println(resultData);
    }
 } else {
    System.out.println("Failed to execute the command");
    result.getException().printStackTrace();
}

文档链接:https://www.ibm.com/developerworks/websphere/techjournal/1303_samantary/1303_samantary.html