JBoss.org Community Documentation

4.4. Indirect access

The run.sh script can be called with an optional parameter 'bus' to specify that calls to the Human Resources service should take place using the microcontainer bus:

./run.sh bus

Instead of using a direct reference to the bean instance obtained from the microcontainer controller we now call an invoke() method on the bus passing in the bean name, method name, method arguments and method types. The bus takes this information and uses it 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;
}

Behind the scenes the bus looks up the reference to the bean instance specified by the bean name and calls the chosen method using reflecton. Since the client never has a direct reference to the bean instance we say that it accesses the service 'indirectly'. As the bus does not cache the reference we can safely make changes to our service configuration and redeploy it at runtime. Subsequent calls by the client will then result in the new reference to the service being used as expected. In technical terms we say that the client and service have been decoupled.

Note

You can test this behaviour for yourself by deploying the service and using the 'p' option to print out the status. Undeploy the service using the 'u' option and check that you cannot access it anymore. Now make some changes to the configuration using the jboss-beans.xml file and deploy it again using the 'd' option (remember to save any changes you make to jboss-beans.xml first). Printing out the status again using the 'p' option should reveal that the client is accessing the new service configuration.

As the bus uses reflection to call bean instances it is slower than using direct access. The benefit however is that only the bus has references to the bean instances. This means that when a service is redeployed we can clean up all of the existing references and replace them with new ones, allowing us to reliably redeploy a service at runtime. Services that are not used very often or that are specific to certain applications are good candidates for indirect access using the microcontainer bus. This is either because the reduction in performance does not matter if the service is used rarely or the service can be deployed and undeployed together with the relevant applications to keep the runtime environment clean.