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;
}
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.