Java IoT Authors: Pat Romanski, Elizabeth White, Liz McMillan, Yeshim Deniz, Frank Lupo

Related Topics: Java IoT

Java IoT: Article

J2ME Clients with Jini Services

J2ME Clients with Jini Services

Jini provides simple and reliable access to services over any network, independent of platform, protocol, or application technology. Enterprises can use Jini to develop a resilient service-oriented architecture that can be accessed from a broad range of clients.

Java 2 Micro Edition (J2ME) enables developers to build portable rich-client solutions that can operate in either connected or disconnected modes. The combination of J2ME-enabled devices interacting with enterprise systems through Jini services provides a flexible architecture for building highly reliable, end-to-end mobile solutions.

However, for any client to directly access and/or run Jini services, it must be capable of dynamically downloading and executing Java classes and be able to participate in Jini Discovery and Join protocols. Currently, many J2ME-enabled devices with limited resources don't have this capability and can't directly participate in a Jini network. This article provides an overview of the technologies, and, with the help of an example, illustrates how to overcome the limitations of J2ME to develop an effective mobile architecture based on J2ME and Jini.

Jini Primer
From the official Jini architecture specification, wwws.sun.com/software/jini/specs, the Jini system is defined as:

...a distributed system based on the idea of federating groups of users and the resources required by those users. The focus of the system is to make the network a more dynamic entity that better reflects the dynamic nature of the workgroup by enabling the ability to add and delete services flexibly.

A Jini system uses the network as a foundation to enable service discovery and service execution. Traditional systems attempt to mask the appearance of the network; in contrast, Jini uses the dynamic, flexible nature of the network to form communities and register, discover, and invoke services. Unlike traditional systems, Jini is founded on the assumptions that networks are unreliable, change frequently, and should not require substantial maintenance by an administrator.

To address the assumption that networks are unreliable, Jini systems are self-healing. Jini services repair themselves using the concept of leasing. Leasing ensures that Jini services are automatically removed from the community after a specified time period, without the need for administrator intervention. The network topology can change, but Jini will continue to work without the need to update the URLs referenced in services, change properties files, or perform manual administration. Jini services adapt to such changes automatically. Communication with a Jini service occurs through service proxies that are downloaded to the client machine automatically without administrator involvement, a practice that is normally required in traditional architectures.

Jini Architecture
The Jini architecture (see Figure 1) is comprised of the following elements:

Jini Service
A Jini service is an entity that can be a hardware device or a software component that provides functions like printing a report, looking up a stock quote, or executing an algorithm. Applications and other Jini services can use a Java interface and a Java proxy class to access a Jini service. The Java interface provides the definition of the methods that are available from the Jini service. The Java proxy provides the communication channel between the client and the actual service. Jini services are registered with the lookup service and are capable of being invoked through their public interface, which is defined via a Java remote interface. In most cases, the underlying system that allows Jini services to communicate is RMI. A Jini service may also use protocols such as SOAP, XML/HTTP, or CORBA.

Lookup Service
The lookup service, which is a Jini service, keeps track of the Jini services and provides proxies to communicate with the services. Sun provides a lookup service called Reggie (used for developing this example) as part of the Jini development kit.

Jini Client
The Jini client is any software that requests the proxy from the lookup service in order to invoke the Jini service.

RestaurantFinder Application
Now that you have a good understanding of Jini and J2ME, let's move on to an example application that uses Jini and J2ME to provide a valuable service to mobile users. We'll use this example to show you how to develop a complete Jini/J2ME solution that overcomes the limitations of MIDP.

DineOut Corporation has a comprehensive database of restaurant listings, reviews, and ratings in different cities in the continental United States. DineOut wants to build a system called RestaurantFinder that enables DineOut's subscribers to access these listings using their J2ME-capable cellphones. The software architecture for the RestaurantFinder system is shown in Figure 2.

The core components of the RestaurantFinder system are as follows.

