处理已部署应用程序的其他属性
可通过管理控制台、wsadmin 工具或编程来处理已部署应用程序的属性。使用此示例通过 AppDeploymentTask 对象来处理安装应用程序期间或之后未显示的属性。
开始之前
此任务假定您对 MBean 编程和 ConfigService 接口有基本的了解。请参阅应用程序编程接口文档中有关 MBean 编程和 ConfigService 接口的信息。
关于此任务
对已部署应用程序执行下列任务以通过 AppDeploymentTask 对象处理未显示的属性。这些属性保存在 deployment.xml 文件中,而该文件是在每个已部署应用程序的配置库中创建的。
过程
- 创建会话。
- 连接到 WebSphere® Application Server。
- 找出 ApplicationDeployment 对象。
- 处理属性。
- 保存所作的更改。
- 清除会话。
结果
成功运行代码之后,将在已部署应用程序的 deployment.xml 文件中更新这些属性。
示例
以下示例显示如何根据先前步骤处理 startingWeight、warClassLoaderPolicy 和 classloader 属性。
import java.util.Properties;
import javax.management.Attribute;
import javax.management.AttributeList;
import javax.management.ObjectName;
import com.ibm.websphere.management.AdminClient;
import com.ibm.websphere.management.AdminClientFactory;
import com.ibm.websphere.management.Session;
import com.ibm.websphere.management.configservice.ConfigService;
import com.ibm.websphere.management.configservice.ConfigServiceHelper;
import com.ibm.websphere.management.configservice.ConfigServiceProxy;
import com.ibm.websphere.management.exception.ConfigServiceException;
import com.ibm.websphere.management.exception.ConnectorException;
public class AppManagementSample1 {
public static void main(String[] args) {
String hostName = "localhost";
String port = "8880";
String appName = "ivtApp";
ConfigService configService = null;
// create a session.
Session session = new Session();
// establish connection to the server.
try {
Properties props = new Properties();
props.setProperty(AdminClient.CONNECTOR_TYPE,
AdminClient.CONNECTOR_TYPE_SOAP);
props.setProperty(AdminClient.CONNECTOR_HOST, hostName);
props.setProperty(AdminClient.CONNECTOR_PORT, port);
AdminClient adminClient =
AdminClientFactory.createAdminClient(props);
// create a config service proxy object.
configService = new ConfigServiceProxy(adminClient);
// Locate the application object.
ObjectName rootID = configService.resolve(session,
"Deployment="+appName)[0];
System.out.println ("rootID is: " + rootID);
// Locate the ApplicationDeployment object from the root.
ObjectName appDeplPattern = ConfigServiceHelper.createObjectName
(null, "ApplicationDeployment");
/*
ObjectName appDeplID = configService.queryConfigObjects(session,
rootID, appDeplPattern, null)[0];
*/
AttributeList list1 = configService.getAttributes(session,
rootID, new String[]{"deployedObject"}, false);
ObjectName appDeplID = (ObjectName)
ConfigServiceHelper.getAttributeValue(list1, "deployedObject");
System.out.println ("appDeplID: " + appDeplID);
// Locate the class loader.
// Change the starting weight through the startingWeight attribute. The starting weight
// affects the order in which applications start.
AttributeList attrList = new AttributeList();
Integer newWeight = new Integer (10);
attrList.add(new Attribute("startingWeight", newWeight));
// Change the WAR class loader policy through the warClassLoaderPolicy attribute by
// specifying SINGLE or MULTIPLE.
// SINGLE=one classloader for all WAR modules
attrList.add(new Attribute("warClassLoaderPolicy", "SINGLE"));
// Set the class loader mode to PARENT_FIRST or PARENT_LAST.
AttributeList clList = (AttributeList) configService.getAttribute
(session, appDeplID, "classloader");
ConfigServiceHelper.setAttributeValue (clList, "mode",
"PARENT_LAST");
attrList.add (new Attribute ("classloader", clList));
// Set the new values.
configService.setAttributes(session, appDeplID, attrList);
// Save your changes.
configService.save(session, false);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
// Clean up the session.
try {
configService.discard(session);
}
catch (ConfigServiceException csEx)
{
csEx.printStackTrace();
}
catch (ConnectorException cnEx)
{
cnEx.printStackTrace();
}
}
}
}