Welcome!

Java IoT Authors: APM Blog, Stackify Blog, XebiaLabs Blog, Liz McMillan, William Schmarzo

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
-Dnet.jini.discovery.interface=%ADAPTERIP%

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.)

Conclusion
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.

References

  • 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
    DX World EXPO, LLC, a Lighthouse Point, Florida-based startup trade show producer and the creator of "DXWorldEXPO® - Digital Transformation Conference & Expo" has announced its executive management team. The team is headed by Levent Selamoglu, who has been named CEO. "Now is the time for a truly global DX event, to bring together the leading minds from the technology world in a conversation about Digital Transformation," he said in making the announcement.
    "Space Monkey by Vivent Smart Home is a product that is a distributed cloud-based edge storage network. Vivent Smart Home, our parent company, is a smart home provider that places a lot of hard drives across homes in North America," explained JT Olds, Director of Engineering, and Brandon Crowfeather, Product Manager, at Vivint Smart Home, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    SYS-CON Events announced today that Conference Guru has been named “Media Sponsor” of the 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. A valuable conference experience generates new contacts, sales leads, potential strategic partners and potential investors; helps gather competitive intelligence and even provides inspiration for new products and services. Conference Guru works with conference organizers to pass great deals to gre...
    The Internet of Things will challenge the status quo of how IT and development organizations operate. Or will it? Certainly the fog layer of IoT requires special insights about data ontology, security and transactional integrity. But the developmental challenges are the same: People, Process and Platform. In his session at @ThingsExpo, Craig Sproule, CEO of Metavine, demonstrated how to move beyond today's coding paradigm and shared the must-have mindsets for removing complexity from the develop...
    In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, led attendees through the exciting evolution of the cloud. He looked at this major disruption from the perspective of technology, business models, and what this means for enterprises of all sizes. John Considine is General Manager of Cloud Infrastructure Services at IBM. In that role he is responsible for leading IBM’s public cloud infrastructure including strategy, development, and offering m...
    "Evatronix provides design services to companies that need to integrate the IoT technology in their products but they don't necessarily have the expertise, knowledge and design team to do so," explained Adam Morawiec, VP of Business Development at Evatronix, in this SYS-CON.tv interview at @ThingsExpo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    To get the most out of their data, successful companies are not focusing on queries and data lakes, they are actively integrating analytics into their operations with a data-first application development approach. Real-time adjustments to improve revenues, reduce costs, or mitigate risk rely on applications that minimize latency on a variety of data sources. In his session at @BigDataExpo, Jack Norris, Senior Vice President, Data and Applications at MapR Technologies, reviewed best practices to ...
    Widespread fragmentation is stalling the growth of the IIoT and making it difficult for partners to work together. The number of software platforms, apps, hardware and connectivity standards is creating paralysis among businesses that are afraid of being locked into a solution. EdgeX Foundry is unifying the community around a common IoT edge framework and an ecosystem of interoperable components.
    Large industrial manufacturing organizations are adopting the agile principles of cloud software companies. The industrial manufacturing development process has not scaled over time. Now that design CAD teams are geographically distributed, centralizing their work is key. With large multi-gigabyte projects, outdated tools have stifled industrial team agility, time-to-market milestones, and impacted P&L stakeholders.
    "Akvelon is a software development company and we also provide consultancy services to folks who are looking to scale or accelerate their engineering roadmaps," explained Jeremiah Mothersell, Marketing Manager at Akvelon, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    "IBM is really all in on blockchain. We take a look at sort of the history of blockchain ledger technologies. It started out with bitcoin, Ethereum, and IBM evaluated these particular blockchain technologies and found they were anonymous and permissionless and that many companies were looking for permissioned blockchain," stated René Bostic, Technical VP of the IBM Cloud Unit in North America, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Conventi...
    In his session at 21st Cloud Expo, Carl J. Levine, Senior Technical Evangelist for NS1, will objectively discuss how DNS is used to solve Digital Transformation challenges in large SaaS applications, CDNs, AdTech platforms, and other demanding use cases. Carl J. Levine is the Senior Technical Evangelist for NS1. A veteran of the Internet Infrastructure space, he has over a decade of experience with startups, networking protocols and Internet infrastructure, combined with the unique ability to it...
    22nd International Cloud Expo, taking place June 5-7, 2018, at the Javits Center in New York City, NY, and co-located with the 1st DXWorld Expo will feature technical sessions from a rock star conference faculty and the leading industry players in the world. Cloud computing is now being embraced by a majority of enterprises of all sizes. Yesterday's debate about public vs. private has transformed into the reality of hybrid cloud: a recent survey shows that 74% of enterprises have a hybrid cloud ...
    "Cloud Academy is an enterprise training platform for the cloud, specifically public clouds. We offer guided learning experiences on AWS, Azure, Google Cloud and all the surrounding methodologies and technologies that you need to know and your teams need to know in order to leverage the full benefits of the cloud," explained Alex Brower, VP of Marketing at Cloud Academy, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clar...
    Gemini is Yahoo’s native and search advertising platform. To ensure the quality of a complex distributed system that spans multiple products and components and across various desktop websites and mobile app and web experiences – both Yahoo owned and operated and third-party syndication (supply), with complex interaction with more than a billion users and numerous advertisers globally (demand) – it becomes imperative to automate a set of end-to-end tests 24x7 to detect bugs and regression. In th...
    "MobiDev is a software development company and we do complex, custom software development for everybody from entrepreneurs to large enterprises," explained Alan Winters, U.S. Head of Business Development at MobiDev, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    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, discussed how from store operations and ...
    "There's plenty of bandwidth out there but it's never in the right place. So what Cedexis does is uses data to work out the best pathways to get data from the origin to the person who wants to get it," explained Simon Jones, Evangelist and Head of Marketing at Cedexis, in this SYS-CON.tv interview at 21st Cloud Expo, held Oct 31 – Nov 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA.
    SYS-CON Events announced today that CrowdReviews.com has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5–7, 2018, at the Javits Center in New York City, NY. CrowdReviews.com is a transparent online platform for determining which products and services are the best based on the opinion of the crowd. The crowd consists of Internet users that have experienced products and services first-hand and have an interest in letting other potential buye...
    SYS-CON Events announced today that Telecom Reseller has been named “Media Sponsor” of SYS-CON's 22nd International Cloud Expo, which will take place on June 5-7, 2018, at the Javits Center in New York, NY. Telecom Reseller reports on Unified Communications, UCaaS, BPaaS for enterprise and SMBs. They report extensively on both customer premises based solutions such as IP-PBX as well as cloud based and hosted platforms.