RestaurantFinder Jini Service
The RestaurantFinder Jini service provides the ability to search for restaurant listings by city and cuisine. The definition for this service is given by the interface rf.service.RestaurantFinder (see Listing 1) and the Java proxy for this service is provided by the class rf.service.RestaurantFinderImpl (see Listing 2). (Listings 2-5 can be downloaded from www.sys-con.com/java/sourcec.cfm.) Using Jini to develop the RestaurantFinder service has the following benefits:
1.   Provides a fault tolerant infrastructure by hosting multiple instances of the RestaurantFinder service on different nodes in the network. Thus, if there's a failure of one node, users will not lose access to the service.
2.   Enables an administrator to add, remove, and move instances of the RestaurantFinder service from the network without affecting the overall operation of the system.

Jini Lookup Service
The lookup service manages a persistent store of service registrations. The J2ME client uses the Jini lookup service to find and locate the RestaurantFinder Jini service. Using the lookup service decouples the J2ME client from the actual service instance, enabling the Jini server to use multiple service instances to handle requests from the client.

J2ME Client
The MIDP client shown in Figure 3 will access the RestaurantFinder Jini service.

A Jini client developed using the J2ME technology has the following advantages over a traditional thin-client WAP-enabled cellphone:
1.   It provides a rich user interface to DineOut's subscribers by supporting features such as on-device validation (as opposed to server-side validation) and persistence to save restaurant listings. Thus, users get a more responsive and easier-to-use solution.
2.   The client can operate in connected or disconnected mode. Although users won't have access to all capabilities when operating in disconnected mode, they can at least browse through saved listings, and could potentially also perform searches if restaurant data were persisted to the device.

J2ME Client Proxy
A MIDP device runs on the Connected Limited Device Configuration (CLDC). The CLDC doesn't allow the user to load classes at runtime. This prevents the MIDP client from acting like a regular Jini client, which has the ability to download and execute the service proxy classes. To work around this restriction, the RestaurantFinder service uses a Java servlet given by class rf.servlet.ControllerServlet (see Listing 3) as a Jini proxy. This proxy communicates with the RestaurantFinder Jini service on behalf of the MIDP client. The communication between the servlet and the MIDP client takes place over HTTP as shown in Figure 2.

Developing the RestaurantFinder System
Now that we're familiar with RestaurantFinder's architecture, let's have a closer look at what's involved in developing the RestaurantFinder system.

Developing the Jini Service
The first step in developing the RestaurantFinder service is defining the service interface rf.service.RestaurantFinder (see Listing 1). This interface defines the method getRestaurants, which takes a city name and cuisine as an argument, and returns an array of rf.service.Restaurant objects. This interface defines the contract between the Jini client and the Jini service.

Next, we develop the Java proxy class rf.service.RestaurantFinderImpl (see Listing 2) that implements the RestaurantFinder interface. During service registration, this Java proxy is uploaded from the RestaurantFinder service (see Listing 4) to the lookup service as shown in Figure 4. The proxy shields the client from the actual communication with the Jini service. When a client looks up a service, the lookup service returns the proxy object for the service. Any classes required by the proxy are dynamically loaded over the network. In our example, the RestaurantFinder service is executed locally as part of the Jini client's (J2ME Client Proxy in our case) virtual machine. However, the Java proxy could easily be changed to communicate using RMI with a remote RestaurantFinder service.

Finally, the RestaurantFinder service is registered with the Jini lookup service (Reggie). The class ServiceRegistrationClient (see Listing 4) performs this operation.

Consider the following lines of code

ldm = new LookupDiscoveryManager(new String[]
{"iguanas"}, null, null);
jm = new JoinManager(new rf.service.RestaurantFinderImpl(),
null , new ServiceRegistrationListener(),
ldm, null);

The first line creates an instance of LookupDiscoveryManager to locate the group "iguanas". This is a utility class provided by the Jini reference implementation for discovering groups that a service is interested in joining. Groups provide a partition mechanism for the physical Jini network. A Jini service can belong to one or more groups. A Jini client has to join a group before accessing the services of the group. The RestaurantFinder service will join the "iguanas" group. This group is created and serviced by Reggie. The names of the group(s) to be serviced by a Reggie are provided as command-line arguments while starting Reggie as shown below.

