|
YOUR FEEDBACK
Did you read today's front page stories & breaking news?
SYS-CON.TV SYS-CON.TV WEBCASTS |
TOP COLDFUSION LINKS CFMX ColdFusion MX/J2EE Hybrid Applications
ColdFusion MX/J2EE Hybrid Applications
By: Drew Falkman
Aug. 12, 2003 12:00 AM
Since the release of ColdFusion MX, the ColdFusion community has been rumbling about the possibilities made available by CFMX's powerful Java 2 Enterprise Edition (J2EE) underpinnings; using servlets, JavaServer Pages (JSP), JavaBeans, and Enterprise JavaBean (EJB) components are the most notable examples. Out of this discussion has arisen the concept of ColdFusion/J2EE hybrid applications. As an ideal, CFMX/J2EE hybrids offer the rapid development and robust high-level services of CFMX, while integrating the deeper transactional and low-level programming power of Java. With all of the possibilities for architecting and developing these hybrid applications, it seems to me that it's time for the ColdFusion development community to seriously discuss what some of the best practices might be when developing these hybrid applications. Surely there are infinite possibilities for integrating CFMX with J2EE, but which of these are viable and truly offer the best that both frameworks have to offer? This is the question I have been asking myself again and again over the past few months while I collaborated on a book about this topic (Reality ColdFusion: J2EE Integration), developed applications (including the Lindex component using Apache Lucene for the Macromedia Developer's Resource Kit 3), and did a case study for Macromedia's DevNet site (www.macromedia.com/devnet/mx/coldfusion/ j2ee/articles/hybrid.html) on hybrid applications. In short, I've been living a hybrid life. This article is not only to share what I've learned, but also to contribute to and further an ongoing discussion on best practices for CFMX/J2EE hybrid applications. Note: in order to best focus on the subject at hand, I have foregone providing background on what the two environments, CFMX and J2EE, are. If you are looking for more background information on this subject, I suggest the following resources: ColdFusion MX and J2EE: A Good Match
![]() At the core, CFMX brings rapid development to J2EE, something it was clearly lacking before. Developers can learn and become proficient in using CFML in significantly less time than Java, which requires understanding object-oriented programming concepts, strict data types, and a vast library of programming constructs. At the same time, because of its object-oriented nature and vast library collection, Java offers a considerable amount of control for those who know how to use it. In addition, the J2EE framework contains a number of programming constructs - such as EJBs and messaging services - that simply can't be emulated using ColdFusion alone. The best part of these differences is that there is no reason to choose one or the other anymore, just when to use which. This leads us to the heart of the matter: the best practices. The 10 Best Practices Best Practice #1: Best Practice #2: In fact, capitalizing on existing code is a central argument for hybrid applications - use the Java code you have already developed, but build around it in CFMX (and in half the time it would take to do so in Java). In addition, this point should include capitalizing on open-source (or closed-source, for that matter) code in both environments. The ColdFusion community has a number of good resources for CF tags and user-defined functions, and the Java community has a huge open-source and third-party product community - use this prebuilt, pretested code whenever possible. Best Practice #3: Best Practice #4: This layer of abstraction makes irrelevant how the eCommerce.cfc works internally, thus allowing the getTaxOwed() CF function to call on an EJB or some other type of Java class, or even perform the logic itself. Using this pattern is great for prototyping - simply return dummy data in the prototype, and then when you plug it in for real, only the CFC needs to be changed. This is based on a well-established J2EE pattern called the Session Façade, whereby a session bean (EJB) is used to abstract logic of other EJB components. Figure 1 shows the CFC Façade pattern.
![]() Best Practice #5: Between the Application.cfm, OnRequestEnd.cfm, CFMX's built-in security, and other services, as well as other frameworks (such as Fusebox and variations thereof), CFMX can effectively control an application, and output the UI - whether the UI is HTML, Flash, or XML. It is in this capacity that CFMX really shines. Developing the View/Controller aspects of an application in CFML will take so much less time than developing servlets and JSP pages that your job might become a part-time position, or you at least will have time (finally!) to get up to date on the water-cooler gossip. Best Practice #6: Use J2EE event listeners Best Practice #7: Understand EJBs There are four types of beans: entity beans that handle interactions with datasources; message beans that handle messaging services; stateful session beans that store session data; and stateless session beans that handle business logic processing. All of these have a unique purpose. Because EJBs can be distributed across an enterprise, they can be an integral part of applications that are called on by other Java applications - be they desktop, applets, J2EE, or CFMX. They are also easy to deploy as Web services and to call on from .NET or other types of applications. Best Practice #8: The best J2EE elements to use (in my opinion) Deciding whether to use the CFMX framework or servlet filters is a matter of choice, remembering that servlet filters are controlled (like event listeners) in the deployment descriptor, so they can be easily turned on/off and are especially handy when they provide processing for multiple applications - something that can't be done with the CFMX framework. Best Practice #9: Some J2EE elements are best used only if they already exist UDFs and ColdFusion custom tags are quicker to build. If you need low-level processing, Java classes are probably a better way to handle this. (For more on integrating Java classes in CFMX, see that CFMX manual reference as well as Guy Rish's eight-part series, "A Cold Cup o' Joe," starting in CFDJ Vol. 3, issue 1 and ending in Vol. 4, issue 8. While it discussed integration in CF 4.5 and 5, many of the same concepts for integrating with Java classes using CFOBJECT apply.) Finally, servlet filters, though handy, really don't offer much beyond what Application.cfm and OnRequestEnd.cfm do. If you need to process logic for only certain pages or folders, do a <cfif> check on the CGI.Script_Name variable; of course the exception is server-wide processing. Best Practice #10: Only use JSPs if they already exist and you won't need to modify them significantly to work with your application. Having used both, I have found it is often faster to rebuild in ColdFusion (using the existing JSP page as a starting point) than to make the modifications to the original JSP pages and maybe their controller servlets. For example, Java developers will often put queries in the controller servlet, then output in the JSP page. So if you want to add a field to the output, you need to edit (and recompile) the servlet to change the query (usually in a Java IDE), and edit the JSP (usually in an HTML editor) to change the output. Multiple development environments alone make this a hassle. Even if you are retrieving a query from a CFC into a CFML template, the change can at least be made in your CFML editor of choice. (For more on a comparison of CFML and JSP, see Vince Bonfanti's article, "Making the Case for CFML," in CFDJ, Vol. 5, issue 6.) As far as the other frameworks such as Velocity and Struts, simply put, these are J2EE-only frameworks. I know there are a very few people out there using Struts with CFMX, but in general I wouldn't recommend it. CFMX offers more services and a better (in my opinion) framework, and this only adds a third element to hybrid applications. What you will end up with is a hydra, not a hybrid. Conclusion YOUR FEEDBACK
CFDJ LATEST STORIES . . .
SUBSCRIBE TO THE WORLD'S MOST POWERFUL NEWSLETTERS SUBSCRIBE TO OUR RSS FEEDS & GET YOUR SYS-CON NEWS LIVE!
|
SYS-CON FEATURED WHITEPAPERS MOST READ THIS WEEK |
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||