此内容没有您所选择的语言版本。

4.4. Indirect Access


The run.sh script can be called with an optional parameter bus, which causes calls to the Human Resources service to use the Microcontainer bus.
Instead of using a direct reference to the bean instance obtained from the Microcontainer controller, the new behavior is to call an invoke() method on the bus, passing in the bean name, method name, method arguments and method types. The bus uses this information to call the bean on the client's behalf.
private final static String HRSERVICE = "HRService";

...

    @SuppressWarnings("unchecked")
	Set<Employee> listEmployees() {
	if (useBus)
	    return (Set<Employee>) invoke(HRSERVICE, "getEmployees", new Object[] {}, new String[] {});
	else
	    return manager.getEmployees();
    }

private Object invoke(String serviceName, String methodName, Object[] args, String[] types) {
    Object result = null;
    try {
	result = bus.invoke(serviceName, methodName, args, types);
    } catch (Throwable t) {
	t.printStackTrace();
    }
    return result;
}
Copy to Clipboard Toggle word wrap
The bus looks up the reference to the named bean instance and calls the chosen method using reflection. The client never has a direct reference to the bean instance, so it is said to accesses the service indirectly. Since the bus does not cache the reference, you can safely make changes to the service configuration, and it can be redeployed at run-time. Subsequent calls by the client will use the new reference, as expected. The client and service have been decoupled.

Note

This behavior can be tested by deploying the service and using the p option to print out the status. Undeploy the service using the u option and notice that it is inaccessible. Next, make some changes to jboss-beans.xml file, save the changes, and deploy it again using the d option. Print out the status again using the p option. The client is accessing the new service configuration.
Because the bus uses reflection to call bean instances, it is a slower than direct access. The benefit of the approach is that only the bus has references to the bean instances. When a service is redeployed, all of the existing references can be cleaned up and replaced with new ones. This way, a service can be reliably redeployed at run-time. Services that are not used very often or that are specific to certain applications are good candidates for indirect access using the Microcontainer bus. Often, the reduction in performance is outweighed by the flexibility that this provides.
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat