Overcoming Challenges in Mobile J2ME Development
- Limited Device Hardware
- Slow, Unreliable Networks
- Pervasive Devices
- Ubiquitous Integration
- The Impatient User
- Summary
- Resources
CHAPTER OVERVIEW
-
Limited Device Hardware
-
Slow, Unreliable Networks
-
Pervasive Devices
-
Ubiquitous Integration
-
The Impatient User
J2ME allows desktop or enterprise Java developers to migrate their existing skills to build smart mobile applications for enterprises and consumers. Those skills include basic concepts of the Java language, APIs, and common design patterns. However, blind "skill transfer" from the desktop, server, or thin client world could do more harm than good. For example, although most AWT-based J2SE applications run on PersonalJava and J2ME Personal Profile without modification, porting them directly to mobile devices often results in unacceptable performance and very poor usability. To build successful smart mobile applications, developers must understand the special characteristics of mobile devices and networks.
As Java developers and architects, what should we know about the mobile development? How do we retrain ourselves for the new tasks? This last chapter of Part II, "End-to-End Enterprise Applications," answers those questions. We analyze challenges in mobile application development and discuss best practices to overcome them. Many of the solutions and tools we introduce in this chapter are discussed in further detail later in this book.
7.1 Limited Device Hardware
The most visible difference between the mobile and PC platforms is the difference in computing hardware. Today's PCs have much faster CPUs and far more memory and storage spaces than any mobile computing devices. Desktop and server developers can afford the luxury to write applications with bloated features (e.g., Microsoft Office); they also have access to rich productivity features provided by large, all-in-one frameworks (such as the J2SE platform itself). However, on mobile devices, it is a completely different story. With CPUs as slow as 20MHz and RAM as little as 100KB, we must carefully evaluate the features we need, thoroughly optimize our code, and live with limited framework support. In this section, we discuss how to cope with those challenges.
7.1.1 Lightweight Libraries
The most common mistake beginners make is the "golden hammer" anti-pattern: choosing the wrong technology for the task. In the Java world, software tools are often available as reusable objects in standard or third-party libraries. To choose the best libraries that support required application features at the minimum hardware cost is essential.
J2ME Foundation and Personal Profiles (as well as PersonalJava) are compatible with J2SE at the bytecode level and inherit a large subset of the J2SE core API. In theory, we can port J2SE libraries (e.g., XML processing, cryptography, messaging, and UI) directly to mobile devices. However, to do so would defeat the purpose of J2ME and result in slow and bloated applications that can be deployed only to the most expensive devices. In most cases, we should choose from lightweight library alternatives that are specifically designed for the mobile platform. Multiple vendors often compete in the same market. Each vendor offers a slightly different lightweight product with an emphasis on different features. Chapters 11 and 19 provide examples of how to compare and choose the best lightweight embedded database and cryptography toolkits for your projects.
CLDC and MIDP standard libraries are designed from the ground up as lightweight components. However, the need to select the right tools also applies to MIDP projects when it comes to third-party libraries. For a specific library, vendors often offer a version with J2SE-compatible APIs for larger MIDP devices (e.g., Symbian OS devices) and another extremely lightweight version that uses proprietary APIs. The latter often has a smaller memory footprint and better performance, but requires extra developer training and results in less portable applications. Examples of MIDP lightweight libraries include the PointBase MIDP relational database APIs (see Chapter 12, Section 12.1) and iBus//Mobile JMS client APIs (see Chapter 10, Section 10.3).
7.1.2 Reduce Application Footprint
Pervasive mobile devices have extremely limited memory and storage spaces, requiring us to minimize both the storage and runtime footprints of the application. Specific suggestions are as follows.
-
Optimize the packaging process: Even after carefully choosing the best lightweight library, we may still find that the application utilizes only part of the library. In the packaging process, we should include only the classes we actually use. We can do this manually for smaller libraries or use automatic tools bundled with some J2ME IDEs (such as the IBM WebSphere Studio Device Developer) for large libraries. If you want to further reduce the binary application size, you can use a bytecode obfuscator to replace long variable names and class names with shorter, cryptic ones.
-
Partition the application: Since the MIDP runtime loads classes only as needed, we can partition the application into separate parts to reduce the runtime footprint. For MIDP applications, the MIDlet suite can contain several relatively independent MIDlets.
Although the standard MIDP specification does not support shared libraries, some vendor-specific implementations do. An example is the BlackBerry Java Development Environment (JDE) for BlackBerry handheld devices. A shared library further reduces the overall footprint, since the library no longer needs to be duplicated and packaged in each application.
7.1.3 Minimize the Garbage Collector
One great advantage of Java is the built-in garbage collector that automatically frees memory space used by stale objects. This allows developers to focus on the core logic rather than on mundane details of memory management. As a result, Java developers are usually unconcerned about object creation. In fact, many popular Java design patterns promote the idea of creating more objects in exchange of more maintainable code. For example, in the Sun Smart Ticket (Chapter 5) sample application, the use of the MVC and facade patterns results in many objects that simply delegate the action to the next layer. To get a feel for this problem, just look into the numerous classes that implement the RemoteModel interface.
But on mobile devices, due to the small amount of available memory, the garbage collector must run more often. When the garbage collector runs, its thread takes up precious CPU cycles and slows down all other application processes. For effective J2ME applications, we need to minimize object creation and quickly dispose of objects that are no longer in use. Specific suggestions are as follows:
-
Carefully examine design patterns in early stages of the development cycle. For example, the screen flow-based approach demonstrated in the iFeedBack sample (Chapter 3) results in many fewer objects than a traditional MVC implementation.
-
Concisely reuse existing objects at the implementation level. For example, if a same button (e.g., the DONE button) appears in many screens, we should create it once and reuse it.
-
Use arrays and StringBuffers. Arrays are much faster and more memory efficient than collection objects. When we modify or concatenate strings, the immutable String objects result in a lot of intermediate objects. The StringBuffer is much more efficient.
-
Close network connections, file handlers, and Record Management System (RMS) record stores quickly after use. We need to look over the documentation carefully to find out all the close(), destroy(), and dispose() methods and use them judiciously. It is usually considered a best practice to place those methods in the finally block to make sure that the resources are released even if runtime exceptions are thrown.
try { HttpConnection c = (HttpConnection) Connector.open("http://someurl"); InputStream is = c.openInputStream (); // do something with the data } catch (Exception e) { // handle exceptions } finally { try { if ( c != null ) c.close(); if ( is != null ) is.close(); } catch (IOException ioe) { } }
-
Free resources when using native libraries. In smart mobile applications, we sometimes need to access native libraries for better performance, restricted functionalities (e.g., to make a phone call), or simply native UI look and feel (e.g., the IBM SWT library for PocketPC). Native resources are not subject to garbage collection. It is important to follow proper instructions of the native libraries (and their Java wrapper classes) to free resources after use.
7.1.4 Use Mobile Portals
Smart mobile devices are getting more powerful every day. However, in complex enterprise environments, many tasks are still too resource-intensive for most mobile devices. In this case, a commonly used approach is to set up portal servers to which the mobile devices can delegate complex tasks. Mobile middleware portals bridge mobile clients to enterprise backend servers. The smart portal is much more than a proxy or a surrogate for mobile devices. The uses of mobile portals include the following.
-
Allow mobile clients to utilize multiple communication and messaging protocols. For example, mobile messaging servers described in Chapters 9 (Section 9.4) and 10 enable a wide range of devices over a wide range of networks to integrate into corporate messaging infrastructures.
-
Aggregate backend services and enable bundled services. For example, the Oracle9iAS Wireless server provides J2ME SDKs for Oracle's SQL database, push-based messaging, and location-based services. The BlackBerry Enterprise Server supports unified access to Microsoft Exchange-based or IBM Lotus Domino-based corporate information systems from BlackBerry MIDP devices (see Chapter 8, Section 8.6).
-
Provide simple mobile interfaces for powerful and sophisticated backend services. There are several notable examples:
-
- The MapPoint facade described in Chapter 18, Section 18.2.2, shows how to build an easy-to-access interface for a complex backend Web service.
-
- Database synchronization servers (Chapter 13) synchronize J2ME mobile databases with backend enterprise data sources using complex conflict resolution logic.
-
- The Simplicity Enterprise Mobile Server supports simple, visual ways to build J2ME clients for legacy (mainframe) applications. (See Chapter 14, Section 14.3).
-
7.1.5 Use Design Patterns Judiciously
No design pattern is the silver bullet for every situation. For example, the powerful MVC and Facade patterns demonstrated in the Smart Ticket blueprint (Chapter 5) require several abstraction layers and are probably too heavy for simple applications. For simple applications, we can design the entire logic around screens, as we did in the iFeedBack example (Chapter 3).