Add dynamic Java code to your application

Write code that can respond to changes at runtime

1 2 Page 2
Page 2 of 2
 

public class DynaCodeInvocationHandler implements InvocationHandler {

public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { // Get an instance of the up-to-date dynamic class Object dynacode = getUpToDateInstance(); // Forward the invocation return method.invoke(dynacode, args); } }

The invoke() method obtains an instance of the up-to-date dynamic class and forwards the invocation to it. This may involve source code compilation and class reloading if the dynamic class's source file has been modified.

Now we have completed the Postman service's dynamic code. A client creates a proxy of the service and calls the method deliverMessage() through the proxy. Each invocation on the proxy is dispatched to the DynaCodeInvocationHandler class's invoke() method. In that method, the latest service implementation is first obtained, which may involve necessary source compilation and class reloading. Then, the invocation forwards to the implementation for real processing.

Put it all together

We have gone through all the tricks required for the dynamic Java code. It's time to put them together to build something reusable.

We can build a utility to ease the adoption of dynamic code by encapsulating the four steps mentioned above. The Postman example relies on such a utility called DynaCode. Remember the PostmanApp application and its omitted getPostman() method? It's time to show it:

 

public class PostmanApp {

public static void main(String[] args) throws Exception { // ...... }

private static Postman getPostman() { DynaCode dynacode = new DynaCode(); dynacode.addSourceDir(new File("dynacode")); return (Postman) dynacode.newProxyInstance(Postman.class, "sample.PostmanImpl"); } }

See how the getPostman() method creates a dynamic Postman service, creates an instance of DynaCode, specifies a source directory, and returns a proxy of some dynamic implementation. To use your own dynamic Java code, you simply need to write three lines of code. Everything else is taken care of inside DynaCode. Try it yourself (the DynaCode's source code is included in the example).

I won't go into further details of DynaCode, but as a review of the technologies we've talked about, take a look at the sequence diagram in Figure 4 to see how DynaCode works at a high level.

Figure 4. Sequence diagram of DynaCode. Click on thumbnail to view full-sized image.

Conclusion

In this article, I introduced the idea of dynamic Java code and the steps required to realize it. I covered topics such as runtime source compilation, class reloading, and the Proxy design pattern. Though none of these topics are new, by putting them together, we create an interesting dynamic feature for common Java classes so they can be modified and refreshed at runtime just like JSP pages.

An example is provided for demonstration purposes in Resources. It includes a reusable utility called DynaCode that makes writing your own dynamic code much easier.

I'd like to conclude by discussing the values and applications of dynamic code: Dynamic code can respond quickly to on-demand change requests. It can be used to implement truly dynamic services and business rules that change from time to time, and to replace embedded scripts used by task nodes of workflows. Dynamic code also eases application maintenance and greatly reduces outages caused by application redeployments.

Li Yang joined IBM in April 2004 and is a Certified IBM Rational Consultant on Rational Unified Process, requirements management, and object-oriented analysis and design. He has experience with Java server-side technologies, Web architectures, Java EE projects, and Java SE system library development.

Learn more about this topic

This story, "Add dynamic Java code to your application" was originally published by JavaWorld.

Copyright © 2006 IDG Communications, Inc.

1 2 Page 2
Page 2 of 2