java -Djava.security.policy=%JINIHOME%\policy\policy.all -jar
%JINIHOME%\lib\reggie.jar http://%DOWNLOADHOST%/reggie-dl.jar
%JINIHOME%\policy\policy.all %JINITEMP%\reggie.log iguanas

The next line creates an instance of JoinManager. This is another utility class that provides a convenient way to register a Jini service with Reggie (lookup service). In our example, an instance of the JoinManager is instantiated with an instance of RestaurantFinderImpl proxy, rf.service.ServiceRegistrationListener, and LookupDiscoveryManager. The JoinManager will register the RestaurantFinder service and upload the Java proxy for the server to the lookup service.

Developing the J2ME Client Proxy
Listing 3 shows the source code for the servlet rf.servlet.ControllerServlet that acts as a proxy for the J2ME client. Upon initialization, this servlet locates the RestaurantFinder service using the lookup service and dynamically loads the Java proxy from the Jini lookup service (see Figure 4).

The doPost method of the servlet decodes the request from a J2ME client, invokes the RestaurantFinder service, and returns the results back to the J2ME client. Note that the ControllerServlet only accesses the Jini service through the interface rf.service.RestaurantFinder, thus decoupling the servlet from the implementation of the Jini service.

Developing the J2ME Client
The last piece of the puzzle is the J2ME client, which is developed as a MIDlet (see Listing 5) called rf.midlet.RestaurantFinderMIDlet. MIDlets are classes that extend the class javax.microedition.midlet.MIDlet and run on J2ME devices that support the MIDP profile. The execution and the life cycle of a MIDlet are controlled by special software on J2ME devices, called Application Management Software. MIDlets are developed on regular desktops and then deployed to smaller devices. The example in this article was developed using the MIDP reference implementation from Sun.

Execution of the RestaurantFinderMIDlet starts with the invocation of the startAPP method by the Application Management Software. This method initializes the J2ME client and sets up a search form as the current display for the application (see Figure 3). Using this form the user can select the name of the city and the type of cuisine for his or her restaurant search criteria. After selecting the criteria, the user can click on the "find" button to perform a search for restaurants.

Clicking the "find" button triggers the command findCommand. In J2ME, a command is something a user can use to interact with the client, e.g., clicking on the "find" button. In response to the command, the MIDlet opens up a connection to the J2ME Proxy Servlet and performs an HTTP GET as shown in method getResult. The J2ME Proxy Servlet (rf.servlet.ControllerServlet) returns a list of restaurants in the form of an encoded string. We've used an encoded string to keep the application simple; however, in real systems XML can be used for exchanging information between the J2ME client and the servlet.

On receiving the results of the search query, the list of restaurants is displayed to the user (see Figure 5).

