Вы находитесь на странице: 1из 64


40 & 62
No. 1 i-Technology Magazine in the World


An Introduction to Java Object Serialization

Service Data Objects for Performance

Best Practices for Managing Objects Between
JDBC Programming Java and C
T H E W O R L D ’ S L E A D I N G i -T E C H N O L O G Y M A G A Z I N E W W W. S Y S - C O N . C O M / J D J

���������� � �


������� ��� ���
������ ���������� ��������
������ ���������� ����������
������������������� � � � �
� ��������������������������� � � � �
� �������������������������� � � � �

����������������� � � � �
� �������������������������������� � � � �
� �������������������������� � � � �
� ������������������������ � � � �
� ������������������������������� � � � �

������������������ � � � �
� �������������� � � � �
� ������������������� � � � �
� ������������������ � � � �
� �������������� � � � �

����������������������� � � � �

���������� � � � �

������������������ ���� ���� ���� ����





From the Group Publisher

Editorial Board
Desktop Java Editor: Joe Winchester
Is Mergermania
Core and Internals Editor:
Contributing Editor:
Contributing Editor:
Contributing Editor:
Calvin Austin
Ajit Sagar
Yakov Fain
Bill Roth
Back? Jeremy Geelan
Contributing Editor: Bill Dudney

Contributing Editor: Michael Yuan
Founding Editor: Sean Rhody urricanes Ivan, Charley, and Fran- And the “winner”? Not Oracle, according
ces notwithstanding, sometimes to the Parasoft chairman. “In a situation
Production being in the eye of the storm has like this,” he contended, “the winner will be
Production Consultant: Jim Morgan its advantages. the other guy, not Oracle. Oracle will have
Associate Art Director: Tami Beatty–Lima
Executive Editor: Nancy Valentine At SYS-CON Media, where we by defini- to support PeopleSoft customers for quite
Associate Editors: Jamie Matusow tion dwell at the epicenter of what might a while. It cannot just kill the products. In
Gail Schultz be called the i-technology weather cycle, the meantime, because of all the public-
Assistant Editor: Natalie Charters
Online Editor: Martin Wezdecki our central position allows us to ask indus- ity, anybody who thought about buying
Research Editor: Bahadir Karuv, PhD try influencers for quickfire responses to PeopleSoft software will already have
burning issues of the day. decided to buy competitive packages. The
Writers in This Issue The recent PeopleSoft–Oracle decision real winners are probably SAP and IBM.”
Derek Ashmore, Calvin Austin, John Cahill,
Brent Daniel, BIll Dudney, Mike Edwards, Tim Ellison, handed down by the U.S. Justice Depart- Dr. Kolawa offered a final thought: “I
Naveen Gabrani, Jeremy Geelan, Hari K. Gottipati, ment is a case in point. U.S. District Judge have seen battles like this before, and
Chet Haase, Onno Kluyt, William Knight,
Ajit Sagar, Dmitri Trembovetski, Kevin Williams, Vaughn Walker had given his verdict on they were irrelevant a few years later. Do
Joe Winchester, John Zukowski the antitrust lawsuit filed seven months you remember Compaq buying DEC, or
To submit a proposal for an article, go to
http://grids.sys-con.com/proposal ago by the U.S. Justice Department and 10 HP buying Compaq? They brought more
states seeking to block Oracle’s would-be headaches than they were worth to the
Subscriptions “hostile takeover” of rival PeopleSoft. In his companies that were involved in them.”
For subscriptions and requests for bulk orders, please send your 164-page decision Walker ruled that there The Powersoft merger with Sybase in
letters to Subscription Department:
weren’t sufficient grounds to block it. 1995 was, at the time, the most valuable in
888 303-5282
201 802-3012 The judgement was enough to send the history of the software industry. But an
subscribe@sys-con.com PeopleSoft’s stock climbing. While it Oracle–PeopleSoft deal would set a new
Cover Price: $5.99/issue. Domestic: $69.99/yr. (12 Issues) seemed to bring to an end a lawsuit on record, including a record for the hostile
Canada/Mexico: $99.99/yr. Overseas: $99.99/yr. (U.S. Banks or
Money Orders) Back Issues: $10/ea. International $15/ea. which Oracle has allegedly spent some $60 nature of the whole arrangement.
million and PeopleSoft even more, some In a letter written on Sept. 9 after the U.S.
Editorial Offices $70 million, we wondered whether it also District Judge’s decision, Oracle chairman
SYS-CON Media, 135 Chestnut Ridge Rd., Montvale, NJ 07645
Telephone: 201 802-3000 Fax: 201 782-9638 marked a new beginning: the reemergence Jeff Henley and CEO Larry Ellison wrote to
Java Developer’s Journal (ISSN#1087-6944) is published monthly of mergermania in the world of enterprise the PeopleSoft board: “With the removal
(12 times a year) for $69.99 by SYS-CON Publications, Inc., 135 technology. Also, if Oracle “won,” who lost? of the U.S. antitrust issue and Oracle’s
Chestnut Ridge Road, Montvale, NJ 07645. Periodicals postage
rates are paid at Montvale, NJ 07645 and additional mailing Taking soundings around us, it wasn’t commitment to acquire PeopleSoft, we are
offices. Postmaster: Send address changes to: Java Developer’s long before we heard back from Mitchell hopeful that a transaction can occur.” Peo-
Journal, SYS-CON Publications, Inc., 135 Chestnut Ridge Road,
Montvale, NJ 07645. Kertzman, now of Hummer Winblad pleSoft’s reply was curt: “PeopleSoft’s Board
Venture Partners – and famous before that, has carefully considered and unanimously
©Copyright of course, as the founder and CEO of Pow- rejected each of Oracle’s offers, including its
Copyright © 2004 by SYS-CON Publications, Inc. All rights reserved. No
part of this publication may be reproduced or transmitted in ersoft in 1974, which merged in February current offer of $21 per share. On May 25,
any form or by any means, electronic or mechanical, including 1995 with Sybase, of which he subsequent- 2004, the Board concluded that the current
photocopy or any information storage and retrieval system, without
written permission. For promotional reprints, contact reprint ly became chairman of the board and CEO. offer was inadequate and did not reflect
coordinator Kristin Kuhnle, kristin@sys-con.com. SYS-CON Media and “The U.S. Justice Department’s PeopleSoft’s real value.” Jeremy Geelan is
SYS-CON Publications, Inc., reserve the right to revise, republish and
authorize its readers to use the articles submitted for publication. patchwork-quilt antitrust policy is the To our mergermania question, JBoss group publisher of
Worldwide Newsstand Distribution loser here,” Kertzman commented. “The CEO Marc Fleury commented: “Con- SYS-CON Media, and
Curtis Circulation Company, New Milford, NJ software industry needs a clearer, more co- solidation is a natural fact in the software is responsible for the
For List Rental Information:
Kevin Collopy: 845 731-2684, kevin.collopy@edithroman.com herent statement and practice of antitrust markets. I don’t see losers but rather development of new
Frank Cipolla: 845 731-3832, frank.cipolla@epostdirect.com guidelines from Justice,” he added. efficiencies at play.” He went on to note titles and technology
Newsstand Distribution Consultant Another prime mover in the enterprise that the mergers and consolidations might portals for the
Brian J. Gregory/Gregory Associates/W.R.D.S.
732 607-9941, BJGAssociates@cs.com technology space, Dr. Adam Kolawa – co- well be catalyzed by the recent rise of open firm. He regularly
Java and Java-based marks are trademarks or registered
founder, chairman, and CEO of Parasoft source software. “Open source accelerates represents SYS-CON at
trademarks of Sun Microsystems, Inc., in the United States and – felt that the problem lay in the issue this natural consolidation by putting great conferences and trade
other countries. SYS-CON Publications, Inc., is independent of
Sun Microsystems, Inc. All brand and product names used on
having ever gone to court in the first place. pressure on the profits of infrastructure shows, speaking to
these pages are trade names, service marks or trademarks of “I think the suit was a mistake,” Dr. Kolawa software players, for example, the rumors technology audiences
their respective companies.
declared. “Oracle should have been allowed have it that BEA is next on Oracle’s shop- both in North America
to purchase PeopleSoft in the first place. So ping list,” Fleury said. and overseas.
we wasted some time and money and now No one said 2004 would to be a dull year
we are able to get down to business.” in the enterprise technology world. jeremy@sys-con.com

www.SYS-CON.com/JDJ October 2004 3

������������������� ������������ ��

JDJ Cover Story

by Chet Haase and
Dmitri Trembovetski
g the basic co
ncepts Part


Is Mergermania Back? Integrating with Eclipse Java Certification
by Jeremy Geelan

Where’s Java Going with 6.0?

An interview with Lee Nackman An introduction
by Naveen Gabrani
.................................24 .................................40
Interview by Bill Dudney


by John Zukowski
.................................6 JAVA NATIVE INTERFACE Private Conversations in Public
Managing Objects Between by Joe Winchester
Java and C .................................50
Take Two Patterns and Building the Java API on top of the native API
Call Me in the Morning by Hari K. Gottipati PRESSROOM

by Ajit Sagar .................................28 Industry News

.................................8 JDJ News Desk
An Introduction to
Mastering Multithreading JSR WATCH
Service Data Objects by Calvin Austin From Within the Java Community
Integrating relational data into Web applications .................................32 Process Program Java Object Serialization
by Kevin Williams and Brent Daniel
.................................10 The ‘wired’ and ‘wireless’ stacks
for Performance
by Onno Kluyt
RECOMMENDATIONS THREAD MANAGEMENT .................................60 by John Cahill
Best Practices for JDBC Java Programming:
Programming The Java Async IO Package @ THE BACKPAGE

Improving maintainability and code quality Fast, scalable IO for sockets and files Java Certification and I JDJ (ISSN#1087-6944) is published monthly (12 times a year) for $69.99 by
SYS-CON Publications, Inc., 135 Chestnut Ridge Road, Montvale, NJ 07645.
Periodicals postage rates are paid at Montvale, NJ 07645 and additional
by Derek C. Ashmore by Mike Edwards and Tim Ellison by William Knight
.................................18 .................................34 .................................62 mailing offices. Postmaster: Send address changes to: JDJ, SYS-CON
Publications, Inc., 135 Chestnut Ridge Road, Montvale, NJ 07645.

www.SYS-CON.com/JDJ October 2004 5


Where’s Java President and CEO:

Going with 6.0?

Fuat Kircaali fuat@sys-con.com
Vice President, Business Development:
John Zukowski Grisha Davida grisha@sys-con.com
Group Publisher:
Jeremy Geelan jeremy@sys-con.com

ith the release of the newly include the Google Web API service (www. Advertising
Senior Vice President, Sales and Marketing:
renamed Java 5.0 J2SE platform, google.com/apis/) in the standard SDK. With Carmen Gonzalez carmen@sys-con.com
it’s time to speculate on just what broadband everywhere by then, develop- Vice President, Sales and Marketing:
might be coming in Java 6.0. Given ers could easily add features like Google’s Miles Silverman miles@sys-con.com
the typical 18–24 month cycle for major J2SE spell-checker, translation options, or just put Advertising Sales Director:
Robyn Forma robyn@sys-con.com
releases, you need to think beyond the norm AdSense in every available Web application. Sales and Marketing Manager:
and not just about new specification releases Viruses and spyware are rather popular Megan Mussa megan@sys-con.com
that require updated versions in the platform. these days. With Sun’s renewed interest in the Associate Sales Managers:
Kristin Kuhnle kristin@sys-con.com
Sun’s Bug Database (http://bugs.sun.com/ desktop, look for new API hooks into the anti- Dorothy Gil dorothy@sys-con.com
bugdatabase/) is a good source for ideas about virus software on the client. Expect the option
the more normal Request for Enhancements to automatically check for viruses on standard Editorial
(RFEs), what’s missing from the current release socket connections. To help virus writers, Executive Editor:
or has been missing in past releases. Since Sun Sun could add a new virus writer/spyware Nancy Valentine nancy@sys-con.com
Associate Editors:
has a top 25 list, we won’t repeat what’s there. toolkit to the platform. This would include Jamie Matusow jamie@sys-con.com
Also, obvious additions/updates such as for features for polymorphic virus generation and Gail Schultz gail@sys-con.com
“new” technologies that Sun is promoting, e.g., keyboard capture. Also, look for an API that Assistant Editor:
Natalie Charters natalie@sys-con.com
the Sun Java Desktop System, will be left off. provides the ability to add users to and remove
Online Editor:
Instead, let’s consider how changes in the users from the operating system and to “su” Martin Wezdecki martin@sys-con.com
world and the Internet will lead to even more over to another user to execute a command
technologies being incorporated into the under the guise of that user. Production
standard Java platform. With Sun and Microsoft settling their legal Production Consultant:
Hopefully, by the time Java 6.0 comes out, the differences, and Microsoft no longer updating Jim Morgan jim@sys-con.com
Lead Designer:
patent on GIF writing will have timed out every- Internet Explorer’s pseudo virtual machine for Tami Beatty-Lima tami@sys-con.com
where in the world and Sun can add this to the Java, Sun could include a pluggable Internet Art Director:
platform. Of course, why anyone would want to Explorer as a full-fledged browser in the JEdi- Alex Botero alex@sys-con.com
write GIFs any more can always be asked. torPane/HTMLEditorKit, reversing the plugin Associate Art Directors:
Louis F. Cuffari louis@sys-con.com
It’s also about time Sun offered full support debate. Richard Silverberg richards@sys-con.com
for PDF files. This is no small feat, as the print- For the low-carb developers out there, Assistant Art Director:
ing API includes a PDF flavor, but no native Sun should delete every deprecated field Andrea Boden andrea@sys-con.com
printing support. This added library would and method. This would lighten up the APIs
need to offer support for parsing, generating, considerably in some areas and remove many Web Services
Vice President, Information Systems:
searching, previewing, printing, encrypting, methods that just redirect calls to different Robert Diamond robert@sys-con.com
and annotating. Form support would also be methods. Of course, this would mean that Web Designers:
beneficial, though that could wait for 7.0. methods like System.getenv(String) couldn’t Stephen Kilmurray stephen@sys-con.com
Matthew Pollotta matthew@sys-con.com
Instant messaging and peer-to-peer net- have been undeprecated in 5.0. But then,
working are very popular these days. Expect getEnv() might have been a better name had
to see APIs like Jabber (www.jabber.org) for Sun not had to keep to the original name when Financial Analyst:
XML streaming protocols and support of the the method got undeprecated. Joan LaRose joan@sys-con.com
up-and-coming Extensible Messaging and For the high-carb developers there, Sun Accounts Payable:
Betty White betty@sys-con.com
Presence Protocol (XMPP) from the Internet should finally add operator overloading. Just
Account Receivable:
Engineering Task Force (IETF) available. look at how Groovy does this at http://groovy. Shannon Rymza shannon@sys-con.com
A new javax.blog package could serve as a codehaus.org/operators.html. Define a method
John Zukowski is hotbed for serving up personal diaries. With like plus() in your object and you can do a + b.
working with SavaJe the help of the XMPP capabilities, support for Define a next() method and use auto increment SYS-CON Events
President, SYS-CON Events:
Technologies on a having everyone’s blogs automatically inter- operators (++). What’s wrong with mapping Grisha Davida grisha@sys-con.com
Java platform for twined by just starting a standard blog server method calls to operators? You should also be National Sales Manager:
mobile phones and could be available. Group blogs would need able to define new operators like ^^ for power Jim Hanchrow jimh@sys-con.com

is wrapping up his to be supported, as well as a handful of Web (since ^ is bitwise-or), like 2^^3 for 8.
latest book Definitive services that, for example, enable Amazon as- With all these things missing from Java for Customer Relations
Circulation Service Coordinators:
Guide to Swing for sociates to maximize revenue options. so long, you wonder how it has even made it Edna Earle Russell edna@sys-con.com
Java 5. Because of Josh Bloch’s huge contribution to version 5.0, let alone to the second release, Linda Lipton linda@sys-con.com
to the Java platform and to maximize his new 1.1. We’ll just have to wait and see what Sun JDJ Store Manager:
Brunilda Staropoli bruni@sys-con.com
jaz@zukowski.net stock options over at Google, Sun should decides to add.

6 October 2004 www.SYS-CON.com/JDJ

Java Enterprise Viewpoint

Take Two Patterns and

Ajit Sagar
Contributing Editor Call Me in the Morning

ife is not easy for today’s enter- and plug-and-play followed soon after. The basic problem is that the guide-
prise application architects. In Although Java is not solely responsible lines and patterns, though invaluable,
today’s IT world, the architect not for this, it has definitely played a big are created outside the application
only has to design solutions for a part in the promotion of these concepts. domain. Although they do address
plethora of interdependent systems (as Add UML and RUP to the mix and you the needs of applications, that need is
is obvious from the job description and not only have the toolkit, but also the addressed across a number of applica-
title), he or she also has to conform to ability to document and manage your tions. After all, it’s the only way that
the ever-evolving standards in a shorter application’s development in a common reuse can be promoted. In this editorial
API life cycle, plan for the not-too-dis- way. we are focusing on the application
tant future, collaborate with business With all these wonderful tools in the architect. The architects in competency
and technical environments, and work architect’s toolkit, why is the job still so centers and standards groups focus
on a feasible roadmap for his or her ap- complex? Given the tools at hand, an ap- on promoting reuse. The focus of the
plication/application portfolio. plication architect should easily be able application architect is on leveraging
In large organizations, standards for to develop applications that leverage: reusable components and leveraging
various facets of business and technol- • Application frameworks documented patterns to solve a busi-
ogy are already laid out and managed • Architecture blueprints ness problem. However, unless a clear
between several competency centers/ • Adaptive architectures path is laid out for navigating through
centers of excellence, and strict gover- • Reference architectures the available choices, he or she may
nance is often in place to ensure con- • Prescriptive architectures easily choose to develop alternatives
sistency throughout the organization. in order to meet the demands of the
To successfully deliver products against These architectures and frameworks application.
hard deadlines, the architects have to have been developed in the software This impasse between the prescrip-
make sure that everything complies in community, as well as within organiza- tion of reuse and its feasibility in the
the governance process. tions, and they facilitate the design of application’s context needs to be
With the advances in software engi- applications from a common base and carefully addressed. Just as the design
neering, especially in component-based building blocks. Apply them to your principles that apply to a broad range
Ajit Sagar is a and service-oriented architectures, application and presto – you have a of applications are made available for
senior technical architect common guidelines have emerged two-minute recipe to create an instant the applications, a guidebook that navi-
with Infosys Technologies, in the form of design patterns. These product. Where’s the catch? gates through these principles should
Ltd., a global consulting design patterns help architects leverage be developed for the application/appli-
and IT services company. what others have learned in their soft- cation portfolio. This guidebook should
He has been working with ware design journey. The Java platform focus on application design, treating
Java since 1997, and has is an obvious example of the application the application as the center of the uni-
more than 15 years’ of design patterns. Before distributed verse. Then clear governance practices
experience in the IT industry. platforms such as Java came along, the should be established around exporting
During this tenure, he number of folks who could spell “design the reusable artifacts, learnings, and
has been a programmer, pattern” was limited to the few who application patterns that emerge out
lead architect, director of had read the Gang-of-Four book, and of each application design cycle. These
engineering, and product that was just a handful of developers in should then be incorporated into the
manager for companies from any organization. With Java – Listeners, rationale for selecting design patterns
15 to 25,000 people in size. Proxies, Observers, Factories, Delegates, published by groups external to the ap-
Ajit has served as JDJ’s J2EE Facades – all these became a part of plication. After all, communication is a
editor, was the founding the designers’ common vocabulary. two-way street. Design patterns are no
editor of XML-Journal, and Reference architectures, frameworks, exception.
has been a frequent speaker

at SYS-CON’s Web Services
Edge series of conferences.
He has published more The basic problem is that the guidelines and patterns,
than 75 articles.
though invaluable, are created outside the application domain”

8 October 2004 www.SYS-CON.com/JDJ

Data Sources

An Introduction to
Service Data Objects by Kevin Williams
and Brent Daniel

Integrating relational data into Web applications

ate last year, IBM Corp., and BEA Each DataObject contains a set of To improve performance, some types
Systems, Inc., introduced Service properties, which can be primitive values of applications can exploit the Data-
Data Objects (SDO), a new data or references to other DataObjects con- Graph’s support for applying multiple
programming specification that tained in the DataGraph. If a DataObject’s updates in one method call to reduce
complements existing Java 2 Enterprise schema is known at development time, the number of connections and/or
Edition technologies and enables ser- a developer can use automated tools to database operations. By storing data
vice-oriented architectures by providing generate typed interfaces that simplify from multiple database rows and tables
uniform data access for a wide variety of DataObject access. Alternatively, the in a DataGraph, applications can make
service and resource types. Not only does application can define the schema changes to the data without making ad-
SDO enable a consistent approach to data at runtime, allowing dynamic access ditional round-trips to the database.
access, but it also provides features that of DataObjects. With either static or
simplify common application tasks, such dynamic access, linked data can be ac- Mediators
as allowing data browsing and update cessed in either a breadth-first or depth- An SDO DataGraph must be popu-
while the application is disconnected first manner. For example, if a DataGraph lated from a data source or a service. The
from the data source. contains customers and related orders, SDO component that populates a Data-
In this article we explain how SDO then orders can be obtained directly Graph is called a data mediator service
works and where it may fit into your own from the DataGraph or from their parent (DMS). A DMS also propagates changes
applications. We also take a closer look customer DataObject. SDO also allows to the in-memory DataGraph back to
at how you can use SDO to retrieve and for accessing data through XML Path the originating data source. Note that
modify data stored in a relational database. Language (XPath) subset expressions. the current SDO specification does not
define a specific API for DMSs – beyond
SDO Concepts Disconnected Programming Model a few basic requirements, each DMS
At the core of SDO are DataGraphs and The DataGraph’s disconnected, data provider is free to design the DMS that
Kevin Williams is a Data Mediator Services (DMS). In simple source–independent nature provides a best suits the associated data source.
software developer and terms, an application program obtains a simple programming model, supports Typically, a DMS accesses a single
is leading the effort to DataGraph from a DMS that is specific to common application patterns, and of- type of data source, for example, JDBC
incorporate SDO some back-end data store. The program fers a potential performance advantage. resources or entity EJBs. All DMSs
technology into the can then examine and/or update the data Today, J2EE application develop- require the developer to provide a de-
WebSphere Application contained in the graph. Optionally the ers have a wide variety of persistence scription of the data to be accessed. This
Server for IBM. program can employ a DMS to propa- frameworks to choose from, such as data description (or metadata) typically
gate the entire set of updates back to the JDBC, EJB, or JDO. These frameworks consists of a schema and a query over
kjwillia@us.ibm.com original data source. have different APIs and are often the associated data source.
complex, requiring developers to spend Figure 1, from the SDO specification,
DataGraph a great deal of time learning multiple illustrates the flow of data during a typi-
As a program object, a DataGraph APIs rather than developing applica- cal interaction between an SDO client
provides an in-memory, nonpersistent tions. Since SDO provides a single data and a DMS. The client makes a request
copy of data. A program can work with access API regardless of the persistence to the DMS to return a DataGraph. The
this data even when there’s no connec- mechanism, developers can choose the DMS reads the requested data from the
tion to the original data source. The data framework that best fits an application data source, constructs a DataGraph
Brent Daniel is a in a DataGraph is organized as a group without using different APIs. of related DataObjects, and returns the
software developer of DataObjects, which may be linked Some developers strive for similar DataGraph to the application. The SDO
with IBM. He currently together (i.e., the data is structured as a independence by developing custom client makes changes to the DataGraph
works on a JDBC “graph”). A DataGraph also contains a Java objects to encapsulate data from in-memory and then sends the modi-
data mediator service schema that describes the structure of the different data sources. This tactic makes fied DataGraph back to the DMS. The
for WebSphere DataObject type(s) contained in the Da- use of the Data Access Object design DMS examines the ChangeSummary
Application Server. taGraph. To handle updates, a DataGraph pattern. SDO inherently supports this contained in the graph and propagates
also maintains a change history to track pattern, freeing developers from the the changes back to the original data
bhdaniel@us.ibm.com all modifications made to the graph. need to develop their own infrastructure. source.

10 October 2004 www.SYS-CON.com/JDJ

������������������ �������������������������������������������������

Data Sources

Because the DataGraph is discon- specifies that the format can be cus- • JDO 2.0: SDO could provide data
nected from the data source, it’s tomized by an XSD. This same format transfer objects from JDO persistent
possible that another application will is used for Java serialization. The se- objects.
update the data (in the data source) rialized form of a DataGraph includes • WEB UI Data Binding: JSF can use
that was used to populate a Data- the DataObjects as well as the schema SDOs as a data binding. JSTL can use
Graph before the application requests and change summary. This capability an SDO DataObject impl that imple-
the DMS to propagate the applica- allows data to be easily transferred ments the map interface.
tion’s changes back to the data source. over the wire as would be required by
To handle such potential update a Web service invocation. Security
conflicts, a DMS typically implements Security is not part of the current
some form of “optimistic” concur- Relationship to Other J2EE Technologies SDO model, so security in an SDO-
rency control and throws an excep- SDO can complement or sim- based application is provided at the
tion to the application when a data plify existing J2EE technologies. SDO edges. For example, if an SDO-based
collision occurs. At that point, it is the complements JDBC by providing a application is developed that employs
application’s responsibility to recover more powerful framework and API an EJB session bean and a JDBC con-
from the collision, for example, by for data access. In a typical relational nection, then security is provided at
rereading the data and restarting the database, data is normalized into the boundaries of the application by
transaction. multiple tables. When this data is read these two J2EE components.
Too-frequent collisions under an using a join query through JDBC, it’s
optimistic concurrency approach returned to the application in a tabu- SDO with JDBC
can degrade performance as well as lar format that includes some data SDO provides a uniform model for
aggravate end users. In applications redundantly (e.g., an order number accessing data from a variety of ser-
where multiple applications will often may be repeated with all individual vices or data sources, including JDBC.
attempt concurrent changes to the line items for the same order). This Figure 2 shows interactions among the
same data, optimistic concurrency format doesn’t directly correspond main artifacts involved when an ap-
control may not be a good choice. to Java’s object-oriented data model plication uses SDO over JDBC. Notice
However, for applications without and can complicate navigation and how the application code calls media-
this behavior, optimistic concurrency update operations. A JDBC DMS can tor and DataGraph methods, while the
control can improve performance by restructure this tabular data into a mediator calls JDBC and DataGraph
reducing lock contention. graph of related DataObjects. For ex- methods, thus insulating the applica-
ample, an order might be represented tion from JDBC.
Metamodel by a DataObject that contains a list There are three central aspects to
The SDO specification assumes the of references to other DataObjects using a JDBC mediator: metadata, con-
presence of a metamodel and meta- containing line-item data. This allows nections, and transaction handling.
data API for the DataGraph, but does an application to use standard Java
not specify one explicitly. Today, SDO techniques to access and modify the Metadata
could be implemented with a variety data. The application must supply the
of metamodels and schema languages Data access via EJBs can also be JDBC DMS with a metadata object
such as XML Schema or the Essential enhanced by using SDO. To imple- that specifies the data to be retrieved.
Meta Object Facility (EMOF). The ment a disconnected Data Access For a JDBC mediator, the metadata
metamodel implementation does not Object design pattern with EJBs contains an augmented relational
affect SDO end users. alone, an application must use some database schema that defines a set of
combination of copy helper objects, tables, their relationships, and selec-
XML Serialization session beans, and EJB access beans. tion and ordering criteria. The JDBC
SDO defines the XML format for An EJB DMS provides a ready-to-use DMS creates a DataGraph that cor-
DataGraphs and DataObjects, and disconnected architecture and frees responds to the provided schema defi-
developers from having to imple- nition. Each DataObject type within
ment their own framework or custom the DataGraph corresponds to a table
artifacts. definition in the schema, and each
SDO could also be used to comple- DataObject property corresponds to a
ment other related technologies. For table column.
read example: The JDBC DMS uses the metadata
Data Data
Client Mediator • JSR 227: Declaratively binding and to generate a SQL Select statement to
update Source
Service accessing data in J2EE applications. retrieve data for the DataGraph. The
SDO could be used as the mecha- simplest metadata example would
nism to return results from the data describe a single table and no selection
source. or ordering. For this specification, the
• JSR 225: XQuery API for Java (XQJ). A JDBC mediator would retrieve all rows
Data Mediator Service could use the of the table and create a DataGraph that
Figure 1 provided API to return SDOs. contains a list of DataObjects, with each

12 October 2004 www.SYS-CON.com/JDJ

Data Sources

DataObject containing the data from by specifying a JDBC Connection object. An Example
one row in the table. Each DataObject The DMS will use this connection for all The following simple example dem-
will have a set of values corresponding database interactions. onstrates JDBC database access with
to the values from each column. SDO employing the JDBC DMS. This
A more complex example might Transactions example has six steps, each illustrated by
involve two related tables; say Custom- As mentioned earlier, SDO provides a a code snippet.
ers and their respective Orders. In this disconnected programming model and,
case, the metadata must specify the accordingly, DMSs will typically connect Step 1: Create the JDBC mediator
relationship between the two tables, to a data store only to read data for metadata instance
which will subsequently be reflected in a graph creation or to write data to reflect Create the metadata instance to rep-
corresponding relationship between two changes back to the store. resent the Customer table. This example
types of DataObjects. When an application requests the demonstrates the creation of the JDBC
The DataGraph returned in this JDBC DMS to retrieve data and produce DMS metadata programmatically. This
case would contain a list of Cus- a DataGraph, the DMS starts a transac- is an obvious candidate for tooling sup-
tomer DataObjects and each of these tion, reads the data, creates the graph, port. Remember that the JDBC DMS uses
Customer DataObjects would have a and ends the transaction. The Data- a simple mapping scheme whereby each
list of related Order DataObjects. The Graph is returned to the application and table definition results in a DataObject
DataGraph will contain all Customers is “disconnected” in the sense that it is type and each table column results in a
and Orders; they are organized as a tree not associated with any connection or DataObject type property (see Listing 1).
with Customers at the “root” of the tree transaction; there are no locks held on
and related Orders branching off of the data. Step 2: Create the DMS instance as in
each Customer. The client can now read data from Listing 2
Applications will frequently want data the DataGraph and make changes to it
only from specified rows of a table. In while it is in memory and disconnected Step 3: Read the DataGraph from the
this case, the metadata for a JDBC DMS from the data source. All changes made database
specifies selection criteria. For example, to the graph are recorded by the Da-
customers might be selected from a taGraph. At some point the client will //Create the “lastName” argument for the
particular zip code or with a particular want to push these changes back to the filter predicate
last name. Also, the DataObjects in the data source and call the JDBC DMS “ap- DataObject arguments = mediator.getParam-
graph can optionally be ordered by plyChanges” API. eterDataObject();
specifying “order by” columns in the As part of the “applyChanges” func- arguments.put("CUSTLASTNAME", “Pavick”);
metadata. tion, the JDBC DMS will reflect to the DataObject graph = mediator.
Normally the JDBC DMS generates data store all changes made to the getGraph(arguments);
SQL select, insert, update, and delete graph as part of a single transaction;
statements to read and update the as- this is true whether there is a single Step 4 : Retrieve data from the graph
sociated relational database. However, change to the graph or an entire batch
an application can optionally provide an of changes. //Iterate through all returned customers
explicit Select statement for the media- The disconnected programming and print the first name
tor to use. If this option is used, the DMS model generally implies the use of an Iterator i = graph.getList("CUSTOMER").
will then generate only the complemen- optimistic concurrency control scheme iterator();
tary insert, update, and delete state- to push changes back to the data store; while (i.hasNext()) {
ments and will use the provided select this is the approach taken by the JDBC DataObject cust = (DataObject) i.next();
statement as is. DMS. System.out.println(cust.getString("CUSTFIR
When the DMS attempts to apply Da- STNAME"));
Connections taGraph changes back to the data store, }
In addition to specifying what data each row being updated is checked to
to retrieve, an application must specify ensure it has not been modified since Step 5: Change the DataGraph
which data source the DMS should ac- it was originally read. If no intervening
cess. For a JDBC DMS, this can be done modifications have taken place, the up- List customers = graph.getList("CUSTOMER");
date proceeds. If a row has been modi- //Get the first customer in the graph and
fied since the data was read, a collision update the name
Application Data
Graph has occurred; the update transaction is DataObject customer =
rolled back and an exception is thrown (DataObject)customers.get(0);
SDO JDBC to the client. customer.setString("CUSTFIRSTNAME",
There is also an option to use the "Kevin");
JDBC DMS within a larger transaction. If this
option is used, the DMS will assume Step 6: Apply DataGraph changes back
that the client is controlling the transac- to the database
tion and will not perform any commit or
Figure 2 rollback operations. mediator.applyChanges(graph);

14 October 2004 www.SYS-CON.com/JDJ

���������� ���������������
�������� �����������

����������������� ����������
�������������������������������������������� �����������������������������������������������
���������������������������������������������� �������������������������������������������������
��������� ��������������������������������������������
�������������������������� ������������� ��������������������������������������������
�������������������������������������������� ������������������������������
���������������������� ��������������������������� �����������������������������������������
����������������������������������������������� �����������������������������������������
��������������������������������������������� ����������������������������������������������
��������������������������������������������������� ����������������������������������������������
������������������������������������������������� �����������������������������������������������
����������������������������������������������� ��������������������������������������������
����������������������������������������������������� ������������������������������������������
������������������� ��������������������������������������������
������������������������������������������� ����������������������������������������
������������������������������������������������ �������������������

Data Sources

Variations on the Example Step 4: Retrieve data from the Conclusion

Metadata File graph In this article we have explored some
In Step 1 we created the mediator of the key SDO concepts and also drilled
metadata programmatically. An alter- //Iterate through all returned customers down into a specific use of the tech-
native is to provide the metadata in and print the first name nology for relational database access
the form of an XML file. Listing 3 is the Iterator i = graph.getCustomers().itera- employing a JDBC Data Mediator.
XML representation of the Customer tor(); SDO is a standard from IBM and BEA
metadata. while (i.hasNext()) { and there is a reference implementation
Using this file, Step 1 would not be Customer cust = (Customer) i.next(); under development at www.eclipse.
necessary and Step 2 would become: System.out.println(cust.getFirstName()); org/emf. This EMF-based implementa-
} tion of SDO will also be delivered with
Step 2: Create the mediator instance as WebSphere Application Server 6.0 and
shown in Listing 4 Paging will be complemented by:
The JDBC Data Mediator Service • JDBC Data Mediator Service
Static Types also provides a paging capability • EJB Data Mediator Service
The example provided above uses that can be useful for marching
the dynamic access APIs of DataOb- through large data sets. A pager It is anticipated that the 6.0 version of
ject. The JDBC DMS also supports the interface provides a cursor-like WebSphere Studio will contain complete
use of static SDO types. To use the next() capability. The next() func- support for creating applications that
static API access to DataObjects, a set tion returns a graph representing leverage SDO; this will include visual
of static types is generated at devel- the next page of data from the entire tooling to configure the JDBC DMS. With
opment time and tools are provided data set specified by the mediator the power of SDO, relational data can be
for this purpose. Using static types metadata; a previous() function is integrated into Web applications more
provides a cleaner user API as well as also available. A CountingPager is easily than ever.
a performance boost at runtime. The also provided that allows the retrieval
generation step is beyond the scope of a specified page from the data set. Acknowledgments
of this article, but this is what Step 4 Listing 5 illustrates paging through a We would like to thank Stephen
looks like when using a static customer large set of customer instances using a Brodsky and Tom Schneider for their
DataObject. Counting Pager. assistance with this article.

Listing 1 sion="2.0" xmlns:xmi="http://www.omg.org/XMI" xmlns:com.ibm.web-

//Create the basic schema description sphere.sdo.mediator.jdbc.metadata="http:///com/ibm/websphere/sdo/
//This describes the physical database table mediator/jdbc/metadata.ecore" rootTable="//@tables.0">
//used to populate the datagraph <tables name="CUSTOMER">
MetadataFactory mFactory = MetadataFactory.eINSTANCE. <primaryKey columns="//@tables.0/@columns.0"/>
Metadata metadata = mFactory.createMetadata(); <columns name="CUSTOMERID"/>
Table custTable = metadata.addTable("CUSTOMER"); <columns name="CUSTFIRSTNAME" type="4" nullable="true"/>
//The generated SQL SELECT will start from the single specified <columns name="CUSTLASTNAME" type="4" nullable="true"/>
//root table <filter predicate="CUSTOMER.CUSTLASTNAME = ?">
custTable.beRoot(); <filterArguments name="CUSTLASTNAME" type="4"/>
Column custID = custTable.addIntegerColumn("CUSTOMERID"); </filter>
custID.setNullable(false); </tables>
custTable.addStringColumn("CUSTFIRSTNAME"); </com.ibm.websphere.sdo.mediator.jdbc.metadata:Metadata>
custTable.setPrimaryKey(custID); Listing 4
//Prior to this point, the application must have
//Add a “lastname” filter to the Customer table descriptor // acquired a JDBC connection for the Mediator to use
Filter filter = mFactory.createFilter(); …
filter.setPredicate("CUSTOMER.CUSTLASTNAME = ?");
FilterArgument arg = mFactory.createFilterArgument(); //Wrap the connection
arg.setName("CUSTLASTNAME"); ConnectionWrapperFactory factory =
arg.setType(Column.STRING); ConnectionWrapperFactory.soleInstance;
filter.getFilterArguments().add(arg); wrapper = factory.createConnectionWrapper(connection);
InputStream stream = new FileInputStream("myMetadata.xmi");
Listing 2 JDBCMediatorFactory fact = MediatorFactoryImpl.soleInstance;
//Prior to this point, the application must have JDBCMediator mediator = fact.createMediator(stream, wrapper);
// acquired a JDBC connection for the mediator to use
… Listing 5
CountingPager pager =
//Wrap the connection. The wrapper indicates whether or not the // PagerFactory.soleInstance.createCountingPager(5);
DMS will actively manage the transaction. The default factory
//method produces an “active” wrapper. Another method is //pro- int count = pager.pageCount(mediator);
vided to create a “passive” wrapper which allows for (int pageNum = 1, pageNum <= count, pageNum++) {
//participation in a larger transaction scope
ConnectionWrapperFactory factory = DataObject graph = pager.page(pageNum, mediator);
wrapper = factory.createConnectionWrapper(connection); //Iterate through all returned customers in this page
Iterator i = graph.getList("CUSTOMER").iterator();
JDBCMediatorFactory fact = MediatorFactoryImpl.soleInstance; while (i.hasNext()) {
JDBCMediator mediator = fact.createMediator(metadata, wrapper); DataObject cust = (DataObject) i.next();
Listing 3 }
<?xml version="1.0" encoding="ASCII"?>
<com.ibm.websphere.sdo.mediator.jdbc.metadata:Metadata xmi:ver- }

16 October 2004 www.SYS-CON.com/JDJ


Best Practices for

JDBC Programming by Derek C. Ashmore

Improving maintainability and code quality

hile many new database Fortunately, all EJB containers and pool. Usually, it doesn’t result in a data-
persistence methods for most servlet engines provide con- base disconnect.
Java programmers have nection pooling features. Connection
been developed in recent pooling provides a way for database Be Diligent About Closing All JDBC Objects
years (e.g., entity beans, JDO, Hiber- connections to be established sepa- This practice is a reiteration of my
nate, and many others), most database rately before your application needs previous article, but not closing JDBC
access code is still native JDBC. This them. They are then used and reused objects after use is the most common
statement doesn’t express a preference, throughout the lifetime of the applica- mistake I see by far. Many developers
just an observation. Reasons for JDBC’s tion. Furthermore, they’re usually not don’t understand that they are supposed
popularity include: (1) it was first, (2) it difficult to configure and use. Listing 1 to close ResultSet, Statement, Prepared-
works, and (3) most developers already contains code that illustrates the use of Statement, and CallableStatement
know it. connection pooling. objects as well as Connection objects.
I first documented “best practices” Many are under the assumption that
for using the JDBC libraries for JDJ closing the connection will cascade into
in April 2000 (Vol. 5, issue 4). For the a close for these other types of objects.
purposes of this article, the “best prac- Some JDBC drivers do, but many don’t.
tices” goals for JDBC programming are Resource leaks caused by not closing
maintainability, portability, and per- JDBC objects are particularly aggravat-
formance. Maintainability refers to the ing because they may not surface until
ease with which developers can under- the code is run under load. In develop-
stand, debug, and modify JDBC code ment, you may not generate enough
that they didn’t write. Portability refers resource leakage to cause a problem.
to the ease with which JDBC code can Furthermore, some JDBC vendors
be used with alternate databases. It Listing 1 assumes the presence of override finalize() to release database
turns out that JDBC does not make a J2EE implementation. Applications resources after objects are garbage
database programming as platform in- without access to J2EE constructs can collected. If leaked objects are properly
dependent as I would like. In addition, implement connection pooling features closed via finalize(), it’s much harder
I consider portability a noble goal even using one of many open source prod- to see the leak in development as the
if you have no current plans to switch ucts. I recommend Commons-DBCP garbage collector corrects the leak.
database vendors. Who knows how from Apache-Jakarta (http://jakarta. Under high load, JDBC objects may not
long your code will be around and what apache.org/commons/dbcp/). There be garbage collected soon enough to
kinds of changes will have to be made is no need for developers to write their avoid exceeding database resources.
to it? Performance refers to optimizing own connection pooling packages these I guard against JDBC resource leaks
the time and/or memory needed to run days. by creating and closing JDBC objects
JDBC code. Connection pooling enhances perfor- within the same method. For example,
mance by reducing the number of physi- the method that creates a connection
Best Practices for JDBC Programming cal database connects and disconnects. will also be the method to close it.
Newer recommendations since Furthermore, it’s common for connec- Furthermore, I close these objects in
my first article on the subject are the tion pooling to have testing features a finally block to ensure that they get
Derek C. Ashmore following. that asynchronously test connections closed in error conditions as well.
is a consultant and before your application needs them. In The reason many developers don’t
the author of the Utilize Connection Pooling Techniques this way, pools provide applications with close JDBC objects in a finally block
J2EE Architect’s Establishing database connec- resilience to database outages. is that it’s programmatically inconve-
Handbook, available tions, depending upon platform, can It’s very important that all connec- nient. The fact that JDBC objects throw
at www.dvtpress.com. take from 30 to 1,000 ms. This can tions created are closed. Note that with a checked exception on close() will
be a meaningful amount of time for connection pooling, issuing a close() cause you to encapsulate the closes in
dashmore@dvt.com many applications if done frequently. merely returns the connection to the try/catch logic nested within the finally

18 October 2004 www.SYS-CON.com/JDJ

block. An example of how to effectively the specifics can differ between data- opposed to a coding recommenda-
close JDBC objects is presented in List- bases. Stored procedures don’t always tion. I usually separate JDBC code
ing 2. perform better than embedded SQL. into separate classes I call data access
To make closing a JDBC object more The soundest advice on this point is objects (DAOs). Data access objects
palatable, I usually create generic to comparatively measure stored pro- manage access to relational databases
“close” utilities, like those I’ve included cedure performance and embedded as well as other types of persistent
in the open source project CementJ SQL for your database. As a general storage.
(http://sourceforge.net/projects/ce- rule, CPU-intensive operations are For convenience, I separate DAO
mentj/), which make this close logic a bad candidates for stored procedures. objects in the package hierarchy (e.g.,
one liner. For example, using a generic For most types of databases, most of com.acme.appname.data or com.
close utility, the close of the Prepared- the performance gain comes from acme.appname.dao). Some devel-
Statement and ResultSet objects in reducing the number of network opers also add a DAO suffix to data
Listing 1 are one line: transmissions, not from being parsed access object names; for example,
and physically stored in the database. a customer DAO might be named
DatabaseUtility.close(results, pStmt); When considering whether or not to CustomerDAO.
make something a stored procedure, I The primary reasons to separate data
CementJ will check for nulls before ask the question: How many network access from the rest of the application
attempting to close. It will also log ex- transmissions will be saved by making is that it’s easier to switch data sources
ceptions received on the close, but not this process a stored procedure? If the and it’s easier to share DAOs between
throw an exception (as there is typically answer is zero, performance will most functions or applications. Medium- to
nothing to be done anyway). Using a likely not be improved. large-sized businesses in particular
generic close utility, Listing 2 can be re- are likely to have multiple applications
written so that it’s considerably shorter Utilize Generated Key Value Retention Feature using the same data access logic. For
and less complex; Listing 2a illustrates One of the classic problems in example, it’s common for an applica-
this. database programming is how to tion to need customer information in
For those who wish to apply this handle generated unique key values. several different functions. The act of
concept but don’t want to add an ad- Most database vendors supply ways to logging into a Web site is different than
ditional product dependency, Listing automatically generate the key value e-mailing a purchase receipt. However,
3 illustrates an implementation for a and make it available when new rows both these processes need customer
generic close utility. are inserted. However, if an applica- information. Separating the access
tion needs that key value for other makes the access easier to reuse.
Check Stored Procedure Use processing, it would have to read the Since JDBC code is usually in
One of the most common questions row just inserted to get the generated separate packages, it’s easier to locate
I’m asked is if SQL should be embed- value. For example, say purchase order should you wish to change database
ded in stored procedures instead of numbers are dynamically generated vendors or support multiple database
the Java source. There’s also a common by the database. A purchasing ap- vendors. As data access code is easier
perception that stored procedures plication would need the generated to find, the separation allows you to
always perform better than SQL state- order number to put on the line items more easily determine the impact of
ments executed directly within Java. associated with that order. database structure changes as well.
Unfortunately, the answers to these The JDBC 3.0 specifies that the
questions aren’t as simple as some JDBC driver should return generated Consider Query Fetch Size for Large Result Sets
would like. values with the insert operation. This This practice is aimed at improv-
There are really two things to means that you don’t have to code ing performance. The fetch size is the
consider with stored procedures: and execute an additional select state- number of rows physically retrieved
portability and performance. From ment to retrieve the key value. Use of from the database at one time by the
a portability standpoint, stored this feature can streamline code, but JDBC driver as you scroll through
procedures written in a proprietary may or may not reduce the over- a query ResultSet with next(). For
language (such as Oracle’s PL/SQL head of issuing an additional select example, you set the query fetch size
or DB2’s SQL Procedure Language) depending upon the specific JDBC to 100. When you retrieve the first
make it much more difficult to mi- driver implementation. A word of cau- row, the JDBC driver retrieves the first
grate the application to another data- tion, some database vendors do not 100 rows (or all of them if fewer than
base should that become a business yet support this feature of the JDBC 100 rows satisfy the query). When you
priority. These proprietary languages specification. retrieve the second row, the JDBC
have unique features that might be To utilize the generated key feature driver merely returns the row from
difficult to replicate in other propri- using JDBC, execute a SQL statement local memory – it doesn’t have to
etary languages. that performs an insert. After execu- retrieve that row from the database.
If you do write stored procedures, tion, issue a getGeneratedKeys() from This feature improves performance by
put them in a nonproprietary language. the Statement or PreparedStatement. reducing the number of calls (which
Some database vendors are supporting The return will be a ResultSet with all are frequently network transmissions)
stored procedures written in Java and generated keys (see Listing 4). to the database.
other third-generation languages. This To set the query fetch size, set the
makes them easier to move. Separate JDBC Code from Business Logic fetchSize field on the Statement (or
As to performance ramifications, This is a design-level practice as PreparedStatement or CallableState-

www.SYS-CON.com/JDJ October 2004 19


ment) before execution. Listing 5 Also, the slower the network, the great- RowSets is premature. As with support
provides an example of setting the er the opportunity for performance for the generated keys feature, I would
query fetch size. (Listings 5–6 can be improvement. expect support for the RowSet feature
downloaded from www.sys-con.com/ to differ among the database vendors.
java/sourcec.cfm.) Changes on the Horizon
As a general rule, setting the query The largest advance in the JDBC 3.0 Summary
fetch size is only effective for large specification is the addition of RowSet We’ve discussed several ways to
result sets. If you set the fetch size support. RowSets are ResultSets that make JDBC code more performant,
much larger than the number of rows eliminate the need for you to explic- maintainable, and portable on an
retrieved, it’s possible that you’ll get a itly declare and use Statements and individual basis. I always recommend
performance decrease, not increase. PreparedStatements for SQL queries. team code reviews and documented
Furthermore, the benefit you get from They were explicitly added to sup- coding standards as ways to develop
increasing the fetch size diminishes the port the JavaBean specification. The more “best practices” and consistent-
higher it’s set. I typically set this value supporting interfaces for RowSets are ly apply existing coding techniques.
to 100 for large result sets. being included in Tiger. Optionally, Furthermore, team code reviews
The performance improvement you can download the JDBC Row- help further the goals of “best practic-
gained from setting the query fetch size Set Implementations 1.0 JWSDP 1.4 es” by improving the maintainability
varies widely depending upon the da- Co-Bundle 1.0 for use with v1.4 of the and general quality of code within an
tabase platform and JDBC driver being JDK. application.
used. I’ve seen performance improve- RowSets can be connected or
ments as large as 50%. Performance in- disconnected. Connected RowSets References
creases vary depending upon the speed maintain an underlying connection to • JDBC Technology Page: http://java.
of the network. Generally, the slower the database. Disconnected RowSets sun.com/products/jdbc/
the network, the more performance allow query results to be serialized and • Commons DBCP – Connection
increases can be gained by manipulat- transmitted between JVMs, possibly on Pooling package: http://jakarta.
ing the fetch size. different servers. An interesting feature apache.org/commons/dbcp/
of disconnected RowSets is that you • JDBC RowSet Implementation
Consider Update Batching can update the RowSet and post those Download: http://java.sun.com/
This practice is aimed at improving changes to the originating database at products/jdbc/download.html
performance. In situations where you a later time. Another interesting feature • JDBC Performance Tips: www.java-
want to issue several inserts, updates, is that RowSets can be serialized as performancetuning.com/tips/jdbc.
or deletes in the same unit of work, XML documents. shtml
update batching allows you to group As this is a new feature, providing • The J2EE Architect’s Handbook: www.
those statements together and transmit “best practices” regarding the use of dvtpress.com/javaarch
them to the database as one set of in-
structions. Like setting the query fetch Core Best Practices
size, update batching works by reduc-
ing the number of network transmis- The practices recommended in my April, 2000 article were the following:
sions between the application and the • Use host variables for literals – avoid hardcoding them: This practice involves using the java.sql.
database. PreparedStatement instead of java.sql.Statement in cases where you need to supply values for
For example, consider a Web site “where” clauses in SQL statements. This eliminates database overhead in figuring out how to
for online sales. When customers cre- access your data. One noted exception is Oracle. Oracle, in recent releases, has tuned Statement
ate orders, they often order multiple processing to the point that it slightly outperforms PreparedStatement processing for small num-
items. When the order is recorded, bers of statement executions.
usually the items on the order are • Always close Statements, PreparedStatements, and connections: This practice involves always clos-
recorded at the same time. Update ing JDBC objects in a finally block to avoid resource limitations found in many databases.
batching allows the multiple inserts • Consolidate formation of SQL statement strings: This practice involves placing the SQL statement
for the order to be transmitted to the text in a field that is declared static final to reduce string processing as well as make SQL state-
database at once. ments easy to identify and read.
Update batching is supported for • Use delegate model for a database connection: This practice involves consolidating any database-
SQL issued via PreparedStatement, specific tuning in a custom implementation of connection so that it’s possible to take advantage
CallableStatement, and Statement of database-specific tuning features without sacrificing too much in the way of portability.
objects. An example of update batching • Use Date, Time, and Timestamp objects as host variables for temporal fields (avoid using strings):
is presented in Listing 6. This practice eliminates conversion overhead in the database and often the application.
As with manipulating the query • Limit use of column functions: This practice makes it easier to switch database vendors.
fetch size, the amount of performance • Always specify a column list with a select statement (avoid “select *”): This practice insulates your
improvement with batching statements code against tuning activities of database administrators.
varies between database vendors. I’ve • Always specify a column list with an insert statement: This practice insulates your code against
seen performance improvements as tuning activities of database administrators.
large as 92% from batching statements.

20 October 2004 www.SYS-CON.com/JDJ

������������������� ������������ ��

Listing 1 Listing 3
public class DatabaseUtility
public void myMethod()
Connection conn = null; public static void close(PreparedStatement pStmt)
try if (pStmt == null) return;
{ try {pStmt.close();}
Context initContext = new InitialContext(); catch (SQLException e)
Context envContext = {
(Context)initContext.lookup("java:/comp/env"); LogManager.getLogger().logWarning("Prepared statement close
error", e);
DataSource ds = }
(DataSource)envContext.lookup("jdbc/ora10g"); }
conn = ds.getConnection();
public static void close(Statement stmt)
// Your application code here
} {
catch (Throwable t) if (stmt == null) return;
{ try {stmt.close();}
// Your error handling code here catch (SQLException e)
t.printStackTrace(); {
} LogManager.getLogger().logWarning("Statement close error",
finally e);
{ }
DatabaseUtility.close(conn); }
public static void close(ResultSet rs)
Listing 2 if (rs == null) return;
public PurchaseOrderVO getPurchaseOrder(int orderNbr) try {rs.close();}
throws SQLException catch (SQLException e)
{ {
PurchaseOrderVO order = null; LogManager.getLogger().logWarning("ResultSet close error",
PreparedStatement pStmt = null; }
ResultSet results = null; }
{ public static void close(Object dbObj)
// Application code here. {
} if (dbObj == null) return;
finally if (dbObj instanceof PreparedStatement) close(
{ (PreparedStatement) dbObj);
if (results != null) else if (dbObj instanceof Statement) close( (Statement)
{ dbObj);
try {results.close();} else if (dbObj instanceof ResultSet) close( (ResultSet)
catch (SQLException s)
// Log warning here. else if (dbObj instanceof CallableStatement) close(
} (CallableStatement) dbObj);
} else if (dbObj instanceof Connection) close( (Connection)
if (pStmt != null) dbObj);
{ else
try {pStmt.close();} throw new IllegalArgumentException(
catch (SQLException s) "Close attempted on unrecognized Database Object!");
// Log warning here.
// Connection purposely not closed –
// managed elsewhere. Listing 4
} private static final String INSERT_STMT =
"insert into PURCHASE_ORDER (CUSTOMER_ID) values (?)";
return order; protected void runTest() throws Exception
} {
PreparedStatement pStmt = null;
Listing 2a ResultSet genKeys = null;
public PurchaseOrderVO getPurchaseOrder(int orderNbr) try
throws SQLException {
{ pStmt = _dbConnection.prepareStatement(INSERT_STMT);
PurchaseOrderVO order = null; pStmt.setString(1, "foo");
PreparedStatement pStmt = null;
ResultSet results = null;
// Generated Key Processing
try genKeys = pStmt.getGeneratedKeys();
{ while (genKeys.next())
// Application code here. {
} System.out.println(genKeys.getString(1));
finally }
// CementJ alternative for close
DatabaseUtility.close(results, pStmt); finally
} {
DatabaseUtility.close(genKeys, pStmt);
return order; }
} }

22 October 2004 www.SYS-CON.com/JDJ

We’d like to think that not all
perfect matches are made in heaven.
Crystal Reports 10
����������������� ������������������ ���������������
�������� �������� ��������



������ ���������



Report Creation
Visual report designer for rapid data access and formatting • • • 1
• 1
• •
Customizable templates for faster, more consistent formatting • • • •
Repository for reuse of common report objects across multiple reports 4
• • •
Data Access
PC -based and Microsoft ® ODBC/OLE DB for MS Access and SQL Server • • • • • •
Enterprise database servers (ODBC, native) • • 1
• 1
• •
Custom, user - defined data through JavaBeans™ • • •
Custom, user - defined data through ADO and .NET • • •
Report Integration
Report viewing APIs (.NET and COM SDKs) • • •
Report viewing APIs ( Java SDK) • • •
Extensive report viewer options ( DHTM L, ActiveX, Java Plug - in, and more) • •
APIs for run -time report creation and modification •
Report Parts for embedding report objects in wireless and portal apps • • • •
Report Deployment
Crystal Reports components for report viewing, printing, and exporting:
a) Java reporting component • • •
b) .NET reporting component • • •
c) COM reporting component • •
Full featured report exporting • • •
Report server (Crystal Enterprise Embedded deployment license) •
1 Limited functionality. 2 Bundled with Microsoft ® Visual Studio ® .NET and Boland ® C#Builder ™.
3 Bundled with BEA WebLogic Workshop™ and Boland® JBuilder ®. 4 This feature is available on the Crystal Enterprise CD, included in the Crystal Reports 10 package.

Perfect matches can be made here too. In order to quickly determine which Crystal Reports® best suits
your project requirements, we’ve provided this basic feature chart. Crystal Reports® 10 simplifies the
process of accessing, formatting, and tightly integrating data into Windows and web applications via
an enhanced designer, flexible data connectivity options, and rich Java™, .NET, and COM SDKs.

To learn more about Crystal Reports 10, compare over 150 different features across versions,
or to access technical resources like the Developer Zone and evaluation downloads, visit:
www.businessobjects.com/dev/p7. To ask more specific report project related questions, contact
an account manager directly at 1-888-333-6007.

Integrating with Eclipse Interview by Bill Dudney

An interview with Lee Nackman

Vice President, Desktop Development Tools
& CTO, Rational Software

n July IBM announced that the some of the standard metamodels, Architects don’t necessarily want to see
Rational tool set would be fully in- like the UML 2.0 model that is also all the detail, however. What support does
tegrated within the Eclipse tool set provided by Eclipse, as a means to the tool set provide for a more abstract
and would provide an integrated provide integration across the tool view of the application?
set of tools to support the full life cycle set. With our current product set you
of software development. Recently I The extensibility features of Eclipse can reverse engineer an entire J2EE
was able to interview Lee Nackman, are also very important for us. Our application and see an abstract view
the CTO of the Rational division of tools are open and have to be ex- of the code. For example, instead
IBM. tensible both by our customers and of seeing the local interface, home
our partners and other third parties. interface, and implementation of
In the press release IBM announced that Eclipse is central to this whole notion an EJB entity, you would see that
the Rational tool set would fully embrace of integration and extensibility. as a single UML class. You can then
Eclipse. What does “fully embrace” mean? create (or view) associations between
The next release (due by year’s It sounds like the model that the analyst is the entities that are converted to EJB
end) will have the ability to address working with is the same model that, for relationships behind the scenes,
several different roles in the software example, the tester is working with; they but as the architect or developer
development process – analysts, ar- just have a different view into the same you don’t have to see all that detail
chitects, developers, and even project information. (or complexity). We do this today in
managers. The development platform Exactly. Let me give you a concrete our shipping product and we will
is an integrated tool set that supports example. When a developer uses deliver more features like this in our
each of these roles and allows them to Eclipse to edit some source code, next release.
work together toward a common goal. assume a method is added. The class
Eclipse becomes the key technology to in the UML model is automatically Often an architect is concerned with an
Bill Dudney, JDJ ’s Eclipse integrate all the tools for each of these updated to reflect these changes. If in- even more abstract view of the code. Is
editor, is a senior consultant roles and for integrating all the capa- stead of editing the code the developer it possible to see the implementations
with Object Systems Group. bilities that are needed across all these added the method to the UML model, patterns like Session Façade, for example,
He has been doing Java roles. For example, requirements the method would be automatically in the model?
development since late 1996 affect each of the roles (analyst, archi- added to the Java code. In essence the Basically yes, but there is no magic
after he downloaded his first tect, designer, developer, and tester), code and the model are basically views here. You can take a big set of code
copy of the JDK. Prior to OSG, Bill and quality affects the people playing into the same information, the same and visualize that. You don’t have to
worked for InLine Software on each of these roles as well. The tools model. This is already being done in see all of it at once either. Various
the UML bridge that tied UML need to integrate very tightly to allow our current product set. aspects of the application can be rep-
Models in Rational Rose and these people to work together, and resented in different diagrams. The
later XMI to the InLine suite of Eclipse is the technology to integrate What about the architect who does not tool will show you the artifacts of your
tools. Prior to getting hooked the tools. For example, we support the want to model all the detail? Is he or she application in UML and allow you to
on Java, he built software on various roles with Eclipse’s perspec- able to work in a disconnected mode, see the relationships that exist. It’s
NeXTStep (precursor to Apple’s tive mechanism, which allows users in where the model is not directly tied to up to the developer, designer, and/or
OS X). Bill has roughly 15 different roles to have a different view the code? architect to discern if a particular pat-
years of distributed software of the tool than users in some other Yes, but one of the things that we tern is correctly applied.
development experience role. think is very important is for the tools
starting at NASA building We think it’s important for the tools to have the ability to analyze the code When you say “not all at once” do you
software to manage the mass to share internal models rather than and let the architect see the architec- mean that the tool will build multiple
properties of the space shuttle. export, import, and transform, which ture as built, as opposed to merely diagrams for you?
You can read his blog at http:// has been the traditional integration specifying the architecture. With our No, the tool will build a model of
jroller.com/page/BillDudney. point behind these tool sets in the tool set the architect can see what is your application and the model will be
past. Consider the Eclipse Modeling actually built and then compare that rich in terms of including a lot of detail
billdudney@sys-con.com Framework (EMF): we use EMF and with what was designed. about the code, relationships, etc., but

24 October 2004 www.SYS-CON.com/JDJ

A free offer for readers of Java Developer’s Journal!
Formula One e.Spreadsheet Engine:
Finally, there’s a supported, Pure Java
tool that merges the power of Excel
spreadsheets and Java applications. Download
1 Automatically generate dynamic this quick-read
Excel reports. No more manual white paper
querying and cutting-and-pasting and trial today!
to create Excel reports!
2 Manage calculations and business
rules on J2EE servers with Excel
files. No more translating Excel
formulas to Java code! Download your trial and test our demos
3 Embed live, Excel-compatible data and sample code. See for yourself how
grids in applets and Java desktop the Formula One e.Spreadsheet Engine
applications. No more static HTML can help your Java application leverage
or presentation-only data grids! the skills of Excel users in your business.



888-884-8665 • www.reportingengines.com

Build reports against JDBC, XML, Java objects, BEA Portal Server logs, BEA
Liquid Data, and other sources visually or with Java code. It’s embedded!
No external report server to set up. Unlimited users and CPUs per license.
Copyright © 2004 ReportingEngines (a division of Actuate Corporation). All rights reserved. Formula One is a registered trademark of Actuate Corporation.
Java and Java-based trademarks and logos are the trademarks or registered trademarks of Sun Microsystems Inc., in the United States and other countries. All other trademarks are property of their respective owners. All specifications subject to change without notice.

the tool will not automatically build more expensive for us to work within the products on each platform that
the interesting diagrams. It’s a manual the open source model because of will prevent us from supporting them
process for the designer to draw the the level of communication that’s re- on every platform.
diagrams. The tool makes it simple quired to make sure that everyone is
though. Since the model exists, it’s on board with whatever changes are On the Model Driven Architecture (MDA)
simply a matter of selecting which ele- being proposed and/or made. That ex- front, where do you see Rational fitting
ments of the model should be shown pense is repaid because of the ecosys- into that market?
on which diagram. Making things look tem that is developing around Eclipse. There are certainly markets where
nice still requires human intervention. There is a large group of vendors build- MDA will be valuable and we think
ing on top of Eclipse. This group helps there is opportunity and value in
Back to the integration provided between to refine and flesh out the extensibility building models and generating
the analyst and the tester. What support features. There is a huge user group code. For example, we’ve seen our
does the tool set provide to allow these that (1) provides usability feedback on customers generate a lot of value
two people to work together? For how Eclipse can be improved and (2) from defining data models and
example, a tester might update a test if someone knows how to use Eclipse generate code from them. Take an
case because a particular condition was they already know a lot about how to XML Schema. Our customers have
incorrect. How does the tool support a use our tool set. been able to achieve value in defi-
change like this getting propagated back The excitement for us is not so ning a model and having the tool
into the use case? much in saving engineering costs by build the XML Schema and database
This is a two-part answer. The using Eclipse but rather the ecosys- schema for them. User interfaces is
first part takes us all the way back to tem that is developing around Eclipse. another place where we have found
requirements. With our ReqPro product This ecosystem together with the modeling to be valuable. Having
the analyst can drop a requirement fact that the technology in Eclipse is our users be able to draw out a user
onto the use case model. The tool will so good is what makes building on interface in a modeling tool and then
automatically create the use case and top of Eclipse so exciting for us. We have the tool generate all the code
then connect the use case back to the don’t need to build a new Java parsing behind that UI is extremely valuable.
requirement through a traceability infrastructure but we also don’t have In our current tools, for example,
link. Now let’s move on to your specific

question. In the same way that the
requirements and use cases can be
tied together with traceability links,
there are ways to connect test cases
The excitement for us is the ecosystem
with use cases. In addition you can that is developing around Eclipse”
connect the test cases with the results
of running the tests. Then the project
manager has insight into the current
state of the project at any time. And the to try to teach our customers how to you can fully define your user inter-
analyst and testers have insight into use the output of that infrastructure face in a visual editor and the tools will
dependencies being changed. Now the nor do we have to invest in educating generate a JavaServer Faces implemen-
integration is not as complete as we’d our partners in how the infrastructure tation behind the scenes.
like it to be, but we are making good works. It’s all a part of Eclipse that is We don’t believe in programming
progress in this next release. widely adopted and documented. We with pictures, however; there are things
can concentrate on building addi- that are best represented in a textural
Moving on to the use of the Eclipse UML tional capabilities on top of Eclipse format.
2.0 model. To what extent are you using for our customers.
this model and to what extent is your Finally how are you integrated with the
team involved in providing feedback to How does the use of Eclipse fit into your Hyades project?
Eclipse? portability picture? Will your tool set be We provide integration with our
We are using the model and strictly reliant on the Eclipse platform so testing tools so that you can deploy
providing feedback to the team. We that they will run anywhere that Eclipse your J2EE application. With the
are continuing to invest very heavily runs? monitoring pieces of Hyades in place
in the combined IBM and Rational In the next release there has been a you can see what is happening on
teams providing quality feedback to big focus on integrating our products the server; for example, if you have
the Eclipse ecosystem. with Eclipse. There are still some cli- a search page that is having perfor-
ents that will not work on all Eclipse- mance problems. With the Hyades
How would you characterize your use supported platforms. The idea going integration the developer is provided
of Eclipse? Has it been an advantage to forward is to make our tools run on with clues that will help nail down
start with the large base of tools already all platforms that are supported by where this problem is happening and
delivered with Eclipse? Eclipse, however, there are still the what needs to be fixed to make the
It’s working quite well for us. It’s practical issues of being able to test problem go away.

26 October 2004 www.SYS-CON.com/JDJ

Java Native Interface

Managing Objects
Between Java and C
by Hari K. Gottipati

Building the Java API on top of the native API

f you’ve ever used JNI, you know API. I then started writing the Java API location. From the memory location
how to manage the primitive data using JNI on top of the C API. The main you can retrieve the C object.
types between Java and the native obstacle I faced was while passing the To maintain the same object in Java
language. As you delve into JNI, objects between Java and C. This article and C, create some object in Java and
particularly when developing a Java API explains how I overcame this obstacle. declare a long variable in it to hold the
on top of a native API, you need to know By the end of this article you’ll be famil- memory location of the object created
how to manage the objects between Java iar with the process of managing objects in C.
and the native language. This article between Java and C. To understand this I’ll illustrate this in detail with the
takes you to the next level and walks article you should be familiar with the mail example. The first part explains
you through the step-by-step process basic concepts in JNI (see the Resources how to fill the Java object from C and
of managing the objects between Java section for the JNI documentation). the second part explains how to get the
and C. C object out of the Java object (to help
you understand, I bifurcated each sec-
Why JNI tion into a Java side and a C side).
Platform independence and flexibility
are the popular buzzwords in the in- Filling a Java Object from a C Object
dustry and programmers want to prune Figure 1 shows the different steps
their code. Everyone wants to program involved in this process.
in their favorite language, unfortunately The following example discusses
you can’t always stick to your selection, these steps in more detail:
as some components of an application • Declare a global long variable:
have to be programmed in a completely
different language or some components Why Do You Need to Manage the public long nativeSession;
are only available in a different lan- Objects Between Java and C?
guage. The solution to these situations If you’re developing the API, some- • Declare a native method:
is JNI. The Java Native Interface (JNI) is times you may need to store an object in
the native programming interface for Java that you created in C; for example, public void native jniLogin(String server,
Java that’s part of the JDK. JNI allows a mail API. First log into the server with String userName, String password);
Java code that runs within a Java Virtual a username and password, then you get
Machine to operate with applications the session. Say you wanted to store that • Define a Java API method to expose
and libraries written in other languages, session in Java for later use, e.g., for read- the native method:
such as C or C++.
Hari K. Gottipati is a ing or writing an e-mail. If you save the
software engineer at In addition, if you want to do some session in Java, you can then pass this public void login(String server, String
system-level programming with Java,
Aligo. He is involved in session to C to read or write e-mail, etc. userName, String password) {
developing a mobile PIM you’ll definitely end up using JNI some- jniLogin(server, userName, String pass-
solution for all known mailwhere. JNI is essential for bridging the Managing the Objects Between word);
and calendar servers thatgap between Java and the native func- Java and C }
can work on any kind oftions. It allows you to write some code You should already know how to pass
device. He is a seven yearin a native language and then talk to the primitive types between Java and C Listing 1 provides a nativeSession
that native code through Java.
Java veteran specializing (otherwise see the Resources section); variable, which will hold the session
in mobile computing. it’s straightforward. Unfortunately object that actually gets created in C
Hari earned his master’s Real-Life Scenario you can’t directly convert the objects through the native method jniLogin.
degree in computer When I wanted to develop a wireless between Java and C like the primitives. Once we create the session object in C,
science from Bharathidasan mail solution for a particular mail serv- You can indirectly store the C object we’ll fill this nativeSession variable that’s
University, India. er, I realized that Java was well suited for memory location in Java through a long declared in Listing 1 with the memory
developing wireless solutions; however, variable. Later, if you want to pass that location of the C object. I’ll discuss this
hari_gottipati@aligo.com that mail server had a C API, not a Java object to C, simply pass the memory in-depth later.

28 October 2004 www.SYS-CON.com/JDJ


Introducing a high-performance database that’s 100% Java.

Berkeley DB Java Edition

Finally there’s a high-performance database that loves Java just as
much as you do: Berkeley DB Java Edition (JE). Brought to you by the
makers of the ubiquitous Berkeley DB, Berkeley DB JE has been written
entirely in Java from the ground up and is tailor-made for today’s
Download at www.sleepycat.com/bdbje
demanding enterprise and service provider applications.

Berkeley DB JE has a unique architecture that’s built for speed. The software executes in the JVM of your application,
with no runtime data translation or mapping required. Plus Berkeley DB JE has been specifically designed to
handle highly concurrent transactions, comfortably managing gigabytes of data. And because it’s built in your
language of choice, your organization enjoys shorter development cycles and accelerated time-to-market.

Experience the outstanding performance of Berkeley DB JE for yourself.

Download Berkeley DB JE today at www.sleepycat.com/bdbje. Register now, and you’ll also receive a 15%
discount on a commercial license purchased before November 30, 2004.
Java Native Interface

(with “this” keyword) to the jniSend-
Platform independence and flexibility are Mail method in Listing 3. Notice
that session object has the nativeSes-
the popular buzzwords in the industry” sion variable that holds the memory
location of the native session object.
With that we can send the e-mail.
I won’t explain the step about creating In line 8 of Listing 2, I’m declaring Let’s see how we can get the native
the header file out of a Java file using the the Session(C) object. In line 14, to session back from this object in the
javah command (see Resources for this connect to a particular mail server, next steps:
information), so I’ll jump directly to the we’re passing the values of the server, • Get the Java object from the
C implementation of this native method. the user name, and the password to parameters:
Let’s see the C implementation of the the connect method. If the given user
native method jniLogin: name and password is valid, the mses- jclass cls = (*env)->GetObjectClass(env,
• Create the native Object: sion object gets filled. Note that the sessionobj);
function “Connect” is already defined
Session msession = NULL; in the C API of the mail server. Now we • Get the long variable from the Java
connect(mserver, muserName, have the session object that logged into object that holds the memory loca-
mpassword, &msession); the server in C. In line 15 we’re getting tion of the native object:
the memory location of the msession
• Get the memory location of the object: object. When you do (jlong)msession, jfieldID fid =(*env)->GetFieldID(env, cls,
it gives the memory location of the "nativeSession","J");
jlong sessionLocation = (jlong)msession; msession object. In line 20 we are get- jlong session = (*env)->GetLon
ting the Java object that called this C gField(env,obj,fid);
• Get the Java object from the function. In line 21, we’re getting the
parameters: long field(variable) “nativeSession” that • Get the native object from that
we declared in Session(Java). In line memory location and reuse it:
jclass cls = (*env)->GetObjectClass(env, 20 we’re setting the long variable with
obj); the variable sessionLocation. Now the Session * msession = (Session)session;
variable nativeSession of Session(Java) sendMail(msession, mto, msub-
• Fill the Java objects long variable with object holds the memory location of ject, mbody);
a memory location: the msession(C) object.
In line 14 of Listing 4, we’re getting
jfieldID fid =(*env)->GetFieldID(env, cls, Getting the C Object Back Out the Session(Java) class. In line 15,
"nativeSession","J"); of the Java Object we’re getting the variable nativeSes-
(*env)->SetLongField(env,obj,f Figure 2 explains the different steps sion. Line 16 provides the value of the
id,sessionLocation); involved in this process. nativeSession variable that has the
The following example provides a memory location of the native session.
clearer picture: Line 18 has the C object from the
Java Side
Define a Java
• Declare the native method with the memory location, since the long vari-
a global
a native
API method to long variable that holds the memory able session holds the memory loca-
long expose the
method native method location of the native object through tion of the native session object. When
the native method: you typecast with (Session)session,
C Side it reads the object from the memory
Get Get the Fill the Java public void native jniSendMail(Object o, location. Now we have the C object.
memory Java object objects long
location of from the variable with String to, String subject, String body); With this in line 19 we are calling the
the object parameters memory location
SendMail function with the values
• Define a Java API method to msession, mto, msubject, and mbody,
Figure 1 Filling a Java object from a C object expose the native method and which sends the e-mail to the address
pass the long variable to the native specified in the field “mto”. Note that
Java Side method: “SendMail” is the function that’s al-
Declare the native method Define a Java API ready defined in the C API of the mail
with the long variable that method to expose the
holds the memory location native method and pass public void sendMail(String to, String sub- server.
of the native object the long variable to the ject, String body) {
through the native method native method
jniSendMail(this, to, subject, body); Resources
C Side } • Java Native Interface tutorial: http://
Get the Java
Get the long variable
from the Java object
Get the native
object from the
object from
the parameter
which holds the memory memory location Let’s say you wanted to send an native1.1/TOC.html
location of the native object and reuse it
e-mail with the session that logged • Java Native Interface Specification:
in through the login method. For that http://java.sun.com/j2se/1.4.2/
Figure 2 Getting the C object back out of the Java object we are passing the session object docs/guide/jni/

30 October 2004 www.SYS-CON.com/JDJ

Listing 1: A Java class with a long variable that will hold the native object’s memory location Listing 4: Reusing the session object that is stored in the long variable of a session class

public class Session { 1:JNIEXPORT void JNICALL Java_Session_jniSendMail (JNIEnv * env,

jobject obj, jobject sessionobj, jstring to, jstring subject,
public long nativeSession;
jstring body) {

public void native jniLogin(String server, String userName, 2: const char * mto;
String password); 3: const char * msubject;
4: const char * mbody;
public void login(String server, String userName, String pass-
word) { 5: jclass cls;
jniLogin(server, userName, String password); 6: jfieldID fid;
} 7: jlong session;

} 8: Session * msession = NULL;

10: mto = (*env)->GetStringUTFChars(env, to, 0);
11: msubject = (*env)->GetStringUTFChars(env, subject, 0);
Listing 2: Native code that fills the long variable of a Session class with native objects memory location
12: mbody = (*env)->GetStringUTFChars(env, body, 0);
1: JNIEXPORT void JNICALL Java_Session_jniLogin (JNIEnv * env, job- 13:
ject obj, jstring server, jstring userName, jstring password) { IO#: HML 007247 REP: Susan
14: cls = (*env)->GetObjectClass(env, sessionobj);
2: const char *mserver; 15: fid =(*env)->GetFieldID(env, cls,"nativeSession","J");
3: const char *muserName; ARTIST: cw 16: session = e157101
ESEND#: V: 1 AD SIZE: 3.5" x 4.75"
(*env)->GetLongField(env,obj,fid); RUN DATE: 2/1/
4: const char *mpassword; 17:
5: jclass cls; Client/Contact: 18: msession = Cost:
Estimated (Session)session; Client Approval (please sign here):
6: jfieldID fid; 19: SendMail(msession, mto, msubject, mbody);

7: jlong sessionLocation; 20:

*Please read this proof carefully and21:
your approval/changes to your Account Executive*
to, mto);
8: Session msession = NULL;
9: 22: (*env)->ReleaseStringUTFChars(env, subject, msubject);
23: (*env)->ReleaseStringUTFChars(env, body, mbody);
10: mserver = (*env)->GetStringUTFChars(env,server,0);
24: }
11: muserName = (*env)->GetStringUTFChars(env,userName,0);
12: mpassword = (*env)->GetStringUTFChars(env,password,0);
14: connect(mserver, muserName, mpassword, &msession);
15: sessionLocation = (jlong)msession;
16: (*env)->ReleaseStringUTFChars(env, server, mserver);
17: (*env)->ReleaseStringUTFChars(env, userName, muserName);
18: (*env)->ReleaseStringUTFChars(env, password, mpassword);
20: cls = (*env)->GetObjectClass(env, obj);
21: fid =(*env)->GetFieldID(env, cls,"nativeSession","J");
22: (*env)->SetLongField(env,obj,fid,sessionLocation); Google, the world leader in large-scale information retrieval, is
23: } looking for experienced software engineers with superb design
and implementation skills and considerable depth and breadth in
the areas of high-performance distributed systems, operating
Listing 3: A Java class with another native method that uses the native session that gets created earlier
systems, data mining, information retrieval, machine learning,
Public class Session {
and/or related areas. If you have a proven track record based on
public long nativeSession; cutting-edge research and/or large-scale systems development
public void login(String server, String userName, String pass-
in these areas, we have plenty of challenging projects for you in
word) { Mountain View, Santa Monica and New York.
jniLogin(server, userName, String password);
Are you excited about the idea of writing software to process a
public void native jniLogin(String server, String userName,
String password);
significant fraction of the world's information in order to make it
easily accessible to a significant fraction of the world's population,
public void sendMail(String to, String subject, String body) {
jniSendMail(this, to, subject, body);
using one of the world's largest Linux clusters? If so, see
} http://www.google.com/cacm. EOE.
public void native jniSendMail(Object o,String to, String subject,
String body);

www.SYS-CON.com/JDJ October 2004 31

Core and Internals Viewpoint

Calvin Austin
Core and Internals Editor Multithreading
ome of you may remember a time Fast forward to J2SE 5.0, which To use my task I just need to pick
when the world of multithreaded should be final by the time you read an ExecutorService. In this example
programming was limited to a this. I’m pleased to report that the Java I chose the cached thread pool,
small set of C or C++ applications. concurrency library is now part of the which will create as many threads
Often the threads were used sparingly Java platform. Does that mean you as I need but will reuse previously
and restricted to a specific task or com- should go back and rework every single constructed threads if available. The
putation or even operating system. piece of threaded code you wrote over logic behind this is that thread cre-
When the Java platform arrived it the last eight years? Not necessarily. ation can be an expensive operation
brought with it the ability for anyone to However, if you are starting from scratch on some systems.
write a fully multithreading program, in- or refactoring some historically tricky I create my Callable task, called
cluding those with very little experience threading code, I would recommend task, and then create a handle to that
in using threads. Even the vendors who spending a little time checking out what task, a Future, that I have named
had the foresight to implement a thread- this library has to offer. result. The Future task serves two
ing library in the OS found they needed Along with synchronization controls purposes. The first is that I can cancel
to fix those libraries in order to let these like semaphores and explicit calls to the task by calling the method result.
new multithreaded Java programs run. create, set, and release locks, there cancel(true). The true argument
The original Java threading model had is a significant upgrade to the thread means that it’s okay to try to cancel an
very basic controls. Synchronized meth- controls. This new functionality is already running task. The second ben-
ods and blocks were there to guard code provided by the ThreadExecutor class. efit is that it’s used to hold the return
and a wait and notify pattern provided You’re probably very aware that when result. To gain access to the return
limited conditional support. Things were you create a runnable task, there is no result, I simply had to call the method
even more confusing when you had a mechanism to return a result, throw an result.get().
thread start method but the suspend, exception to the parent, or even cancel
resume, and stop counterparts were all that task. Where before you may have ExecutorService es = Executors.new-
deprecated! It wasn’t too surprising that used a Runnable task, you can now CachedThreadPool();
talks, books, and articles about using create a Callable one instead. They’re Callable<String> task = new Worker();
Java threads soon became very popular. very similar; the following is a small Future<String> result;
One book in particular stood out, example to show how easy this is. result=es.submit (task);
partly due to the title but primarily Here is my runnable task, called try {
because of the ideas it contained. That Worker. In the Callable version all I System.out.println(result.get());
book was Concurrent Programming in have to do is describe my return type, }catch(Exception e){}
Java: Design Principles and Patterns by in this example a string, and use the
Doug Lea (Addison-Wesley). This book new declaration of Callable that has This small example enabled me to
was different because it looked at de- one method named call instead of use a cached thread pool to hand off
signing true concurrent, multithreaded run. Notice that with Generics we can tasks, check their status, and cancel
programs first, and then applying that to explicitly declare the return type so it them if required. As I mentioned
the Java platform. In the process it un- can be checked at compile time. earlier, I used an ExecutorService that
covered areas where the Java APIs were declares some convenient default
limited. Instead of forcing developers to class Worker implements Callable<String> { implementations. The concurrency
A co-editor of JDJ since work around this restriction, Doug cre- public String call() { library has a full set of interfaces and
June 2004, Calvin Austin ated the Java concurrency API library – a System.out.println("New Worker"); many other implementations that you
is the J2SE 5.0 Specification library that provided higher-level thread return "return result"; can use. I encourage you to check the
Lead at Sun Microsystems. controls, a complement of locks, and } library out along with the other J2SE
He has been with Java finer-grained synchronization tools. } 5.0 features.

Software since 1996 and
is the Specification Lead
for JSR-176, which defines
the J2SE 5.0 (“Tiger”)
I’m pleased to report that the Java concurrency library
release contents.
is now part of the Java platform”

32 October 2004 www.SYS-CON.com/JDJ

Thread Management

Java Programming:
The Java Async IO Package by Mike Edwards
and Tim Ellison

Fast, scalable IO for sockets and files

he Async IO package is designed facilities available to Java applications
to provide fast and scalable through a package that provides IO
input/output (IO) for Java ap- capabilities linked to an asynchronous
plications using sockets and files. style of programming.
It provides an alternative to the original The current version of the Async IO
synchronous IO classes available in the package, com.ibm.io.async, is designed
java.io and java.net packages, where as an extension to the Java 2 Standard
scalability is limited by the inherent Edition 1.4, which can in principle be
“one thread per IO object” design. provided on any hardware and soft-
It also provides an alternative to the ware platform. The platforms currently
New IO package (java.nio), where supported by the package include
performance and scalability are limited Windows, AIX, Linux, and Solaris.
by the polling design of the select()
method. Elements of the Async IO Package
As its name implies, the Async IO The major elements of the Async
package provides asynchronous IO IO package are the classes Async-
operations, where the application FileChannel, AsyncSocketChannel,
requests an IO operation from the sys- and AsyncServerSocketChannel. The
tem, the operation is executed by the channels represent asynchronous
system asynchronously from the ap- many files to serve requests from versions of files, sockets, and server
plication, and the system then informs those clients. The existing standard sockets. These fundamental classes
the application when the operation is Java facilities for handling network are designed to be similar in nam-
complete. The Async IO package sup- connections and files do not serve ing and in operation to the channel
Dr. Mike Edwards is a ports a number of styles of application the needs of server-side applications classes of the New IO package. Good
strategic planner in the programming and gives the applica- adequately. The java.io and java.net news for Java programmers familiar
IBM Java Technologies tion designer considerable freedom packages provide synchronous IO with the New IO package.
group in Hursley, England. in the management of the number of capabilities, which require a one- AsyncFileChannels and AsyncSock-
He is responsible for threads used to handle IO operations thread-per-IO-connection style of etChannels provide asynchronous
technical planning for and also in the design of the compo- design, which limits scalability since read and write methods against the
future IBM products nents that handle the asynchronous running thousands of threads on a underlying file or socket. An asynchro-
including the IBM Java notifications. server imposes significant overhead nous operation is a request to the sys-
SDKs and for Web services– on the operating system. The New tem to perform the operation, where
related products. Before Why Java Applications IO package, java.nio, addresses the the method returns immediately to
working on Async IO, Mike Need the Async IO Package scalability issue of the one-thread- the calling application regardless of
The question “Why do Java applica-
was involved in the per-IO-connection design, but the whether the operation has taken place
tions need the Async IO package?” can
planning of Java SDK 1.4.0 New IO select() mechanism limits or not. Instead of providing a return
be answered in two words: performance
and was a member of the performance. value that gives information about
and scalability.
Expert Group for JSR Current operating systems, such the operation, such as the number of
Performance and scalability are
059, which defined the as Windows, AIX and Linux, provide bytes read/written, asynchronous read
key attributes of the IO system for IO-
specification for J2SE 1.4.0 facilities for fast, scalable IO based on and write operations return objects
intensive applications. IO-intensive
and JSR 051, which created the use of asynchronous notifications that implement the IAsyncFuture
applications are typically, although
the New IO package. Mike of IO operations taking place in the interface.
not exclusively, server-side applica-
received his PhD in Elementary operating system layers. For example, The IAsyncFuture interface is anoth-
tions. Server-side applications are
Particle Physics from Windows and AIX have IO Completion er important component of the Async
characterized by the need to handle
Birmingham University. Ports, while Linux has the sys_epoll IO package. First an IAsyncFuture rep-
many network connections to many facility. The Async IO package aims resents the state of the asynchronous
mike_edwards@uk.ibm.com clients and also by the need to access to make these fast and scalable IO operation – most important, whether

34 October 2004 www.SYS-CON.com/JDJ

the operation has completed or not. the most truly “asynchronous” de- methods such as bytebuffer.asIntBuf-
Second, the IAsyncFuture provides signs would tend to use the callback fer(). ByteBuffers also have a series of
methods that return the result of the method. If an application uses the methods that support the reading and
operation once it has completed. An callback method, the thread that writing of primitive types at arbitrary
IAsyncFuture can throw exceptions requests the IO operation carries on locations in the ByteBuffer using
as well as the normal outcome of the to do more work, while the callback methods like bytebuffer.putLong
operation, if something goes wrong is normally handled on a separate ( index, aLong).
during the operation. thread.
The application uses one of three Simple Examples of Async IO Read
methods to find out whether a particu- Data Formats Supported by and Write Operations
lar operation has completed: Asynchronous Read and Write Operations Listing 1 shows the use of an
• Polling: Calls the isCompleted() The read and write operations AsyncSocketChannel as a client socket
method of the IAsyncFuture, which supplied by the Async IO package use that involves connecting the socket
returns true once the operation is the ByteBuffer class to hold the data. to a remote server and then perform-
complete This class is the same as the one used ing a read operation. In this example, Tim Ellison is a senior
• Blocking: Uses the waitForComple- in the New IO package. One differ- the blocking style is used to wait for software engineer and
tion() method of the IAsyncFuture, ence between the Async IO package asynchronous operations to complete. strategic planner in the
which can be used either to wait for and the New IO package is that the Listing 2 is a program fragment that emerging technologies
a specified period or to wait indefi- ByteBuffers used for the Async IO shows the use of a callback to receive team at IBM Hursley Java
nitely for the operation to complete package must be Direct ByteBuffers. the notification of the completion of Technologies group. He
• Callback: Uses the addComple- Direct ByteBuffers have the memory an asynchronous operation. This frag- has contributed to the
tionListener() method of the for their content allocated in native ment shows just some of the methods implementation of
IAsyncFuture, so the application memory outside the Java Heap. This of a class that is handling socket IO. It’s Smalltalk, IBM VisualAge
can register a method that’s called provides better performance for IO assumed that an AsyncSocketChan- Micro Edition, Eclipse, and
back by the system when the opera- operations since the operating sys- nel has already been opened and the Java SDK over a period
tion completes tem code can access the data in the connected, that a direct ByteBuffer is of 20 years. His interests
buffer memory directly, without the available, and that an object named are in new ways to apply
Which method an application uses need for copying. “state” tracks the state of the IO. object technology to
to find out about the completion of ByteBuffers can be viewed as buf- When the IO operation is requested difficult problems.
an operation is driven by the overall fers supporting other primitive types, (channel.read( ... )) an IAsyncFuture
design of the application, although such as Int, Float, or Char, using is returned. The next step is to give tim_ellison@uk.ibm.com

Technology is hot again. Is your

career? NOW is the time to explore
new opportunities.

Visit Dice.com to find a better job

with better pay. Check your salary.
Compare your skills. Search over
F I N D SOM E T H I NG BE T T E R. 50,000 tech jobs from leading
companies and choose to have
new jobs emailed to you daily.

IT’S TIME for something better.

Visit Dice.com today.

©2004 Dice Inc.

www.SYS-CON.com/JDJ October 2004 35

Thread Management

the IAsyncFuture a callback method by by the AsyncSocketChannelHelper and completion of the IAsyncFuture. If this
calling the addCompletionListener AsyncFileChannelHelper classes. This is time-out expires, control is returned to
( ... ) method. The callback method gets done to keep the interface to the Async- the application, but the IAsyncFuture
called when the operation completes. FileChannel and AsyncSocketChannel is not completed and the underlying
The callback method is the futureCom- classes as straightforward as possible. read/write operation is still underway.
pleted( ... ) method that forms part of Create an AsyncSocketChannelHelper By contrast, if the time-out expires on
a class that implements the IComple- object by wrapping an existing Async- the AsyncChannelHelper read/write
tionListener interface. SocketChannel. An AsyncFileChannel- methods, the IAsyncFuture is com-
In this example, the class with the Helper is created by wrapping an existing pleted (with an AsyncTimeoutExcep-
callback is the same as the class that AsyncFileChannel object. All operations tion) and the underlying operation is
makes the read request (so “this” is on the channel helper object apply to the cancelled.
used as the first parameter in the underlying asynchronous channel. An important point about opera-
addCompletionListener method). The multi read/write operations tions that time out is that the state of
The signature of the futureCompleted take ByteBuffer arrays as input and the channel is left indeterminate. Once
( … ) method is fixed: its parameters return IAsyncMultiFuture objects. an operation is cancelled, it’s unlikely
are an IAsyncFuture object that rep- IAsyncMultiFuture objects differ from that the channel can be used again and
resents the operation and, second, an IAsyncFuture objects only in that they the safe option is for the application to
object that holds the application state, have a getBuffers() method that returns close the channel.
which is associated with the IAsync- the ByteBuffer arrays involved in the
Future through the addCompletion- operation in place of the getBuffer() Asynchronous IO Thread Management
Listener( … ) method where it forms method, which relates to the single If you write an application program
the second parameter (in this example, buffer read/write operations. The that uses the callback method to get
we use the object called “state”). multi read/write operations are useful notifications that asynchronous IO
The futureCompleted( … ) method is for applications that need to send or operations have completed, you need
called when the operation completes. receive data that’s best handled by to understand which Java threads are
It is possible that the operation is com- multiple buffers, perhaps where differ- used to run the callbacks. The threads
plete before the completion listener is ent elements of the data are handled by used to run the callbacks will run
added to the future. If this happens, the different application components (see application code. If your application
futureCompleted( … ) method is called Listing 3). code needs the threads to have any
directly from the addCompletionLis- The time-out operations provided special characteristics, such as specific
tener( … ) method, without any delay. by the AsyncSocketChannelHelper context information or security set-
The futureCompleted( … ) method and AsyncFileChannelHelper classes tings, this could cause problems for
receives the future object relating to are versions of the basic read and your application code unless your
the completed operation, plus the ap- write operations that have a time-out application carefully controls the
plication state object. period applied to them. The basic read actual threads that are used to run the
and write operations of asynchronous callbacks.
Beyond the Basics: Multi Read/Write channels can in principle take forever The threading design of the Async
Operations and Timeouts to complete. This is particularly a IO package is outlined in Figure 1.
The previous sections described the problem for an application that uses Applications make requests to the
basic functions available as part of the the callback technique to get notified package for Async IO operations. The
Java Async IO package. The package also that the operation is complete, since requests are passed to the operat-
supplies more advanced interfaces for the callback might never get called ing system’s IO functions. When the
asynchronous IO. The first advanced if the operation does not complete. operations complete, notifications of
interface supplies the capability to The use of the time-out versions of their completion are passed back to the
perform read and write operations the operations guarantees that the Async IO package and are initially held
using multiple buffers for the data. The IAsyncFuture will complete when the in an IO Completion Queue. The Async
second advanced interface provides time-out expires, even if the underly- IO package has a set of one or more
a time-out on the asynchronous IO ing read/write operation does not Java threads that it uses to process the
operation. complete. If the time-out expires, notifications in the IO Completion
Both the multi read/write operations the IAsyncFuture completes with an Queue. Notifications are taken from the
and the time-out facility are provided AsyncTimeoutException. In addition, Completion Queue, and the IAsyncFu-
the underlying operation is cancelled ture related to the operation is marked
Application Async IO Package (equivalent to invoking the IAsyncFu- as completed. If a Callback Listener has
IO Requests
ture cancel(future) method). been registered on the IAsyncFuture,
IO Completion
Note that using the time-out ver- the Callback Listener method is called.
Completion Queue sions of read and write are different Once the CallBack Listener method fin-
from using the IAsyncFuture wait- ishes, the thread returns to the Async
Thread Pool
ForCompletion( timeout ) method IO package and is used to process other
(see Listing 4). waitForCompletion notifications from the Completion
Figure 1 Async IO Thread Pool provides a time-out for the wait on the Queue.

36 October 2004 www.SYS-CON.com/JDJ

By default, the Async IO package Number of Clients Sync Server New IO Server Async Server
uses its own Result Thread Manager Each client performing 50 overall time/data overall time/data overall time/data
read/write cycles. transfer time transfer time transfer time
to manage the threads that handle
50 357/186 487/180 332/180
the callbacks. It allocates a number of
200 326/178 200/179 211/178
threads, typically equal to the number 400 193/181 201/178 190/178
of processors on the system. These 1000 200/180 433/197 222/186
threads are vanilla Java threads with no 3000 230/180 565/184 217/201
special characteristics. However, the 1,000 active + 3,000
application can control the threads in static 232/187 688/196 230/181
one of two ways. 1,000 active + 6,000
static Failed out of memory 781/269 275/195
The application can override the de-
All times are average microseconds per cycle.
fault Result Thread Manager by calling
the setResultThreadManager(IResult- Table 1 Performance comparison of IO packages
ThreadManager) method of the
Abstract- AsyncChannel class. The Alternatively, the application can Manager. For example, application
application must supply its own use the default IResultThreadMa- data can be attached to the thread or
manager class that implements the nager implementation provided by specific security settings applied to
IResultThreadManager interface, the Async IO package, but control the the thread, or the threads used in the
which defines the full life cycle for nature of the threads used to handle IResultThreadManager can be cached
threads used by the Async IO package. results and callbacks. This is done by the IThreadPool.
The IResultThreadManager interface by supplying the default IResult-
provides control over the policies ap- ThreadManager implementation with Performance
plied to the result threads, including an application-defined IThreadPool Performance is one of the impor-
the timing of creation and destruction, object, by calling the set-ThreadPool( tant reasons for using the Async IO
the minimum and maximum numbers IThreadPool ) method on the IResult- package. How does its performance
of threads, plus the technique used ThreadManager. This allows the stack up against the original synchro-
for creation and destruction of the application to control the nature of nous Java IO and also against the New
threads. the threads used in the Result Thread IO package?

Tools that help you understand and

maintain impossibly large bodies of
source code.

www.SYS-CON.com/JDJ October 2004 37

Thread Management

Performance is a complex issue, but The last two cases involve running Pitfalls to Avoid
a simple test provides some guidance. with a number of inactive client sock- As with the use of any API, there
The test uses Socket IO with multiple ets, which are connected to the server are some aspects of the Async IO API
clients communicating with a single but are not transmitting any data dur- that you need to think about to avoid
server. Each client performs repeated ing the test. This is more typical of a problems.
operations, writing 256 bytes to the real Web server. These inactive sockets You need to be careful with the use
server and reading a 2,048 byte re- are a load for the server to handle of the ByteBuffers that are used in the
sponse from the server. For the test, the alongside the active sockets. read and write methods of asynchro-
clients are always the same code, but This shows the Async IO, New IO, nous channels. Because the IO opera-
three variations of the server code are and Sync servers are all similar in tions occur asynchronously, there is
used: terms of average times in lightly load- the potential for the Async IO package
• Synchronous Server, using the original ed situations. The failure of the Sync to use the ByteBuffers at the same
Java IO classes server to handle the case of 7,000 total time as the application code. The rule
• New IO Server, using the New IO clients shows its limitations in terms to follow in order to avoid trouble is
classes of scalability. The figures for the New that the application code should not
• Asynchronous IO Server, using the IO server show that the performance access the ByteBuffers from the time
Async IO package suffers as the number of clients rise. that an asynchronous read or write
In particular the New IO server shows operation is requested until the point
The server code is as similar as pos- a marked rise in the overhead for that the Async IO package signals that
sible, but the differences implied by starting up new connections as the the operation is complete. Any at-
the different programming models of number of connections rises. The tempt by the application to access the
the IO packages are built into the code. Async IO server manages to achieve ByteBuffers before the operation is
Most notably, the threading design of reasonably stable performance right complete could cause unpredictable
the Synchronous Server is one-thread- through the range tested, both for results.
per-client, while the number of threads startup time and for the read/write Asynchronous channels provide
used for the New IO and Asynchronous cycle time. facilities for the cancellation of
Server is determined by the number of These simple tests show that the asynchronous IO operations. These
processors on the server system and Async IO package is able to deliver include the explicit cancel() method
is much smaller than the number of cli- on its promise of performance and available on the futures returned by
ents. An important feature of the server scalability and can form part of operations on asynchronous chan-
code is that the caches of the common the solution for server applications nels, and also the implicit cancella-
objects used by the server code are intended to handle many thousands tion that takes place as part of the
used as much as possible – notably the of clients. time-out of an IO operation on an
ByteBuffers (in all three cases) and the
threads (in the case of the Sync Server). Listing 1: Basic Use of AsyncSocketChannel
This is done to reduce the startup times /* Create a new AsyncSocketChannel and connect it to a
* given hostname and portnumber.
as much as possible for each socket; *_____________________________________________________
this reflects a common practice for
typical server designs. try {
AsyncSocketChannel aChannel = AsyncSocketChannel.open();
We ran the tests with a Windows InetSocketAddress theAddress = new InetSocketAddress( hostname, portnumber );
2000 single processor server system IAsyncFuture future = aChannel.connect( theAddress );
/* The connect operation is asynchronous
and a Windows Server 2003 four-way * wait for completion on the future returned by the connect
system running the clients, connected * method
via a 100Mb Ethernet network, with future.waitForCompletion();
} catch ( Exception e ) {
varying numbers of client sockets each // Deal with exception while opening and connecting the Channel
performing a connect followed by 50 } // end try block
read/write cycles with the server. The // Perform a read operation
results are shown in Table 1, which
// First set up the Byte Buffer
provides the data for the average time ByteBuffer readbuf = ByteBuffer.allocateDirect( BUFFERSIZE );
in microseconds to complete each
try {
read/write cycle, quoted with and // Request the read operation
IAsyncFuture future = channel.read( readbuf );
without the startup time included. The
startup time is the time taken for the // wait for completion of read, up to 15 seconds
long bytesRead = future.getByteCount( 15000 );
client socket to connect to the server } catch ( AsyncTimeoutException te ) {
before any data is transmitted. // the wait for the read to complete timed out
} catch ( IOException ie ) {
(If you’re surprised that the four-way // an IOException happened when performing the read
server system is used to drive the client } catch ( ClosedChannelException che ) {
// the AsyncSocketChannel was closed before the read
side for this test, it’s used to ensure that } catch ( InterruptedException inte ) {
// the thread was interrupted while waiting
the very large number of clients can be } // end try block
created successfully.)

38 October 2004 www.SYS-CON.com/JDJ

AsyncSocketChannelHelper or ated with setting up and executing providing an event-driven interface
AsyncFileChannelHelper. If an the asynchronous notifications. The for IO operations that is simple to
operation is cancelled, the under- implication of this is that asynchro- use.
lying channel (file or socket) is left nous reads and writes involving very
in an indeterminate state. Because small packets of data (i.e., a few bytes Resources
of this, your application should not only) are going to have a significantly • The C10K Problem Page contains a
attempt to perform any more opera- higher overhead than synchronous comprehensive discussion of the need for
tions on the channel once cancella- equivalents. You should take this into fast, scalable IO for servers and the facili-
tion has occurred. The best thing to account when designing your applica- ties available to provide this on various
do is to close the channel as soon as tion to use Async IO. systems: www.kegel.com/c10k.html
possible. • Pattern-Oriented Software Architecture:
The performance of read and write Summary www.cs.wustl.edu/~schmidt/POSA/
operations using Async IO is designed The Java Async IO package provides • New IO APIs have a full description
to be as close as possible to the per- valuable facilities for fast, scalable of the standard Java New IO package:
formance of equivalent synchronous Socket and File IO, which are an http://java.sun.com/j2se/1.4.2/docs/
IO operations. However, there is some alternative to the use of java.io and guide/nio/index.html
extra overhead involved in running an java.nio facilities in client-side and • Download the Async IO package to try
asynchronous operation compared server-side applications. The package out in your applications: www.alpha-
with a synchronous operation, associ- also assists the program design by works.ibm.com/tech/aio4j

Listing 2: Simple use of Callback with a future ByteBuffer[] bufs = new ByteBuffer[ MAXBUFS ];
public void performRead() { // Create the AsyncSocketChannelHelper
try { AsyncSocketChannelHelper helper = new AsyncSocketChannelHelper( s
// read );
buffer.position( 0 );
buffer.limit( buffer.capacity() ); //Allocate the buffer array
state = READ_PENDING; for ( int i = 0 ; i < MAXBUFS ; i++ ) {
IAsyncFuture future = channel.read( buffer ); bufs[i] = ByteBuffer.allocateDirect( BUFFERSIZE );
// Set up the callback } // end for
future.addCompletionListener( this, state );
} catch (Exception e) { try {
System.out.println("Exception occurred on read request"); // read
// Handle exception... IAsyncMultiFuture future = helper.read( bufs );
} // end try
// Use blocking to wait for completion...
} // end method performRead
long bytesRead = future.getByteCount( );
} catch (Exception e) {
// Callback method dealing with completed operations
System.out.println("Exception occurred on read request");
public void futureCompleted( IAbstractAsyncFuture theFuture,
// Handle exception...
Object userState ) {
} // end try
if ( userState == READ_PENDING ) {
try {
long bytesread = aFuture.getByteCount();
//.... do processing ....
Listing 4: Use of read operation with time-out
// Perform a read operation with a timeout
} catch (Exception e) {
AsyncSocketChannel schannel;
//.... handle exceptions ...
static final int TIMEOUT = 15000; // 15 second timeout
} // end if
AsyncSocketChannelHelper helper = new AsyncSocketChannelHelper(
schannel );
if ( userState == WRITE_PENDING ) {
try {
// Set up the Byte Buffer
long byteswritten = aFuture.getByteCount();
ByteBuffer readbuf = ByteBuffer.allocateDirect( BUFFERSIZE );
//.... do processing ....
} catch (Exception e) {
//.... handle exceptions ... try {

} // Request the read operation

} // end if IAsyncFuture future = helper.read( readbuf, TIMEOUT );

} // end method futureCompleted

// wait for completion of read, or for the timeout to happen
long bytesRead = future.getByteCount( );
} catch ( AsyncTimeoutException ) {
Listing 3: Use of multi read operation // deal with the timeout of the operation
// Async Socket Channel: assume open and connected in earlier } catch ( Exception e ) {
code // deal with the exception...
AsyncSocketChannel s; } // end try block

www.SYS-CON.com/JDJ October 2004 39


Java Certification by Naveen Gabrani

An introduction

ave you looked at the certificate Sun Certified Java Programmer (SCJP) is the prerequisite for most of the other exams. It tests your
that your neighbor has so proudly knowledge of the core Java language. There are three versions of the SCJP exam – Java 1.2, Java 1.4,
displayed in his or her office? Have and an upgrade from Java 1.2 to Java 1.4. Unless you are using AWTs extensively, you should stick to the
you ever wondered if getting certi- SCJP exam for Java 1.4.
fied in Java is worth the time and effort? This
article argues the case for certification and Main Contents Java Fundamentals, Threads, Collections
provides information on how to prepare for Exam Number CX-310-035
URL http://suned.sun.com/US/catalog/courses/CX-310-035.html
the SCJP exam. Prerequisites Nil
In today’s tough job market, a certifica- Exam Fee $150
tion from an established vendor like Sun Exam Type Multiple Choice
can act as one more selling point in your ré- Number of questions 61
Minimum to Pass 32
sumé. All else being equal, most employers
Time Limit 2 hours
will prefer a certified candidate. Certifica-
tion alone does not guarantee a job. But
it acts as a reassurance to a prospective Sun Certified Java Developer (SCJD) is a more advanced exam that covers J2SE (Standard Edition). The exam
employer that the candidate at least under- consists of two steps – a take-home programming assignment and an essay exam on the programming
stands the basics of Java. The certification is assignment. The essay exam is conducted through the Prometric center. The exam requires more time and
especially useful if you are a new graduate money commitment than most of the other Java Certification exams.
or if you’re looking for your first job in Java.
Perhaps even more useful than getting Main Contents Object-Oriented Design, GUI, and Locking
the certificate is the learning involved in Exam Number CX-310-025A (programming assignment) and CX-310-027 (online essay exam)
preparing for the exam. The certification URL http://suned.sun.com/US/certification/java/java_devj2se.html
Prerequisites SCJP
enforces discipline. Most people are too Exam Fee $250 for programming assignment and $150 for the essay exam
busy with their jobs to have time to stay up- Exam Type Programming assignment and essay exam
to-date on the latest technologies. Prepar- Number of questions 4 questions in the essay exam
ing for certification helps bring focus and Minimum to Pass 320 out of 400.
discipline, and lets you update your skills Time Limit 2 hours for essay exam. No time limit for programming assignment.
on a specific technology. The certification
exams from Sun are industry standard and Sun Certified Web Component Developer (SCWCD) exam mainly covers servlets and JSP.
well recognized in the industry.
Sun offers seven Java-related certifica- Main Contents Servlets, JSP, and Design Pattern
tion exams: Exam Number CX-310-081 (programming assignment) and CX-310-027 (online essay exam)
URL http://suned.sun.com/US/catalog/courses/CX-310-081.html
• Sun Certified Java Programmer (SCJP)
Prerequisites SCJP
• Sun Certified Java Developer (SCJD) Exam Fee $150
• Sun Certified Web Component Exam Type Multiple Choice
Developer (SCWCD) Number of questions 69
• Sun Certified Business Component Minimum to Pass 43
Time Limit 135 minutes
Developer (SCBCD)
• Sun Certified Mobile Application
Developer (SCMAD) The Sun Certified Business Component Developer (SCBCD) exam was introduced a year back. It focuses on
• Sun Certified Enterprise Architect various facets of EJBs. The exam consists of multiple-choice questions on designing, developing, testing,
(SCEA) deploying, and integrating Enterprise JavaBeans.
Naveen Gabrani is a • Sun Certified Developer for Java Web
project manager at Services (SCDJWS) Main Contents EJBs
Computer Sciences Exam Number CX-310-090
Corporation (CSC), India. Sun also has another new exam – Web URL http://suned.sun.com/US/certification/java/java_busj23e.html
Prerequisites SCJP
He also runs a Java Services Certification (SCDJWS). This Exam Fee $150
certification site is currently in beta and is likely to be Exam Type Multiple Choice
www.javaprepare.com. launched in the near future. Number of questions 70
A quick overview of each of these exams Minimum to Pass 45
ngabrani@csc.com is provided in the following. Time Limit 120 minutes

40 October 2004 www.SYS-CON.com/JDJ

Wireless Messaging

Sun Certified Mobile Application Developer (SCMAD) exam focuses on the micro edition (J2ME) – Sun’s test is different than Test, hence option
platform for mobile Java technologies. This exam was introduced recently. A is not valid. The constructor must not
return any value, so option C is incorrect. A
Main Contents Wireless Messaging API and Mobile Media APIs constructor cannot be declared abstract or
Exam Number CX-310-110 final. Hence D and E are incorrect answers.
URL http://suned.sun.com/US/catalog/courses/CX-310-110.html Since taking a void argument is illegal in
Prerequisites SCJP
Exam Fee $150 Java, G is also illegal.
Exam Type Multiple Choice
Number of questions 68 Question 2 (Flow control)
Minimum to Pass 38 What is printed when the following gets
Time Limit 150 minutes compiled and run? Select the two correct
Sun Certified Enterprise Architect (SCEA) exam evaluates architect-level skills on the J2EE platform. The exam
has three parts – a multiple choice–based exam (Part 1), a programming assignment (Part 2), and an essay 1. public class example {
type (Part 3) that has follow-up questions on the programming assignment. You must clear all three to be 2. public static void main(String args[]) {
certified. It’s arguably the most complex Java certification exam offered by Sun. 3. int x = 0;
4. if(x > 0) x = 1;
Main Contents J2EE 5.
Exam Number Multiple Choice (CX-310-051), Assignment (CX-310-300A), Essay exam (CX-310-061) 6. switch(x) {
URL http://suned.sun.com/US/certification/java/java_archj2ee.html 7. case 1: System.out.println(1);
Prerequisites None
8. case 0: System.out.println(0);
Exam Fee $150 for part 1, $250 for part 2, $150 for part 3.
Exam Type Multiple Choice, programming assignment and essay type 9. case 2: System.out.println(2);
Number of questions 48 (part 1), 4 (part 3) 10. break;
Minimum to Pass 31 (part 1), 70% for the essay and assignment. 11. case 3: System.out.println(3);
Time Limit 75 minutes (for part 1), 90 minutes (for part 3) 12. default: System.out.println(4);
13. break;
Sun Certified Developer for Java Web Services (SCDJWS) has just been launched by Sun. It covers Java technology 14. }
components related to Web services. 15. }
16. }
Main Contents XML Web Service Standards, SOAP, WSDL, UDDI, JAX-RPC, JAXR
Exam Number CX-310-220 A. 0
URL http://suned.sun.com/US/catalog/courses/CX-310-220.html B. 1
Prerequisites SCJP
Exam Fee $250 for programming assignment and $150 for the essay exam. C. 2
Exam Type Multiple Choice D. 3
Number of questions 69 E. 4
Minimum to Pass 47
Time Limit 150 minutes Answer: A and C. The main method initial-
ized x to 0. The if condition at line 4 returns
Scheduling the SCJP Exam The exam covers some of the facets of false, so x remains zero at the beginning
All the online exams are conducted at Java that are sometimes unknown even of the switch statement. The value of x
the Sylvan Prometric Centers throughout to a seasoned Java programmer. So some matches with case 0. Hence 0 gets printed.
the world. To schedule a test: exam-specific preparation is required even Since there is no break statement after this,
1. Visit http://suned.sun.com/US/ for experienced Java programmers. 2 also get printed. The break statement
catalog/courses/CX-310-035.html. after case 2 leads to the control coming out
Add the product to the shopping cart Sample Questions of the switch statement. Hence 0 and 2 are
and purchase it. Question 1 (Declaration and access control) the only numbers that get printed.
2. Visit www.prometric.com. Locate a Which of the following are valid con-
Prometric site that suits you. After structors within a class Test? Select the two Question 3 (Assertions and exception handling)
this you can schedule an exam correct answers. What happens when the following
through the site. A. test() { } code is compiled and run? Select the one
B. Test() { } correct answer.
Course Contents C. void Test() { }
The exam objectives define the course D. private final Test() { } 1. public class example {
contents for the exam. The topics include E. abstract Test() { } 2. public static void main(String args[]) {
declarations and access control, flow F. Test(Test t) { } 3. for(int i = 1; i < 4; i++)
control, assertions and exception handling, G. Test(void) { } 4. for(int j = 1; j < 4; j++)
garbage collection, language fundamentals, 5. if(i < j)
operators and assignments, overloading, Answer: B and F. A constructor must have 6. assert i!=j : i;
overriding and object orientation, threads, the same name as the class. The name of 7. }
fundamental classes, and collections. the class is Test. Since Java is case sensitive, 8. }

42 October 2004 www.SYS-CON.com/JDJ


A. The class compiles and runs, but does letter, underscore, or a currency symbol. Answer: E. The example is a legal Java class
not print anything. The first character cannot be a digit. Since so A is incorrect. When array is passed as
B. The number 1 gets printed first with – is not a valid character in an identifier an argument, Java treats it as a call by refer-
AssertionError. name, D is incorrect. ence. When the method change_i updates
C. The number 2 gets printed first with i[0], the first element of the array is directly
AssertionError. Question 6 (Language fundamentals) getting modified. This element is set to 4 in
D. The number 3 gets printed first with What happens when the following change_i. This change in the value of i[0] is
AssertionError. program is compiled and executed with available in the main method also. Hence 4
E. The program generates a compilation the command - java test? Select the one gets printed.
error. correct answer.
Question 8 (Operators and assignments)
Answer: A. The if condition returns true, 1. class test { In the following class definition, which
if i is less than j. The assert statement 2. public static void main(String args[]) { is the first line (if any) that causes a compi-
performs a check whether i is equal to j. 3. if(args.length > 0) lation error? Select the one correct answer.
Since i will never be equal to j, the assert 4. System.out.println(args.length);
statement will always return true. Hence 5. } 1. public class test {
the AssertionError does not get gener- 6. } 2. public static void main(String args[]) {
ated. So B, C, and D are incorrect. 3. char c;
A. The program compiles and runs but 4. int i;
Question 4 (Garbage collection) does not print anything. 5. c = 'A';
At what stage in the following method B. The program compiles and runs and 6. i = c;
does the object initially referenced by s prints 0. 7. c = i + 1;
become available for garbage collection? C. The program compiles and runs and 8. c++;
Select the one correct answer. prints 1. 9. }
D. The program compiles and runs and 10. }
1. void method X() { prints 2.
2. String r = new String("abc"); E. The program does not compile. A. The line labeled 5
3. String s = new String("abc"); B. The line labeled 6
4. r = r+1; Answer: A. The above program is a legal C. The line labeled 7
5. r = null; Java file. No arguments are passed to D. The line labeled 8
6. s = s + r; the program, as the command line just E. All the lines are correct and the pro-
7. } says java test. test here is the name of the gram compiles
class. The args array does not contain any
A. Before statement labeled 4 elements. Since args.length is zero, the Answer: C. “A” is a character. So assigning
B. Before statement labeled 5 program does not print anything. “A” to c in statement 5 is perfectly valid.
C. Before statement labeled 6 Assigning a character to integer is also
D. Before statement labeled 7 Question 7 valid and does not require a cast. Thus
E. Never What happens when the following the statement labeled 6 does not generate
program is compiled and run? Select the a compilation error. It’s not possible to
Answer: D. After statement 3, r and one correct answer. assign an integer to a character without a
s are pointing to two different memory cast. Hence the statement 7 generates a
locations both containing “abc”. After 1. public class example { compilation error.
statement 4, the memory location to 2. int i[] = {0};
which r was pointing is available for 3. public static void main(String args[]) Question 9 (Objects)
garbage collection. After statement 6, s { Which of the following are true? Select
now points to a different location. The 4. int i[] = {1}; the three correct answers.
memory block that s was pointing to 5. change_i(i); A. A static method may be invoked before
that contained “abc” is now available for 6. System.out.println(i[0]); even a single instance of the class is
garbage collection. 7. } constructed.
8. public static void change_i(int i[]) { B. A static method cannot access
Question 5 (Language fundamentals) 9. i[0] = 2; nonstatic methods of the class.
Which of these are legal identifiers? 10. i[0] *= 2; C. An abstract modifier can appear before
Select the three correct answers. 11. } a class or a method but not before a
A. number_1 12. } variable.
B. number_a D. The final modifier can appear before
C. $1234 A. The program does not compile. a class or a variable but not before a
D. -volatile B. The program prints 0. method.
C. The program prints 1. E. A synchronized modifier may appear
Answer: A, B, and C. In Java, each char- D. The program prints 2. before a method or a variable but not
acter of an identifier can be either a digit, E. The program prints 4. before a class.

44 October 2004 www.SYS-CON.com/JDJ

Why Settle For
Answer: A, B, and C. A and B are correct statements
about the behavior of static methods. The abstract
forum on Java certifications and other topics:
“Sorta Close?”
modifier may appear before a method or a class, • Javaprepare has two mock exams, a tutorial and
but not before a variable, so C is also correct. A final FAQ on Java certification: www.javaprepare.com
modifier may appear before a method, a variable or • Dan Chisholm’s Web site has high-quality mock
before a class. Hence D is incorrect. A synchronized exams for the SCJP 1.4 exam. It has more than 350
modifier cannot come before a variable so E is also questions. It also has mock exam questions by
incorrect. topic: www.danchisholm.net
• Javacertificate.com has a large pool of questions
Question 10 (Collections) and some are organized by objectives: www.javac-
Which of these are interfaces in the collection ertificate.com
framework? Select the two correct answers.
A. HashMap The three standard books on Java certification are:
B. ArrayList • Mughal, K. A., Rasmussen, R. (2003). A Program-
C. Collection mer’s Guide to Java Certification. Addison Wesley
D. SortedMap Professional.
E. TreeMap • Sierra, K., and Bates, B. (2002). Sun Certified
Programmer & Developer for Java 2 Study Guide.
Answer: C and D. Collection and SortedMap are McGraw-Hill Osborne Media.
examples of interfaces in the collection framework. • Roberts, S., and Heller, P., (2003). Complete Java 2
HashMap. ArrayMap and TreeMap are examples of Certification Study Guide, Fourth Edition. Sybex Inc.
general purpose implementations for some of the
collection interfaces. Any one of these will be sufficient for you to pre-
pare for the exam.
Question 11 (Fundamental classes) Besides the numerous free mock exams available
What gets written on the screen when the follow- on the Web, there are a few commercial exam simula-
ing program is compiled and run? Select the one
correct answer.
tors also available. Some of the main ones are:
• Practice exam offered by Sun: http://suned.sun.
Get The Workflow
1. public class test {
• J@Whiz is a test simulator on the latest pattern of
That Fits
2. public static void main(String args[]) { SCJP2, with 700 questions (12 Tests) at $59.95 or Reactor 5 – the ideal solution for
3. int i; India Rs. 495: www.whizlabs.com
Workflow Automation, Business
4. float f = 2.3f; • JQPlus offers an exam simulator based on the new
Process Integration and
5. double d = 2.7; certification pattern: www.enthuware.com/jqplus
6. i = ((int)Math.ceil(f)) * ((int)Math.round(d)); • JCertify provides an exam simulator that costs
Web Services Orchestration
7. $69.95: http://enterprisedeveloper.com/jcertify
8. System.out.println(i); Fits Within Your Architecture
J2EE-based, XML-driven, platform neutral
9. } Other Java Certifications
10. }
Though Sun’s Java certification is an industry standard, Fits Your Business Requirements
there is another certification exam on Java technology offered
A. 4 The extensibility your developers want, the
by Brainbench (www.brainbench.com/xml/bb/common/test- simplicity your business users demand
B. 5 center/taketest.xml?testId=118) that costs $49.95.
C. 6
D. 6.1 Fits How You Want To Buy
E. 9 Retaking the Exam Flexibly priced, with source code access

In case you fail to clear the exam on the first attempt, Download your free evaluation
Answer: E. The method ceil returns the smallest
you can take the exam again. You would need to buy another
double value equal to a mathematical integer that is copy at www.oakgrovesystems.com,
voucher from Sun. Sun also requires that you wait for two
not less than the argument. Hence ceil(2.3f) returns weeks before retaking the exam.
or contact us at 1.818.440.1234.
3. The round method round returns the integer We can’t wait to help you…
closest to the argument. Hence round(2.7) returns 3.
Multiplying the two numbers returns 9. After Clearing the Exam Declare Your Workflow
Resources for Preparing for SCJP After clearing the exam, Sylvania Prometric will provide you Independence!™
with a score sheet highlighting your scores in various sections.
There are a large number of mock exams available
You should receive a formal certificate from Sun. This may take
on the Web. The links to the best of these is included
8–10 weeks. If you don’t receive the certificate within this time
frame, contact them. You may also check your certification
• Sun’s sample questions for the programmer
results online at www.galton.com/~sun. This site has a data-
exam: http://suned.sun.com/US/certification/
base of all candidates who have cleared Sun’s certifications.
• JavaRanch has extremely popular discussion © 2004 Oak Grove Systems. All rights reserved. All product names are trademarks
or registered trademarks of their respective companies.

www.SYS-CON.com/JDJ October 2004 45


Java Object Serialization

for Performance
Streamline your applications
by John Cahill

Performance is a goal that all Java developers should put Development Scenario
A Case Where Serialization Helped to Enhance Performance
at the head of their design list for any project. To help you I started taking serialization seriously when I wrote a
Swing application that featured some 2D API graphics. The
achieve this goal, there is a powerful mechanism that’s used application allowed users to view flat, wire-frame abstractions
of vehicles and to pick which part of the vehicle they wanted
in just about every commercial Java product that most information on. The application used animation, showing
vehicle wire frame drawings, which were JPEG images. So
developers are not aware of. This mechanism is called when a mouse pointer was moved across the active pane, the
application highlighted specific areas of the vehicle image.
serialization. To set up this feature I first had to map the vehicle wire-

frame image “hot spots” and save the polygon points (x:y coor-
his article discusses Java object serialization and dinates) to a file. There were a lot of polygons in each drawing,
presents it as a possible performance-enhancing and a lot of x:y coordinates to contend with. When the program
tool for Java systems. We’ll discuss what serializa- started, I found that reading in all of this static image setup data
tion is and review a real-world case study where was a big performance hit. The images would take a few sec-
serialization was used to enhance the perfor- onds to load, mainly because of reading in the image hot spot
mance of an application. (The source code for this article data. Since the image data never changed, serialization seemed
can be downloaded from www.sys-con.com/java/sourcec. like a good way to minimize this bottleneck. It turned out to be
cfm.) an excellent solution. Now the application uses a serialized col-
lection of polygon objects for each type of image, and the image
What Is Serialization? information loads immediately. If your application uses large
Serialization has been around for a while. Simply put, amounts of static data, serialization might be the way to go.
it’s a transmission and storage mechanism for Java ob-
jects. It was originally invented for use in RMI (Remote How to Serialize Objects
Method Invocation) so that Java objects could be reliably The Basics, Part I: Serialization
moved between servers. It’s also used in many other Java After I realized that I had a performance problem, I needed
systems, like JMS ( Java Message Service), as a persis- to give my polygon collection the ability to serialize itself.
tence mechanism for message queue objects. While many Java objects are serializable, this is only the start-
These days the Java community largely ignores serial- ing point for our serialization adventure.
ization. This is mainly due to long-term object storage is- First some bad news: the Java language gives developers
sues and the fact that the serialized object is written out a small set of tools to perform object serialization. Serializa-
John Cahill currently works in native bytes. However, if you are aware of these issues, tion is not inherently a part of the Java language as java.lang.
as a core software engineer serialization can be an excellent solution to certain types Object contains no serialization methods. Now some good
for Intuit Eclipse in Boulder. of performance bottlenecks, if used wisely. news: basically, object serialization is quite simple. There are
He has developed software Serialization is a tricky process for the Java platform only three steps to performing simple object serialization.
in Java since 1995, and since an object can be a composite of other objects.
designed effective object- For serialization to work correctly, all objects that are Step 1: Create an output file to hold the contents of the
oriented solutions for a contained by the object you want to serialize must serialization stream. The name of the file doesn’t
variety of businesses. implement the serializable interface. The serializable matter since “the stream is the thing” (to para-
He enjoys the Colorado interface has no methods that you’re required to phrase Shakespeare). It is, however, customary to
lifestyle with his wife, Holly. implement, as it’s a marker interface used by the name the file the same as the class name, with an
Java Virtual Machine to identify objects that can be extension of “.ser”. This is the convention used in
john_cahill_boulder@msn.com serialized. most of the Java platforms that use serialization.

46 October 2004 www.SYS-CON.com/JDJ

Step 2: Create an ObjectOutputStream. This is a special type Speaking of XML, Java now gives you the choice to
of OutputStream that performs the work of transform- serialize your objects into XML rather than nonportable
ing the object into a stream of bytes that is specific to byte streams. See http://java.sun.com/j2se/1.4.2/docs/
the Java serialization standard. api/java/beans/XMLDecoder.html for information on this
class, which can be used as a functional replacement for
Step 3: Write the stream out and close the stream. ObjectInputStream. The complimentary object is called
In real-word systems it would be inconvenient to have
serialization as part of an application framework. You could Serialization Pitfalls
do this, but we’ll just use an abstract base class that supports All of this sounds great, but there are some practical
serialization as in Listing 1. downsides. The first pitfall is that serialization is a poor
mechanism for the long-term storage of objects and
The Basics, Part II: Deserialization their data. Why? It’s quite simple. If your program relies
After I created the SerializableObject base class for my on a serialized object, and the object changes (methods
polygon collection, I needed some way for an application or fields deleted), you cannot deserialize that object
to read the serialized object from the file system. Whatever anymore.
you serialize, you must be able to deserialize. One obvious way around this is to never change that
We will not give our SerializableObject class the ability object. In practice, this is easier said than done, especially
to deserialize itself. If the user creates the object to de- if another development team inherits the application. Ac-
serialize another instance of the same object, what cording to the serialization specification, there are both ac-
would be the point? It would be wasteful. The only way ceptable and unacceptable changes to a class. Simply put,
to make the method work would be to make the method you can add methods and fields to a class, but you cannot
static, which wouldn’t work since it would be necessary remove fields and methods and still expect serialization
to have access to the “this” reference, which static to work. Some methods to avoid this serialization pitfall
methods cannot use. might include:
For the developer, deserialization can be broken down • Serialize Java utility objects, like Properties, Hash-
into the following programmatic tasks (see Listing 2). tables, etc. Still, Sun Microsystems could change the
(Listings 2–3 can be downloaded from www.sys-con.com/ implementations of these objects and then your
java/sourcec.cfm.) code would not be able to run in a new version of
• Locate the serialized file of the object you are trying to the JDK.
• Get an InputStream to the file.
• Create an ObjectInputStream given the InputStream you
have obtained in the previous step.
• Read in the object from ObjectInput.readObject(), per-
forming the cast to the type of object and watching out
for exceptions.

Using the Transient Keyword to Your Advantage

Let’s say your object graph contains time/date
sensitive info, or nonsecure items like database URLs or
application user information. If this is the case, declare
the fields in question to be transient. The Java serializa-
tion mechanism will exclude transient fields from the
serialization process. When these fields are deserial-
ized, they have a null value, if they are objects. The tricky
bit here is to code your class in such a way that it is not
critically dependent upon transient fields. I’d suggest
that if your serialization candidate object contains lots of
transient data, don’t consider the object as a candidate for

Serializable vs Externalizable
Java provides a wonderful option to basic serialization.
If you don’t want your objects represented as a byte
array, you should consider externalizable. The main dif-
ference between serializable and externalizable is that
externalizable objects must provide instructions on how
they are to be serialized and deserialized. This is a natural
if you want absolute control over how your objects are
represented in serialized form. This is also useful if you
want to transform your objects to an abstract representa-
tion, like XML.

www.SYS-CON.com/JDJ October 2004 47


• Version your objects with a unique numerical ID that can more details). Note that this system is not part of the
be read during serialization/deserialization. Sun does this original serialization system. JSR 57 addresses long-
with SDK objects that can be serialized and ships a tool term storage of JavaBeans and specifies how to serial-
called serialver that generates serialization IDs for classes. ize objects to abstract object representations (like XML),
The ID is a hash of the class attributes (like class name, rather than byte streams. Also, the serialization system
method count, etc.), which is a unique fingerprint for for Java was designed so that different byte encodings
that class. Therefore, it’s possible to compare the UID of a could be used, since these encodings on other platforms
required object (referring to the required implementation) could differ.
to ensure that it agrees with the serialized ID of the dese-
rialized object. This approach is good to use if you plan to Configuration Management Issues
use externalizable object serialization and is not required After reading all this information, you have decided
of serializable objects. whether to actually store serialized objects in resource JARs
• Late binding of serialized objects is the best practice. (or on servers, where they can be accessed via URLs). If you
In other words, don’t store the serialized object in a JAR do this, remember to play nice with your local configuration
file where people will forget about it. Have the applica- management specialist. For CM people and anyone else who
tion do the serialization work. That way the first time the is responsible for installing and migrating applications, seri-
app is run, the heavyweight data object is loaded, then alized objects represent an odd problem: serialized objects
serialized. On subsequent application runs, the applica- are not any of the following:
tion will look for serialized instances of the heavyweight • Source code
data object. Also, consider using a class that implements • Application properties
the Factory pattern. That way the Factory can make an • Compiled code
informed decision whether or not to use the serialized
instance of the class. If you’re not willing to take the “first I would like to offer this bit of advice: if you decide to
time” performance hit, I suggest you consider generating include serialized objects as an application resource, they
the serialized objects at project build time. This way, the should be generated at build time. For this scenario I would
serialized objects can be generated and stored in a JAR at recommend that you get involved with creating a custom Ant
build time and will be valid for the lifetime of the com- target that creates, serializes, and packages your serialized
piled project codebase. This involves a little more work, objects.
though, because you have to build a “builder” for your Again, the biggest pitfall here, and it is a significant one, is
serialized objects that can be called from Ant. guarding against objects going out of date, or if the code base
has been repackaged.
Setting Up a Framework for Serialization The other thing to be aware of is the practical impossibil-
Once I had gotten this far in my project, I took a step ity of building on one platform and deploying on another.
back to review my work. I had accomplished a very useful Since the serialized object stream uses the native platform
thing, but I was still dissatisfied. I had the polygon collec- byte representation, it would be unwise to build your code
tion deciding if it should load the list of x:y coordinates on Windows (ASCII) when you intend to use the serialized
from a file or use the serialized version of itself. This was class on an AS/400 (EBCDIC). This is why most serialization
a tad inefficient, to say the least. I decided to delegate this strategies used in other Java platforms serialize objects only
work to a singleton that controlled access to my polygon when they are created/received on the platform that the
collection object. code is running on.
Since late binding of serialized objects will keep you out
of some types of serialization pitfalls, let’s talk about how to Summary
control serialization in your application. This type of control I hope I have shown that serialization is a powerful tool with
is necessary because if you aren’t careful, object serialization a few manageable limitations. I believe that it can be used in
can turn out to be a nuisance. a variety of development scenarios, where object creation at
Setting up a simple object factory is a good way to control runtime represents a performance problem. If you understand
access to your object and allow you to decide when it is the limitations of serialization, using this tool can be a power-
appropriate to use the serialized version of the object in ful ally that will help to streamline your applications.
question. The other thing that makes this solution powerful
is that you avoid the long-term storage issue of your object. Acknowledgment
If your application is performing serialization at runtime, Thanks to Chris Merrion of Intuit Eclipse for proofreading
the serialized object is never out of date. In Listing 3, a assistance.
mechanism (like using serial UIDs) could be built in so that
the object in question is only serialized where appropriate. Resources
When the object changes, the serialized object could be • JSR-57: www.jcp.org/en/jsr/detail?id=57
deserialized. • Java Object Serialization Specification: http://java.sun.
Some Serialization Trivia html
Sun Microsystems has a serialization system used • Ant Programming – see the “Writing your own task” section:
for JavaBeans that is part of the 1.4 SDK (see JSR 57 for http://ant.apache.org/manual/index.html

48 October 2004 www.SYS-CON.com/JDJ

Listing 1 catch (IOException ex)
package com.cathail.serialization; {
import java.io.File; }
import java.io.ObjectOutputStream; finally
import java.io.FileOutputStream; {
import java.io.IOException; try
import java.io.Serializable; {
import java.io.NotSerializableException; if (os != null)
import java.io.FileNotFoundException; {
public abstract class SerializableObject implements Serializable }
{ }
catch (IOException ex)
private String pathToSerializeTo = null;
* a basic, no frills serialization method. Note that
* it is customary to use ".ser" as a file extension
* for the serialized object filename
/** this method will set the output path so that
* when serialization takes place, the output will go to
public void serialize() ARTIST: cw ESEND#: e157101 V: 1 AD SIZE: 3.5" x 4.75" RUN DATE
* a specific directory.
ObjectOutputStream os = null;
Client/Contact: * @param path - the
Estimated path to the serialized
Cost: output (please sign here):
Client Approval
public void setSerializationPath(String path)
*Please read this proof carefully and forward your approval/changes to your Account Executive*
/* set the output path for the object, if one has this.pathToSerializeTo = path;
been provided... }
if one has not been provided, just go without
it... }
String outPath = ((this.pathToSerializeTo == null) ?
this.getClass().getName() :
this.pathToSerializeTo +
File.separatorChar +

File serializedObjectFile = new File(outPath +


os = new ObjectOutputStream(new FileOutputStream(seri


/* Google, the world leader in large-scale information retrieval, is

also, you can encrypt the bytes coming off looking for experienced software engineers with superb design
of the stream for added security!
and implementation skills and considerable depth and breadth in
the areas of high-performance distributed systems, operating
os.writeObject(this); systems, data mining, information retrieval, machine learning,
catch (NotSerializableException ex)
and/or related areas. If you have a proven track record based on
{ cutting-edge research and/or large-scale systems development
/* it is important to catch this exception, rather in these areas, we have plenty of challenging projects for you in
* than just catching IOException, because even
Mountain View, Santa Monica and New York.
* this base class implements Serializable, it is
possible Are you excited about the idea of writing software to process a
* to construct an object graph that contains non-
significant fraction of the world's information in order to make it
* objects, which would cause us some problems... easily accessible to a significant fraction of the world's population,
*/ using one of the world's largest Linux clusters? If so, see
http://www.google.com/cacm. EOE.
catch (FileNotFoundException ex)


www.SYS-CON.com/JDJ October 2004 49

Desktop Java Viewpoint

Private Conversations
Joe Winchester
Desktop Java Editor in Public

ne of the principles of any OO * checking when it is used. A value of try{
language such as Java is an “false” indicates fooBarField.set(object,Boolean.FALSE);
object’s ability to encapsulate * that the reflected object should } catch (Exception exc){
its data and provide clients with enforce Java language access checks. }
a specific and well-defined API. This is
done through the visibility keywords To best way to illustrate this is to take We’re using the method setAc-
public, protected, and private. The use class A, which has a private method cessible(boolean) as it was designed
of these is one of the first things any Java isFooBar(); to allow reflection to ignore any
programmer learns; the fairly well-un- visibility constraints; however, the
derstood key points being that a public public class A{ setAccessible(boolean) method does go
accessor (be it a method or a field) is vis- private boolean fooBar; through the security manager, which
ible to any other object, protected only private boolean isFooBar(){ throws a SecurityException if this kind
to subclasses and private to no one but return fooBar; of private invocation is now allowed.
the class itself. } The default JRE security manager does
A not-uncommon scenario, however, } not prohibit the accessibility being
is that when you’re using a class, you find overridden, and if you wanted to check
a gem of a method you want to call that’s your JVM to see whether or not this
been marked as private, or you want to technique was going to work, the code
access or modify a field that isn’t visible would be:
to your class. I find this occurs quite a
bit with frameworks where you might try{
be extending or calling someone else’s System.getSecurityManager().
code and it works fairly well except for checkPermission(new ReflectPermission(""));
one little twist that you need to complete } catch (SecurityPermission exc){
your task. There is a way (that I describe // VM not going to let you do funky reflec-
below) that allows you to access private Using reflection we can get the tion
methods and fields and it’s a useful tech- method and execute it as follows: }
nique that, in these situations, enables
you to code your way out of a blind alley. A object = new A(); I don’t recommend using reflection
The secret lies in the java.lang.reflect Method fooBarMethod = A.class.getDeclaredMet and accessibility as a matter of normal
package that lets you execute Java code hod(“isFooBar”); programming, but when you’re in a tight
reflectively. Typically this is used for such fooBarMethod.setAccessible(true); spot with a deadline breathing down
tasks as looking up a field, method, or try{ your neck and you can see the API you
constructor by name and then access- System.out.println(“FooBar=” + fooBar- need but it’s just not there, it can be a
ing it for a given object. It’s very useful if Method.invoke(object); lifesaver. You’ll be linking to private field
your program has to consume objects at } catch (Exception exc){ names and methods though so the next
runtime that it has no prior knowledge } version of the framework or class library
of at compile time (like introspection, for you’re abusing might very well change
example). The three classes – java.lang. The key is the setAccessible(true) and break your code. You won’t pick up
reflect.Method, Field, and Constructor statement. If this weren’t present, a java. this breaking change at compile time
– however, all inherit from the java.lang. lang.IllegalAccessException would be because it’s reflective and will only die at
AccesibleObject. This has the method thrown. With the accessible set to true runtime.
setAccessible(boolean) whose method the code to execute the method and get It’s very interesting that this month
comment is: the result can be in any class in the JVM. we have a great article on Java 2D and
Joe Winchester is a In addition to calling private methods, gaming, and last month we published
software developer * Set the “accessible” flag for this we can also set field values; the following a fantastic piece on Java3D using Mars
working on WebSphere object to code sets it to false: rover images as examples. I hope this is
development tools for * the indicated boolean value. A value of a renaissance in both frameworks and
IBM in Hursley, UK. “true” indicates that Field fooBarField = A.class.getDeclaredFiel we see more interesting uses in which
* the reflected object should suppress d(“fooBar”); Java apps can find their way onto the
joewinchester@sys-con.com Java language access fooBarField.setAccessible(true); desktop.

50 October 2004 www.SYS-CON.com/JDJ


Par ersta
t1 nd ing
ic c

by Chet Haase
and Dmitri Trembovetski

t JavaOne 2004 we gave a presentation on This Runnable could be used when spawning a dedicated
Java game development that included general
Thread t = new Thread(new GameLoop());
framework information and tips and tricks t.start();

on using the media APIs effectively. We also showed an …and you’re off and running. Now you just have to imple-
ment the methods in run() and you’re all done. Right?
application named “Ping” that demonstrated some of the Although the loop above is pretty simplistic, it covers the
basics for many types of games and it’s what we’ll use for the
ideas we discussed (see Figure 1). purposes of our discussion. Let’s see what the details are in
the methods called inside that game loop.
Working code is a great way to illustrate an API, and we
have posted the source code that accompanied our presenta- syncFrameRate()
tion on http://ping.dev.java.net. This article fully explains At the start of any frame, you should figure out the timing
the ideas behind our JavaOne presentation as well as the information. Many games have been written that assume
Chet Haase is an engineer in elements of interest in the Ping application. some kind of constant frame rate, or constant movement per
the Java 2D team at Sun This two-part article is divided into three main sections: frame. If you actually have a constant frame rate this may
Microsystems, Inc., where he 1. Game framework work fine, but in this world of arbitrarily fast or slow comput-
focuses on graphics rendering 2. 2D rendering specifics ers, different performance – based on machine configuration
and performance issues for the 3. Ping: the demo and load and per-frame variability in terms of scene com-
Microsoft Windows platform. plexity and rendering time – makes it suicidal to count on a
Catch his blogs and articles at We will cover the first topic here and save the rest for Part 2. constant frame rate. Instead, your game should figure out the
http://javadesktop.org. per-frame movement based on how much time has actually
Game Framework passed. For example, don’t assume that your characters move
chet.haase@sun.com Usually a game will want to spin off a separate thread one unit per frame, but rather one unit per time unit; then
to handle all of the main work of the per-frame game loop. no matter how much time passes between one frame and
This doesn’t mean that all processing for the entire ap- the next, the movement will look constant to the user of your
plication happens on this thread (some things, like events, program.
are inherently processed on different threads), but that
one single thread will be used to synchronize and serialize
all of the actions that must occur in every frame.
For example, a simple game loop might be:
Dmitri Trembovetski is an
engineer in the Java 2D team public class GameLoop implements Runnable {
at Sun Microsystems, Inc., public void run() {
where he focuses on graphics while (true) {
rendering and performance syncFrameRate();
issues for the Solaris and Linux gatherInput();
platforms. Read his frequent updateObjects();
posts on the forums at render();
http://javagaming.org. }
} Figure 1 Ping demo: Balls, paddles; what more could anyone want?
dmitri.trembovetski@sun.com } Source code available at http://ping.dev.java.net

52 October 2004 www.SYS-CON.com/JDJ

syncFrameRate() is an attempt to boil down this idea into Let’s analyze some of the methods used in the code.
some simple code that does two things: Line 3: System.nanoTime() is a new method in JDK 5.0 that
1. Tries to regulate the frame rate to some configurable speed. finally enables the use of high-resolution timers in the Java
2. Calculates the actual time passed since the last frame. This core classes. The old System.currentTimeMillis() call that
elapsed time will be used later in object movement calculations. applications typically use is sufficient for most static or low
frame-rate situations, but games usually require a finer degree
Frame Rate Consistency of control than higher-resolution timers can provide. In recent
Of course you could always run flat-out as fast as you can simple timing tests on Windows machines I found System.
and just use the elapsed frame times to make sure the object currentTimeMillis() to have a minimum resolution of 10–15
movement is realistic. However, you may then end up spending milliseconds, whereas System.nanoTime() had a much better
way too much effort just getting through your game loop at that resolution of only one millisecond or so.
speed and not leaving enough CPU cycles for other things that Line 5: Thread.yield() ensures that we cede CPU time to
your application (or your machine) might want to do instead. other waiting threads every time around the timing synchro-
For example, if events are getting processed asynchronously, nization loop, even when we have exceeded our nsPerFrame
you want to make sure that the thread that processes those time; sharing the CPU with other threads is usually a good
events gets enough time to actually run effectively. Also, your idea.
game may use extra cycles in a different thread to run the AI Line 7: Thread.sleep(1) – in our demo, we set this call to sleep
engine, the physics engine, the audio, or a host of other things. for the minimum amount of time before spinning around the
It’s far better to determine a “good” or “good enough” frame loop again. You may want to set this sleep time more carefully
rate and try to match that than to just run as fast as you can. in your game according to how much time you can afford to
Another reason for consistency is that you don’t want disturb- give away to other threads. The trade-off here is that you want
ing artifacts if your game runs like a demon for several frames to sleep for the maximum amount of time possible to allow
and then crawls for a frame or two because some other thread other threads to have fair access to the CPU, but the minimum
is playing catch up. Far better to set a consistent rate lower than amount of time so you can still wake up soon enough to hit
the peak so that the game can run smoothly at all times. your target time to process the next frame; sleeping too long
One simple way to get a consistent frame rate is to figure out can make it impossible to hit your ideal frame rate.
what rate you would like to run at (based on preset values, user Line 12: prevFrameTime is assumed to be a variable of type
input, or timing tests before or during the game) and to throttle long that is retained between frames just for the purposes
the game loop down to that speed during syncFrameRate(). seen in this syncFrameRate() method.
Line 11: elapsedFrameTime is the actual time passed since
Elapsed Frame Time the last time around the loop. It’s used in later processing of
Once you’ve throttled the frame rate to some reasonable things like object movement.
level, you also want to measure the exact time that has passed
since the last frame. This is what you should use to calculate gatherInput()
things like object movement – you may be close to some ideal This stage is used to process all of the “input” that has ac-
time value by trying to set a consistent frame rate but chances cumulated since the last frame. This input includes both local
are you’ll usually vary slightly from that ideal (and you may oc- input events such as keyboard and mouse as well as network-
casionally vary widely due to some hiccup in performance). You ing events in networked games.
should therefore ensure that your movement calculations are Input is actually a two-stage process, the second of which is
correct with respect to actual time and not the ideal time you are the call to gatherInput(). The first half of the input process oc-
aiming for with the frame rate. curs before this step on other threads. We’ll go through both of
To achieve this you need to record the actual time elapsed these input stages below: Event accumulation and Intelligent
since the last time around the game loop. Later movement Event accumulation.
calculations will then use this elapsed time value to come up
with the time-correct values. Important Caveats and Petty Justifications
syncFrameRate() Code We (the authors) are not game programmers, instead we develop
The following code makes sure that your game is not trying graphics libraries and tend to know a thing or two about game program-
to run any faster than this rate (the nsPerFrame variable): ming concepts. Therefore this article is intended more for programmers
who would like to know more about how to write basic games in Java
1: public void syncFrameRate() { rather than advanced game programmers. We hope the information about
2: long nextFrameTime = prevFrameTime + nsPerFrame; Java programming will benefit anyone who wants to know more about
3: long currTime = System.nanoTime(); doing graphics programming on the Java platform, but we don’t want to
4: while (currTime < nextFrameTime) { mislead anyone who is looking for state-of-the-art information on how to
5: Thread.yield(); write a really slick circa-1980’s arcade game…
6: try {
7: Thread.sleep(1); Demo
8: } catch (Exception e) {} This demo illustrates the points of the article and presentation and
9: currTime = System.nanoTime(}; is not intended to be the end-all of 2D game programming. Instead, we
10: } focused on the general concepts of game frameworks and Java2D tips and
11: elapsedFrameTime = currTime - prevFrameTime; tricks. So take the game in the spirit in which it is offered, consume it, and
12: prevFrameTime = currTime; have fun writing something better.
13: }

www.SYS-CON.com/JDJ October 2004 53


Event Accumulation whether the key is down or up once per frame because the
In a typical AWT/Swing application, events are processed time elapsed between frames may be long enough to allow
on the Event Dispatch Thread by calls to EventListener objects several shots to be fired in that time. Instead, we need to
whenever events occur. The same thing happens here – we will know how long the spacebar has been down. Or, if it was
attach EventListeners to find out when specific events occur. pressed and released all within the same frame, we need to
The only difference is that we won’t process the events until the know how long it was down before it was released. Further-
later gatherInput() stage. Instead we’ll simply accumulate the more, if it was pressed and released several times, we should
information about what has happened and wait until gatherIn- track that total time held and perhaps the number of times it
put() to actually do something about all of those events. was pressed and released.
The process for listening to events is the typical AWT/Swing We may want even more information than the total time
one. First register some object to be a listener: held. Say a key determines movement for a character; the lon-
ger the key is down the more the character moves. If we track
EventListener eventGatherer = new EventGatherer(); total time held, that at least gives us constant movement per
frame.addKeyboardListener(eventGatherer); time unit the key is held, but it will make character movement
very stilted as characters will always move at the same rate. A
The actual process of gathering the input happens in the more natural motion is to use acceleration and perhaps decel-
EventGatherer class that we have created. In the class below, eration, taking the time held into account when determining
we’re assuming that spacebar events are of interest. Note that how fast to move the character.
an actual implementation of the class would need to override For all of these additional factors, we need to record addi-
the other KeyboardListener events as well (and may have tional information about each event. It’s not enough to simply
many other methods besides); we are concentrating only on record that an event happened. Instead, we need to record
showing the process of tracking these types of events: when that event occurred, as well as what that event was.
The simple call to spacebarEvents.add(e) above hints at
public class EventGatherer implements KeyboardListener { this; we are adding this new event to some list of events for
public void keyPressed(KeyEvent e) { that key, but we are adding it with the full event structure so
if (e.getCharCode == KeyEvent.VK_SPACE) { we can record the event times as well as the event actions.
spacebarEvents.add(e); From this information, we can later decode the information
return; we need to track such things as total hold times and press/
} else if (...) { release data.
// etc.
} gatherInput(): Processing the Input
} The steps outlined above (accumulating the input data)
} happen throughout a frame. Later on, exactly once per frame,
the application takes all of the accumulated data and pro-
Intelligent Event Accumulation cesses it accordingly:
In the EventGatherer code above, note that we are calling
spacebarEvents.add(e) instead of just toggling a variable that public void gatherInput() {
determines whether the spacebar is being held or not. In if (spacebarEvents.size() > 0) {
general, we want to accumulate information about all of the processSpacebarEvents(elapsedFrameTime);
relevant events that occurred in each frame, not just which }
state things are in at the end of a frame. // etc.: process all other events here as well
As an example, the spacebar might be responsible for }
shooting some rapid-fire projectile. It’s not enough to know
It’s important to synchronize access to the structure used
for storing the events. Since we are, by definition, accessing
that structure from different threads, the program needs to
ensure that it only accesses it from one thread at any given
time. Moreover, since we want to process all events at one time
in gatherInput(), no more events can be added to the queue in
parallel while we are working on it. Failure to do this will mean
we process events that have not actually occurred yet. This is
because any frame is being processed based on the time just
passed, and we cannot take into account things that are hap-
pening while we are processing that frame because all of that
must be processed on the next frame.
The method processSpacebarEvents(long) is passed in the
elapsed time that we calculated in syncFrameRate(), and uses
the argument to calculate event actions based on the events
that occurred for the spacebar as well as the times that those
Figure 2 Collision detection finds two possible collisions this frame events occurred.

54 October 2004 www.SYS-CON.com/JDJ

This may appear pretty sketchy, but that’s what having
sample code is all about. In the Ping demo we do the event
accumulation and processing steps outlined above to cal-
culate accelerating movement for the game paddles in that

What About Networking?

At a high level, network events can be handled exactly
as described earlier for local events – you accumulate the
network events in separate threads asynchronously and then,
once per frame, you process these events and calculate any
necessary movement and actions.
The details behind this high-level view are, of course,
much trickier. Networking can be far more complicated
than the simple local event processing described earlier. We Figure 3 Ball bounces due to first collision and we recalculate for new trajectory,
decided not to pursue networking in this article and demo finding a new collision with the wall
and to instead just focus on simple game frameworks and
rendering issues. Nevertheless it’s useful to at least call out • Latency: Even if all players have DSL, there are probably
some of the important issues that will arise in any networked still inherent delays in transmission between the clients,
game. more so with geographically widespread clients. Your game
must deal with latency aggressively through things like
Network Events movement prediction. For example, instead of depending
There are many different types of networking events that on every client transmitting new positions for their charac-
you may want or need to process. These include player move- ters every frame, it would be far better for clients to simply
ment, player action, and synchronization: transmit when movement trajectory/velocity has changed
• Player movement: Players on different machines will pre- and what those new values are. Then the other clients can
sumably have characters they control locally whose positions do their own calculations of where that character is every
need to be communicated to the other machines involved in frame until they receive new information from that client.
the game. • Synchronization: As mentioned earlier, all of the clients
• Player action: Actions such as firing, colliding, and speaking need to have the same view of time in order to have the
need to be communicated to all appropriate clients in the same view of the game universe. Issues such as latency
game. can make synchronization difficult (you cannot tell
• Synchronization: Timing synchronization is critical in the machines to start an action at the same time if the
networking. Chances are slim that games on separate cli- machines don’t get that message to start at the same
ents began at exactly the same moment, or even that the time).
clocks on the different game clients are matched. There • Loss: There is always the potential for data loss over the
needs to be some kind of handshaking and potentially network. If your game is dependent upon every data
ongoing sanity-checking to make sure that one client’s packet reaching its destination intact, there will be prob-
view of time and the world is the same as all other lems when that doesn’t occur.
clients. • Protocol: The issue of data loss over the network raises the
important issue of networking protocols. There are various
Networking Considerations protocols to choose from, some of which handle such things
There are a variety of important considerations that must as packet loss. These higher-level protocols can detect loss
be taken into account when designing and implementing a and retransmit lost data, but at the cost of a higher overhead
network architecture for a game including network architec- protocol with longer transmission and processing times.
ture, networking artifacts, and protocol: In general, with the very fast networking requirements of
• Network architecture: Your game could use a peer-to-peer games, a better choice is the smallest and cheapest (in terms
architecture, where each client of the game speaks directly of overhead) protocol available. But if this choice means that
to the other clients. This is possibly a simpler architecture your game may experience data loss and out-of-order data
with lower overhead, but requires that each client keeps a packets, your code must account for that possibility.
more complete model of the universe locally. This approach
also has security implications, as any client has the ability to updateObjects()
hack the universe and thus cheat in the game. A client/server Once we have determined our elapsed frame time and
architecture has higher overhead, but benefits from the secu- processed all of the input for this frame, it’s time to update
rity and manageability standpoint because there is always the positions of all objects. Object positions are based, in
one single machine that has the master view of the universe. general, upon two factors: elapsed time (a moving object
• Networking artifacts: Even in these days of widespread must be moving at some rate per time) and change in move-
broadband access, there are still important network arti- ment (presumably triggered by some event). There is also
facts that cannot be ignored such as latency, synchroniza- object collision that can cause movement change, but we’ll
tion, and loss. handle that as part of this updateObjects() process.

www.SYS-CON.com/JDJ October 2004 55


The general idea behind updateObjects() is simple: for all dynamic (moving) objects; and how physically correct you
objects that can move this frame, calculate their movement would like your collisions to be.
and reposition them accordingly: In the simple case of the Ping demo there are only two types
of collisions possible: the paddles with the walls, and the ball
public void updateObjects() { with the paddles and walls. The paddle/wall collisions are
for (Movable movable : movables) { handled implicitly during the gatherInput() stage; keyboard
movable.update(elapsedFrameTime); events determine paddle movement and this movement is
} merely constrained to be within the area defined by the walls.
processCollisions(); The ball collisions are handled during the processCollisions()
} call and consist of checking the ball boundaries against the
wall/paddle boundaries and calculating new positions based
The interesting for() statement is based on the new, on any collisions and bounce effects.
expanded for() semantics in release 5.0. Essentially, we’re just Interestingly, even this “simple” collision caused artifacts
saying that for all of the objects that can move, go ahead and in the original version of the Ping demo to use only the ball
move them by calling update() on those objects. center instead of the ball bounds, as our code simplified
The update() method left unspecified above is a method the collision detection algorithm. This meant that the ball
that takes the current elapsed time for this frame and calcu- would not “collide” at exactly the right time and place; if
lates the new position for that object according to movement one part of the ball collided with a paddle but the center of
information that the object knows about. This includes the the ball did not, we would not detect the collision and the
object’s previous position and its speed (or acceleration, in ball would flow right through. The current version of the
the case of nonlinear movement). demo fixes that problem by using simplifying assumptions
about which sides of the ball can collide with which sides of
processCollisions() each paddle.
Collision processing can be one of the most complex In any case, the processCollisions() step in our demo
parts of your game depending on several factors: how many consisted of checking a single moving object (the ball)
objects can potentially collide; how many of those objects are against the various elements in the scene with which
it might collide. Note that multiple collisions might be
detected (the ball trajectory might intersect both a paddle
and a wall, for example); in that case, the closest collision
would be selected (if the ball hit the paddle first, the col-
lision against the wall would be invalid because the ball
would bounce off the paddle). If a collision was detected, a
bounce would occur and a new position would be calcu-
lated. Then collision processing must happen again for the
new trajectory that the ball takes after that first bounce.
This process must be repeated until there are no more colli-
sions for that object in this frame.
This process is illustrated in Figures 2–5.
Figure 2 shows the ball (blue) traveling toward the paddle
(yellow) with the trajectory illustrated by the dotted line, in the
direction of the arrow. We calculate two collisions (black Xs),
one with the paddle and one with the wall (pink). The original
Figure 4 Ball bounces again due to second collision and we recalculate for new endpoint of the ball would be where the gray ball is painted
trajectory, finding no more collisions inside the wall (if there were no collisions).
Figure 3 shows the next step in processCollisions. We’ve cal-
culated that the ball will bounce off the paddle first and travel
in the resulting trajectory toward the wall, resulting in the new
collision shown with the new black X.
Figure 4 shows the next step in processCollisions() where
we have calculated a bounce off the wall and a trajectory for
the ball flying back out into the playing area.
Finally, in Figure 5 we show the final state of the ball (still in
gray) with the trajectory and collisions as shown by the lines
and Xs that caused the ball to end up where it is.
One thing that this example glosses over is the actual calcu-
lations involved, both the calculation of each collision as well
as the calculation of the bounce. We’ll save that discussion
for the code itself where you can see what we did to calculate
these results in the demo. However three points are important
Figure 5 Final location of ball after all collision processing to bring up about this process.

56 October 2004 www.SYS-CON.com/JDJ

1. Trajectory Versus Position Collision were dynamically deforming or even if the collisions were
The simplest approach to collision detection would be to accurately modeled on complex objects. The equations in all
simply calculate whether the end position of an object overlaps of these cases become much more involved and need to be
with any other object in the world. If so, do the right thing. If carried out potentially many times in each frame as objects
we had taken this approach in the previous example, it’s easy bounce off possibly several other objects in any given frame.
to see what artifacts would result. In the first picture we would
not have calculated the bounces as shown; we would have just render()
detected that we were inside the wall and would then calculate The main idea in the rendering stage is to traverse the
a bounce based on that collision while ignoring the first colli- world of visible objects and draw all of them to the screen.
sion that actually should have occurred with the paddle. Of course the details are a bit more involved. In general, for
To do this correctly requires calculating collisions based on most 2D games, the scene rendering might be boiled down
object trajectories, not just object positions. In the figures this to something like:
means we need to calculate the collisions between the dotted
line representing the object movement and the objects that the 1: public void render() {
line encounters. 2: renderBackground();
3: renderObjects();
2. Physically Accurate Collisions 4: renderForeground();
Figures 2–5 show a single vector that we use to base the col- 5: showBuffer();
lisions on. However, the ball occupies an area, not just a point. 6: }
To calculate correct collision information, we would need to
calculate the intersection of the ball area with all available Let’s analyze each method call in turn.
objects, not just some point or line representing its trajectory.
To simplify the demo code (and make sure it was ready to Line 2: renderBackground() draws the background image
be shown at JavaOne), we actually used the simplified ap- or any other static graphics that tend to be the same in every
proach of intersecting a single line in the version we showed frame. It underlies everything else in the game, so draw it
at the conference. It worked pretty well in general, but had first.
visible artifacts. For example, if the collision point is based on Line 3: renderObjects() draws all of the static and mov-
the center of the ball, you’ll notice the ball passing through able objects in a game. This includes things like the walls,
objects up to that center point; not very realistic (even for a paddles, and ball from the Ping demo. You may want to
game this unrealistic to begin with), and the artifacts only break apart the static and movable objects and draw one or
worsen with the size of the objects that take this simplified the other first (for example, maybe your player characters
approach. should always appear on top of any static objects, in which
For a real game with much more complex objects than our case you should draw the static objects before the movable
simple ball, say cars or planes, you would need to decide what objects).
level of simplification you could live with, if any, and what arti- Line 4: renderForeground() draws anything that overlaps
facts those assumptions would cause. For example, you could the game, such as the score, any help, status windows, or
use a simple bounding box or circle around an object to cal- text, a dashboard in a driving game, or other things that are
culate collisions; this is better than the point-based approach considered to be separate from the world of the actual game
above, but would still have artifacts such as detecting collisions objects. Since these things need to be rendered on top of the
when there were none (because the bounding area might col- rest of the game, we draw them last.
lide when the actual object inside that area would not). Line 5: showBuffer() – in general, games will always draw
to an offscreen buffer instead of directly to the screen. This
3. Bounce Calculation makes animations appear much smoother and game play
The figures assume a standard thetaI == thetaR (angle more enjoyable. After everything is rendered to the back
of incidence equals angle of reflection) equation for the buffer, the final step is to show this buffer on the screen. The
bounce. This works fine, in general, but ignores some more details of how to do that will be discussed in Part 2.
complicated and interesting possibilities such as putting This is a pretty gross simplification of the rendering pro-
english, or spin, on the ball. For example, if a paddle is mov- cess. The actual work your application would do to render-
ing when the ball hits it, it would be reasonable to figure ing would be highly dependent upon the type of game that
in some kind of angle perturbation based on the speed of it is. For example, 3D games have very different require-
paddle movement, such as you would get if a tennis racket ments than 2D games. In the 2D domain, there are many
were moving sideways when it struck a ball (thus putting different types of games (scrollers, top-down, action, word,
spin on the ball and changing the angle of reflection). The arcade, etc.); how you choose to render your world depends
Ping demo shows some of this behavior, as we modify the much on what you have to render and what the interface
angle of reflection based on the speed that the paddle is is like.
moving when it hits the ball. There are, however, some basic elements of 2D rendering
that are common to many different games in that domain.
This was the case for our simple one-collidable-object case Next issue, we will dive into 2D rendering issues, such as
in Ping – imagine how complex this process would be if our buffering, fullscreen, image usage, and performance tips. We
world consisted of multiple dynamic objects or if the objects will also give an overview of the Ping demo code.

www.SYS-CON.com/JDJ October 2004 57


Industry News
JBoss Becomes a Member of the COMcheck-EZ – a free computer software ap- Application Portal Borland Developer Edi-
Eclipse Foundation plication provided by the U.S. Department of tion. This wizard-based solution is designed
(Atlanta/Ottawa) – JBoss, Inc., the Profes- Energy (DOE). It streamlines the energy code to help developers create custom portlets
sional Open Source company, and the Eclipse compliance and approval process for those based on the Java standard (JSR 168) to
Foundation, a community committed to the in the commercial building industry. The new deliver security features inherent in the J2EE
implementation of a universal platform for Java version provides platform-independent environment, shorten development time,
tools integration, have announced that JBoss user interfaces allowing the software to run on and enhance scalability and performance for
has become a member of the Eclipse Founda- either Windows or Macintosh computers. enterprise applications.
tion and will contribute code toward the J2EE www.redrocksw.com www.borland.com
Standard Tools Project. By working together, www.vignette.com
the two will combine their expertise to pro- AMX Extends NetLinx with Java for Industry’s
vide a more complete and tightly integrated First Dual Language Control System Jinfonet Software Awarded GSA Schedule
open source tools and middleware solution. (Richardson, TX) – Demonstrating the (Rockville, MD) – Jinfonet Software has an-
www.jboss.com company’s support of open standards, AMX nounced that JReport is now listed on the
www.eclipse.org Corporation has announced Duet, an exten- United States GSA Schedule. Also referred to
sion of AMX’s NetLinx platform that provides as Federal Supply Schedules, GSA Schedules
Stottler Henke Introduces dealers with the opportunity to integrate AMX’s provide government agencies with access
DataMontage Software advanced control systems using the stan- to high-quality products and services at the
(San Mateo, CA) – Stottler Henke Associates, dards-based Java programming language. By most-favored customer price.
Inc., has announced DataMontage, a new providing the option to program using NetLinx, JReport is a 100% J2EE reporting solution
software product that enables rapid visual Java, or both, Duet expands the development that seamlessly embeds into any application
analysis of complex, time-oriented data. opportunities for existing NetLinx program- with minimal development effort, resulting
DataMontage is a Java class library that offers mers while opening the door to 4.2 million Java in a fast time to deployment and enhanced
flexibility to display information-dense col- programmers who now can develop innovative application value. By combining enterprise
lections of timelines, time-series graphs, and applications for control systems. and ad hoc reporting with analysis, JReport
time-stamped notes. It enhances reporting www.amx.com 7 provides users with the ability to generate,
and decision support systems for medical share, and analyze on-demand or scheduled
data review, manufacturing data analysis, Borland and Vignette to Help reports from within any application. JReport’s
market research, and planning and schedul- Enterprise Software Teams GSA schedule number is GS-35F-0702P.
ing – environments in which professionals are (San Jose, CA) – Borland Software Corpora- www.jinfonet.com
often called upon to quickly glean meaning by tion and Vignette Corp. have announced a
detecting patterns spanning many variables. relationship and combined solution designed Lockheed Martin Unit Selects Parasoft’s Jtest,
www.stottlerhenke.com to help organizations accelerate the creation C++Test, and Insure++
of portlets with custom functionality, improve (Monrovia, CA) – Parasoft, a provider of auto-
Red Rock Powers the Department of the design and quality of Web interfaces, and mated software error detection and reporting
Energy’s COMcheck Software consolidate management of disparate portlet solutions, announced that Lockheed Martin
(Salt Lake City/Richland, WA) – Red Rock Soft- functionality across the enterprise. The solu- Maritime Systems & Sensors (MS2) has se-
ware and Pacific Northwest National Labora- tion will consist of a portlet creation plug-in lected Parasoft’s Jtest, C++Test, and Insure++
tory have released a new Java version of for Borland JBuilder 2005 and the Vignette to support quality testing for its software.
Jtest and C++Test provide automated unit
Dralasoft Launches Aggressive Channel Reseller Program testing and coding standard analysis for Java
and C/C++ applications by automatically veri-
(Westminster, CO) – Dralasoft, Inc., a provider of business process management (BPM) solutions, has announced the fying compliance to hundreds of coding rules
debut of a results-oriented Channel Reseller Program aimed at attracting top-name VARs (Value-Added Resellers) in a while automatically generating and executing
broad range of vertical markets. Dralasoft believes its Channel Reseller Program will provide partners with the strongest unit tests in order to ensure quality early on in
Business Process Management product capability in the industry. the software development life cycle. Insure++
Dralasoft’s new program will place heavy emphasis on personal contact, training, and ongoing exchange of informa- detects elusive memory errors, such as cor-
tion. Newly signed partners will not only attend a quick-start program orientation, ruption, leaks, and allocation errors in C/C++
but also will receive comprehensive product and sales training at the Dralasoft facili- code. Parasoft’s automated error detection
ties. These initial events, which lead to “Authorized” reseller status, will be followed solutions enable software development organi-
by shared marketing and sales activities. zations to improve development effectiveness,
www.dralasoft.com reduce cost, and increase software quality.

58 October 2004 www.SYS-CON.com/JDJ

JSR Watch

From Within the

Java Community Process Program Onno Kluyt

The ‘wired’ and ‘wireless’ stacks

elcome to the October Connected Device Configuration (CDC) succeeded in advancing into Public
edition of the JCP column! and JSR 219 is creating an update to the Review. This JSR defines annotations
Each month you can read J2ME Foundation Profile. All four JSRs and metadata for the enterprise Web
about the Java Community have now entered Public Review. These services sector and builds upon JSR 174,
Process: newly submitted JSRs, new JSRs progress under JCP 2.6 and thus the which lays the foundation for annota-
draft specs, Java APIs that were final- last week of the review will give the ME tions functionality in the Java platform.
ized, and other news from the JCP. This EC another opportunity to vote on these
month there are no less than 22 JSRs JSRs. OSS/J News
worthy of discussion. There is one more JSR in the J2ME Nakina Systems has proposed to
space that I’d like to call your atten- develop a specification for an OSS
J2SE 5.0 Platform Approved tion to. This is Siemens’ and Motorola’s Discovery API (JSR 254). This API would
The J2SE 5.0 technology, also known Mobile Telephony API JSR proposal, provide mechanisms for the discovery
by its development codename “Tiger,” number 253. An example of the use of physical devices and their associated
is represented by 12 different JSRs in- of this JSR would be applications that data present on a network. Some areas
cluding the so-called umbrella JSR 176, provide conferencing, call scheduling, that the JSR will focus on are discovery
which coordinates the coming together and voice services, but with the limited seed data, agents, concurrency control,
of the individual technology JSRs. The resources of the devices in mind. and device security credentials. As the
JSRs successfully passed their Final title of the JSR suggests, it’s part of the
Approval Ballot. By the time you read JavaServer Faces Starts Work on Its OSS through Java Initiative (OSS/J). A
this column the final release should be Second Installment good place to find “OSS/J”-minded de-
available from java.sun.com. JSR 252 was approved by the SE/EE velopers is at its community on java.net.
EC with its main goal of providing align-
The “Wired” Stack of the ment for JavaServer Faces with JSR 245, And Finally, JSR 255 JMX 2.0
J2ME Technology which is developing a new version of the This JSR submission comes ahead of
The J2ME environment informally JavaServer Pages technology. the umbrella JSR for the J2SE 6.0 plat-
recognizes two technology stacks: the Related to the enterprise space is JSR form. It will make modifications to the
CDC and Personal Profile combina- 181, which BEA as the spec lead has Java Management Extensions as well as
tion (sometimes referred to as the the JMX Remote API. Ease of use is one
“wired stack”) and the CLDC and MIDP focus of this effort, and the JSR will work
combination (sometimes referred to as on some new functionality.
the “wireless stack”). A set of four JSRs It will look at generics, use annota-
related to the first stack is progress- tions to make writing MBeans easier,
ing through. JSRs 216 and 217 define address interoperability and versioning
version 1.1 of the Personal Profile and concerns for Open MBeans, and provide
Personal Basis Profile, respectively. Both support for cascaded MBean Servers.
JSRs update the J2ME technology with That’s it for this month. I’m very in-
changes from the J2SE 1.4 platform (e.g., terested in your feedback. Please e-mail
updates to AWT and JavaBeans APIs). me with your comments, questions, and
JSR 218 is developing version 1.1 of the suggestions.

Onno Kluyt is the
director of the
JCP Program
The J2ME environment informally recognizes two technology stacks:
Management Office,
Sun Microsystems.
the CDC and Personal Profile combination and the CLDC
and MIDP combination”

60 October 2004 www.SYS-CON.com/JDJ

Advertiser Index
Advertiser URL Phone Page
Altova www.altova.com 978-816-1600 13

Axalto www.simagine.axalto.com 47

Borland www.go.borland.com/j6 831-431-1000 9

Business Objects www.businessobjects.com/dev/p7 888-333-6007 23

DataDirect www.datadirect.com/jdj 800-876-3101 7

Dice www.dice.com 877-386-3323 35

Google www.google.com/cacm 650-623-4000 31

H&W Computer Systems www.hwcs.com/jdj04.asp 800-338-6692 51

ILOG jviews-info-kit.ilog.com 800-for-ILOG 17

InetSoft www.inetsoft.com/jdj 33

InferData www.inferdata.com/jdj 888-211-3421 41

InterSystems www.intersystems.com/match3 617-621-0600 15

IT Solutions Guide www.sys-con.com/itsg 888-303--5282 59

M7 www.m7.com/d7.do 866-770-9770 27

Microsoft msdn.microsoft.com/visual/think 11

Mindreef www.mindreef.com +1 603 465-2204 43

Northwoods Software Corp. www.nwoods.com/go 800-434-9820 49

Oak Grove Systems www.oakgrovesystems.com 818-440-1234 45

Oracle www.oracle.com/portal 800-633-0759 Cover II

Parasoft Corporation www.parasoft.com/jtest 888-305-0041 21

Quest Software, Inc. http://www.quest.com/jdj 800-663-4723 Cover IV

ReportingEngines www.reportingengines.com/download/f1ere.jsp 888-884-8665 25

Scientific Toolworks, Inc. www.scitools.com 37

Sleepycat Software www.sleepycat.com/bdbje 510-597-2128 29

Software FX www.softwarefx.com 800-392-4278 Cover III

Stylus Studio www.stylusstudio.com 781-280-4488 4

Web Services Edge 2005 East www.sys-con.com/edge 201-802-3069 61

General Conditions: The Publisher reserves the right to refuse any advertising not meeting the standards that are
set to protect the high editorial quality of Java Developer’s Journal. All advertising is subject to approval by the
Publisher. The Publisher assumes no liability for any costs or damages incurred if for any reason the Publisher
fails to publish an advertisement. In no event shall the Publisher be liable for any costs or damages in excess
of the cost of the advertisement as a result of a mistake in the advertisement or for any other reason. The
Advertiser is fully responsible for all financial liability and terms of the contract executed by the agents or agen-
cies who are acting on behalf of the Advertiser. Conditions set in this document (except the rates) are subject
to change by the Publisher without notice. No conditions other than those set forth in this “General Conditions
Document” shall be binding upon the Publisher. Advertisers (and their agencies) are fully responsible for the
content of their advertisements printed in Java Developer’s Journal. Advertisements are to be printed at the
discretion of the Publisher. This discretion includes the positioning of the advertisement, except for “preferred
positions” described in the rate table. Cancellations and changes to advertisements must be made in writing
before the closing date. “Publisher” in this “General Conditions Document” refers to SYS-CON Publications, Inc.

This index is provided as an additional service to our readers. The publisher does not assume any liability for errors or omissions.

October 2004 61
@ the Backpage

Java Certification and I by William Knight

s the Java certification program “I am a Java Certified Programmer it immediately, it’s wrong. Many of the
offered by Sun really the route to and the only thing I’ve gotten out of it questions in the exam require careful
a higher salary and better quality is the ability to say that it’s been exactly study – the exact opposite of what we,
of code for businesses? I have my useless to me.” as programmers, should be trying to
doubts. “I had hoped that certification might achieve.
In my fifth year of Java program- carry some clout, but it isn’t turning Instead I would like to see the exam
ming, after being involved in several out that they have as much clout as the concentrate on the process of program-
distributed developments for large certification vendors would have you ming rather than the code itself; to
companies, a prospective employer believe.” me you can be a first-class developer,
tested my ability. “Don’t worry,” he “I’m an SCJP and SCJD. They’re abso- without knowing all the grammatical
said, “you’ll have no trouble; this is for lutely useless in your career.” details, by concentrating on clarity. Busi-
beginners.” This attitude of irrelevance is nesses require the production of quality,
Unfortunately I failed and was confirmed by the statistics on Job- maintainable code in a timely manner,
embarrassed – it was a dismal score. He Serve (the largest job database for IT and this can be tested. For example, the
asked me why I had scored so low and personnel in the UK: www.Jobserve. following subjects should be covered:
with my career and self-belief crushed co.uk). A search for Java returned 1,498 1. Comment this section of code.
and broken and in ruins on the grey current jobs, while a search for Java 2. Rewrite this code to be self-docu-
carpet-tiles, I mumbled something and Certified returned just 10 current menting.
about IDEs, esoteric details of thread jobs. About 0.7% of positions ask for 3. How to avoid threading. The prolif-
control, and tricky default behavior. I certification. eration of unnecessary threading is
dashed from the building and imag- a personal irritation. I went through
ined him later berating the agent for 10 years of development on huge
sending somebody so obviously lying systems without needing to create
about his experience. threads, and now they appear every-
But I wasn’t lying. For 10 years I where.
hadn’t created a source file or method 4. What is wrong with this dry run?
name from scratch – the IDE had done 5. Write the code this flow chart
it for me. I never had cause to use represents.
Java threads, and I avoided the use of 6. Provide suitable names for these
default behavior on principle. When classes.
I first began programming in Java,
integrated development environments When considering the testing of
already existed that created the files ability, it is important to remember
and provided all the correct packages that programming does not exist in
and imports. isolation but side by side with business
Never once was I concerned with goals: deadlines, quality controls, and
the correct syntax for the main() maintenance. It’s not enough for a
method or the Java source file. What programmer to know the language – in-
William Knight has been mattered to me was creating an under- deed I argue that in some heads, know-
developing software for 20 standable design and hitting the busi- I decided to take the test despite the ing the language too well can even be
years and writing about it ness goals. As a team leader, if one of evidence and discovered that learning a hindrance to readable code. Yet the
for two. His career has my coders had written examples like Java for the real world and learning it for business aspects of development are
taken him around the the ones I’d been tested on, I’d have the exam are not the same. The exam entirely missing from the program-
world twice and onto the sent him or her on a training course to curriculum is well intended but seems ming exam. It does not test language so
staff of a selection of the learn the language. to encourage dubious practices that much as it tests grammar, but the two
world’s largest companies. I later discovered that the interview only an experienced programmer would are very different.
He currently lives and questions had come from the Sun Java know about. After passing with 80%, I You can learn more about the Java
works in Devon in the UK. certification. While finding out more was left wondering why Sun had cre- certification program at http://train-
about certification I came across the ated such devious questions. My rule of ing.sun.com/US/certification/java/
william@wordsafari.co.uk following quotes on the Web: thumb is that if you can’t comprehend index.html.

62 October 2004 www.SYS-CON.com/JDJ


PerformaSure — a system-wide performance

diagnostic tool for multi-tiered J2EE applications Join The Thousands of Companies Improving Java Application
running in test or production environments.
Performance with Quest Software.

Whether it’s a memory leak or other performance issues,

Quest Software’s award-winning Java products — including

JProbe® and PerformaSure™ — help you spend less time trouble-

JProbe — a performance tuning toolkit
for Java developers. shooting and more time on the things that matter. Quest’s Java

tools will identify and diagnose a problem all the way down to

the line of code, so you no longer have to waste time pointing

fingers or guessing where the problem lies. Maximize your

team’s productivity with Quest Software by downloading a free

eval today from http://www.quest.com/jdj.

© 2004 Quest Software Inc., Irvine, CA 92618 Tel: 949.754.8000 Fax: 949.754.8999

Вам также может понравиться