The Road Ahead
Using a servlet as a proxy between the J2ME client and the Jini service is a step in the right direction for integrating J2ME clients in a Jini network. However, to fully enable J2ME clients as "true" Jini citizens, we can use the surrogate architecture (http://surrogate.jini.org). This architecture enables the J2ME clients to be part of the Jini network using a surrogate executing within a surrogate host. The surrogate knows of the device's capabilities and operates within the Jini federation. Services in the federation may use the surrogate just as they would use any other service in the federation. The IP Interconnect Adapter specification identifies the requirements of how the surrogate communicates with the device. This surrogate architecture combined with J2ME enhancements, like the MIDP 2.0 push API, can lead to the development of rich wireless clients that can not only leverage the Jini infrastructure but can also be managed as Jini services. (At the time this article was written, the specification for the surrogate architecture and the Interconnect adapters was not yet fully defined.)

J2ME and Jini can be used to develop a highly portable, resilient service-oriented architecture that meets the needs of mobile users. By providing some simple workarounds to current limitations with J2ME, it's possible to build these powerful solutions today.


  • Edwards, W. K. (2001). Core Jini (2nd Edition). Prentice Hall PTR.
  • Edwards, W. K., and Rodden, T. (2001). Jini Example by Example. Prentice Hall PTR.
  • http://java.sun.com/j2me/
  • Knudsen, J. (2003). Wireless Java: Developing with J2ME. APress.
  • More Stories By Ron Dearing

    Ron Dearing is a Senior Software Engineer at Cysive, Inc., based in Reston, VA. Ron has worked in the software industry for over 6 years, and is currently working in the Product Development group on the Cymbio Interaction Server. Ron has a BS in Computer Science from North Caroling A&T.

    Comments (1) View Comments

    Share your thoughts on this story.

    Add your comment
    You must be signed in to add a comment. Sign-in | Register

    In accordance with our Comment Policy, we encourage comments that are on topic, relevant and to-the-point. We will remove comments that include profanity, personal attacks, racial slurs, threats of violence, or other inappropriate material that violates our Terms and Conditions, and will block users who make repeated violations. We ask all readers to expect diversity of opinion and to treat one another with dignity and respect.

    Most Recent Comments
    Aaron Dagan 07/03/03 09:27:00 AM EDT

    " 1. PsiNaptic have already demonstrated an implementation of Jini for J2ME
    at the 2002 JavaOne where we had JMatos running on aJile aj100 processor
    2. Using the surrogate architecture for mobile devices is the wrong approach in many cases
    since one can not and should not assume the presences of a more capable machine.
    With the introduction of Bluetooth enabled J2ME based phones such as the
    Sony Ericsson P800 or the Nokia 7650 the majority of data transfer between
    cell phones and other devices will be done in a PAN environment, eliminating
    the need to connect to a central server over the WAN. For Jini to be
    effective it needs to be self contained and not rely on the presence of
    servers somewhere on the net."

    @ThingsExpo Stories
    Nordstrom is transforming the way that they do business and the cloud is the key to enabling speed and hyper personalized customer experiences. In his session at 21st Cloud Expo, Ken Schow, VP of Engineering at Nordstrom, will discuss some of the key learnings and common pitfalls of large enterprises moving to the cloud. This includes strategies around choosing a cloud provider(s), architecture, and lessons learned. In addition, he’ll go over some of the best practices for structured team migrat...
    SYS-CON Events announced today that Taica will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Taica manufacturers Alpha-GEL brand silicone components and materials, which maintain outstanding performance over a wide temperature range -40C to +200C. For more information, visit http://www.taica.co.jp/english/.
    SYS-CON Events announced today that MIRAI Inc. will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MIRAI Inc. are IT consultants from the public sector whose mission is to solve social issues by technology and innovation and to create a meaningful future for people.
    Recently, REAN Cloud built a digital concierge for a North Carolina hospital that had observed that most patient call button questions were repetitive. In addition, the paper-based process used to measure patient health metrics was laborious, not in real-time and sometimes error-prone. In their session at 21st Cloud Expo, Sean Finnerty, Executive Director, Practice Lead, Health Care & Life Science at REAN Cloud, and Dr. S.P.T. Krishnan, Principal Architect at REAN Cloud, will discuss how they bu...
    Coca-Cola’s Google powered digital signage system lays the groundwork for a more valuable connection between Coke and its customers. Digital signs pair software with high-resolution displays so that a message can be changed instantly based on what the operator wants to communicate or sell. In their Day 3 Keynote at 21st Cloud Expo, Greg Chambers, Global Group Director, Digital Innovation, Coca-Cola, and Vidya Nagarajan, a Senior Product Manager at Google, will discuss how from store operations...
    As hybrid cloud becomes the de-facto standard mode of operation for most enterprises, new challenges arise on how to efficiently and economically share data across environments. In his session at 21st Cloud Expo, Dr. Allon Cohen, VP of Product at Elastifile, will explore new techniques and best practices that help enterprise IT benefit from the advantages of hybrid cloud environments by enabling data availability for both legacy enterprise and cloud-native mission critical applications. By rev...
    Join IBM November 1 at 21st Cloud Expo at the Santa Clara Convention Center in Santa Clara, CA, and learn how IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Cognitive analysis impacts today’s systems with unparalleled ability that were previously available only to manned, back-end operations. Thanks to cloud processing, IBM Watson can bring cognitive services and AI to intelligent, unmanned systems. Imagine a robot vacuum that becomes your personal assistant tha...
    With major technology companies and startups seriously embracing Cloud strategies, now is the perfect time to attend 21st Cloud Expo October 31 - November 2, 2017, at the Santa Clara Convention Center, CA, and June 12-14, 2018, at the Javits Center in New York City, NY, and learn what is going on, contribute to the discussions, and ensure that your enterprise is on the right path to Digital Transformation.
    SYS-CON Events announced today that Datera will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Datera offers a radically new approach to data management, where innovative software makes data infrastructure invisible, elastic and able to perform at the highest level. It eliminates hardware lock-in and gives IT organizations the choice to source x86 server nodes, with business model option...
    Infoblox delivers Actionable Network Intelligence to enterprise, government, and service provider customers around the world. They are the industry leader in DNS, DHCP, and IP address management, the category known as DDI. We empower thousands of organizations to control and secure their networks from the core-enabling them to increase efficiency and visibility, improve customer service, and meet compliance requirements.
    Digital transformation is changing the face of business. The IDC predicts that enterprises will commit to a massive new scale of digital transformation, to stake out leadership positions in the "digital transformation economy." Accordingly, attendees at the upcoming Cloud Expo | @ThingsExpo at the Santa Clara Convention Center in Santa Clara, CA, Oct 31-Nov 2, will find fresh new content in a new track called Enterprise Cloud & Digital Transformation.
    SYS-CON Events announced today that NetApp has been named “Bronze Sponsor” of SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. NetApp is the data authority for hybrid cloud. NetApp provides a full range of hybrid cloud data services that simplify management of applications and data across cloud and on-premises environments to accelerate digital transformation. Together with their partners, NetApp emp...
    SYS-CON Events announced today that N3N will exhibit at SYS-CON's @ThingsExpo, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. N3N’s solutions increase the effectiveness of operations and control centers, increase the value of IoT investments, and facilitate real-time operational decision making. N3N enables operations teams with a four dimensional digital “big board” that consolidates real-time live video feeds alongside IoT sensor data a...
    Smart cities have the potential to change our lives at so many levels for citizens: less pollution, reduced parking obstacles, better health, education and more energy savings. Real-time data streaming and the Internet of Things (IoT) possess the power to turn this vision into a reality. However, most organizations today are building their data infrastructure to focus solely on addressing immediate business needs vs. a platform capable of quickly adapting emerging technologies to address future ...
    SYS-CON Events announced today that Avere Systems, a leading provider of hybrid cloud enablement solutions, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere Systems was created by file systems experts determined to reinvent storage by changing the way enterprises thought about and bought storage resources. With decades of experience behind the company’s founders, Avere got its ...
    SYS-CON Events announced today that Avere Systems, a leading provider of enterprise storage for the hybrid cloud, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 - Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Avere delivers a more modern architectural approach to storage that doesn't require the overprovisioning of storage capacity to achieve performance, overspending on expensive storage media for inactive data or the overbui...
    SYS-CON Events announced today that IBM has been named “Diamond Sponsor” of SYS-CON's 21st Cloud Expo, which will take place on October 31 through November 2nd 2017 at the Santa Clara Convention Center in Santa Clara, California.
    SYS-CON Events announced today that Ryobi Systems will exhibit at the Japan External Trade Organization (JETRO) Pavilion at SYS-CON's 21st International Cloud Expo®, which will take place on Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Ryobi Systems Co., Ltd., as an information service company, specialized in business support for local governments and medical industry. We are challenging to achive the precision farming with AI. For more information, visit http:...
    High-velocity engineering teams are applying not only continuous delivery processes, but also lessons in experimentation from established leaders like Amazon, Netflix, and Facebook. These companies have made experimentation a foundation for their release processes, allowing them to try out major feature releases and redesigns within smaller groups before making them broadly available. In his session at 21st Cloud Expo, Brian Lucas, Senior Staff Engineer at Optimizely, will discuss how by using...
    In this strange new world where more and more power is drawn from business technology, companies are effectively straddling two paths on the road to innovation and transformation into digital enterprises. The first path is the heritage trail – with “legacy” technology forming the background. Here, extant technologies are transformed by core IT teams to provide more API-driven approaches. Legacy systems can restrict companies that are transitioning into digital enterprises. To truly become a lead...