Welcome!

Weblogic Authors: Yeshim Deniz, Elizabeth White, Michael Meiner, Michael Bushong, Avi Rosenthal

Related Topics: Weblogic

Weblogic: Article

Are Your Handhelds Safe in J2EE Hands?

Are Your Handhelds Safe in J2EE Hands?

As J2EE usage proliferates into new domains, software engineers are realizing that out-of-the-box components cannot be used to solve problems for which they were not designed. One such example is state and session management (SSM) for applications serving wireless devices. We'll look at why traditional techniques don't work and how you can create your own scalable, fault-tolerant SSM solution.

Despite what your stock portfolio says, there is life out there in the high-tech world. One such example is all the software being created for wireless operator consumption. It's becoming more of a wireless world out there and new specifications are emerging. One example is the Wireless Village initiative created by Nokia, Ericsson, and Motorola. Essentially, this joint venture has created a specification for wireless devices. With its emphasis on interoperability, it addresses instant messaging and presence services (IMPS). This feeds into the expanding universe of mobile information appliances and their associated protocols, paradigms, and policies. Now more than ever, software engineers must think beyond the browser-based client.

J2EE is at a crossroads and facing increasing competition from .NET. Which one of these platforms will dominate the wireless frontier? Is J2EE up to the challenge?

Browser-Based SSM
What's so different about architecting an application serving wireless devices vs. one serving browsers? The big difference is state and session management - or what I like to call "SSM." To fully appreciate this, let's look at how SSM works for good old browsers.

To communicate with servers, browsers use HTTP, which is by nature a connectionless and stateless protocol. With HTTP alone, the server cannot maintain a relationship among the series of requests originating from a browser. There are times, of course, when stateful connections are needed, as in the case of a shopping cart in an online store. Each browser must be identified on subsequent requests in order to access its own shopping cart stored as an object in the server. Browser identity is commonly achieved through the use of HTTP cookies. When a browser visits a Web site, the server's response can include a cookie that contains a unique session ID. The browser will automatically pass this back on subsequent requests. If a browser does not accept cookies for security reasons, the server can use URL rewriting. Essentially, this means that the session ID is encoded right into the URL.

Whichever technique is used, ultimately a stateful connection is established via a session ID, which should have the following properties:

  • Be a long alphanumeric string (80-100 characters)
  • Be randomly generated (nonsequential)
These properties lead to session IDs that are difficult to guess and make it challenging for a hacker to masquerade as legitimate user. This requires the attacker to hijack the session, by guessing and using someone else's session ID, to gain access to the system.

Once this is in place, we have a mechanism that allows the server to establish browser identification, create a session, and hold state (i.e., the shopping cart) on behalf of its client. These are the fundamentals of SSM. On a small-scale application, SSM can be fairly straightforward. However, on a big-time, highly available, fault-tolerant, and scalable application, this is quite another matter. Let's take a look at some techniques that WebLogic (version 6.0 or higher) uses to fulfill these big-time requirements.

Performance and Scalability
No matter how much CPU power, memory, or disk space is available, a host has a finite load capacity. If the load increases beyond that point, a cluster of hosts is needed to achieve further scalability. This, of course, adds complexity to SSM. Let's look at how this works in a clustered environment. (Note that this is just one of the various configurations supported by WebLogic.)

In Figure 1, the application is hosted by a cluster composed of four identical and interchangeable hosts - meaning a request can be served by any host. Each is a WebLogic server hosting a servlet and EJB container. The cluster is accessed by a dedicated Web server (such as an Apache HTTP server or Microsoft Internet information server) that serves static content. Dynamically generated content, on the other hand, is dispatched to the WebLogic cluster. The Web server contains a WebLogic-specific plug-in (a.k.a. HttpClusterServlet) that load balances each request to a cluster member. Note that a client's session state is stored in only one particular server. Therefore, load-balancing algorithms, such as round-robin, can cause the request to be sent to a host that does not contain the session state. How will session state be accessed in this situation?

Session state could be accessed using network calls (via RMI) but this would incur extra network traffic and reduce performance. Alternatively, replicating the session state on every host so that it is available everywhere would also be nonviable because as the cluster grows, replication becomes more and more costly. N-host replication hinders scalability.

WebLogic has a clever solution. The session ID, stored in the cookie, has an embedded host name representing the session's home host. The plug-in uses this information to route the request to the home host. This guarantees that accessing the state will be a local, non-RMI call. Typically, processing requests requires many read/writes to session state. When you compare remote calls to local calls, which are 100 times more expensive, this is a key design advantage.

Fault-tolerance and state replication
Another benefit of clustered hosts is their inherent fault-tolerant quality. When one host fails, another one steps in to serve the request. All this happens transparently from the client's perspective. Let's look at how SSM fits into this.

In actuality, a particular session's state is stored on exactly one host (on whichever host the session was created), but is constantly being replicated on exactly one backup host (see Figure 2). Each time an element in the session state is created, updated, or deleted, WebLogic automatically replicates the state on a backup host. Furthermore, to minimize network traffic, only the state delta is replicated.

If the primary (home) host fails, the plug-in will transparently route the request to a secondary (backup) host. Conveniently, all of the session's state will be available and up-to-date right on that host. The secondary will now become the session's primary host and a new secondary will be assigned.

In an effort to avoid n-host state replication, WebLogic employs a one-host replication scheme, or what is referred to as "in-memory replication." As the cluster grows, scalability is not hindered by state replication since there is still only one secondary. The only drawback is that if both primary and secondary fail simultaneously, session data will be lost. WebLogic does offer alternatives, such as database replication, but to the detriment of performance. According to tests, this can represent an 83% drop in performance. In reality, the odds of having two hosts simultaneously fail and losing both session replicas are indeed slim. So in-memory replication is sufficient for most applications.

In retrospect, this is quite powerful functionality. It's all available in J2EE through Sun's servlet specification classes and WebLogic's load-balancing and fail-over mechanisms. Now that I've exposed the inner working of a browser-based client serving, let's see how this relates to wireless clients.

Why Out-of-the-Box Components Won't Do
Unlike the Web browser in the desktop world, there is no ubiquitous counterpart in the wireless world. Wireless devices come in many shapes and sizes and communicate using various protocols. Some are HTTP-based, some are not. All use proprietary networks and all client-to-server communication is mediated by a device-specific gateway. Furthermore, wireless devices must be served differently due to their inherent limitations in screen size, bandwidth, memory, and processing power.

Suffice it to say that despite efforts to make wireless devices behave like desktop browsers (WAP and HDML come to mind), they are a different kind of animal. As far as SSM is concerned, out-of-the-box Web components were specifically designed for HTTP clients. Until the specification addresses this, engineers can choose between two platform-leveraging solutions:

  • Solution 1: Create an adapter component that converts from the native protocol to HTTP - and get all the goodies J2EE has to offer
  • Solution 2: Build upon the platform and roll a custom solution

    These can be viewed as "poor man's solution" vs. "rich man's solution" respectively. Don't get me wrong, I'm not passing any judgements in terms of the affluence of these solutions. The first may be adequate for applications that don't require stateful connections. It can also be good enough if the adapter's performance is not an issue. There are caveats, though, and we'll examine them in detail. We'll also look at a best-of-breed solution.

    Creating an Adapter Component (Solution 1)
    To put things in perspective, let's say you're designing a server-side application that supplies content to SMS-based cell phones upon request. Assume that a stateful connection between client and server is required. The application would most likely use short message peer-to-peer (SMPP) protocol - the most widely used SMS gateway protocol. As Figure 3 demonstrates, the adapter component would convert every request from SMPP to HTTP.

    So where do the problems lurk? First, processing requests incurs the extra costs associated with piping requests across two network connections; one between gateway and adapter and another between adapter and application. Second, and more important, there would be two levels of SSM to maintain: the servlet-based SSM in the servlet container and the custom, adapter-based SSM. Essentially, since SMS gateways have no concept of HTTP cookies, this solution requires the adapter to emulate a browser. That is, save cookies and pass them back to the application. However, unlike the browser, which stores cookies for only one user, the adapter would need to store cookies for every connected client.

    So, how can we do this? A simple solution is to leverage your database's powerful storage and lookup features to store cookies. Naturally, extra costs would be incurred with database lookups on every request. Furthermore, the adapter would also be responsible for garbage collection. A custom eviction policy would need to be designed and implemented.

    There is another potential side effect. Transactions do not propagate across HTTP boundaries. Therefore, components on either side of the divide could not participate in the same transaction. Consequently, if the adapter would read and lock database tables needed by other components, database deadlocks would ensue. Transactions need to be carefully demarcated.

    Alternatively, memory could be used to store cookies. However, with memory, fault-tolerance and replication are now the main concern. If the host fails, the cookie cache must be replicated and available elsewhere.

    In summary, this solution can be sufficient for some applications but clearly falls short of servlet-based SSM.

    Rolling Your Own (Solution 2)
    The custom solution will have the following design ideals:

  • Maximize the amount of functionality that can be leveraged from the platform (write as little code as possible)
  • Have all the features and performance of servlet-based SSM

    Since servlet-based SSM will serve as a model, let's review the major players involved:

  • HttpSession: Holder of session state. Any serializable object can be stored. WebLogic automatically replicates state to a secondary host.
  • Servlet container: Manages the HttpSession cache including the accessibility of HttpSession and garbage collection.
  • Plug-in: Load-balances requests and manages fail over.
  • Cookie: Used by the plug-in to route requests. Contains the primary and secondary hosts for routing.

    The solution will consist primarily of moving these responsibilities to other J2EE components and writing custom code where this is not possible. In place of the HttpSession will be a stateful session bean (SFSB) in the EJB container. All that's needed is a custom bean class that allows clients to get and set any serializable object representing session state. Note that SFSBs and HttpSessions share the same replication mechanisms. When their hosts fail, and in-memory replication is enabled, their state is available on a secondary host. Unlike HttpSessions, however, SFSBs state replication is controlled by the transactional context. This gives SFSBs a net advantage in keeping session state cohernt across the cluster.

    In managing the cache of SFSBs, the EJB container is far more flexible than its servlet counterpart. The EJB specification defines the bean life cycle but allows vendors to implement their own cache management schemes. In WebLogic, you can control how aggressively the trash is taken out. First, define a timeout value for the bean. This tells WebLogic when an unused SFSB can be removed either temporarily (via passivation) or permanently (via eviction). Second, choose a cache management policy. Under the not-recently-used (NRU) policy, SFSBs are only passivated when the cache is running out of space. Conversely, the least-recently-used (LRU) policy will passivate upon timeout even if the cache is not full. The timeout value also affects the time of eviction.

    As for fail over and load balancing, this will be managed by SFSB's remote stub object. Remote stubs are objects that act as proxies to the actual session bean. Clients invoke methods on these stubs, which in turn invoke on the actual session bean. In WebLogic, stubs are "replica-aware" objects capable of routing the request to the host containing the SFSB. Similar to cookies, they also contain primary and secondary host data used for routing. When a primary host fails, they automatically route to the secondary host and update themselves to reflect the change. If a host fails during a request, however, the remote stub will throw an exception. The client need only retry and the stub will automatically adjust itself and subsequently function correctly.

    Like HttpSessions, EJB stubs must be cached and made accessible to clients. Caching can be done by retrieving the "handle" from the EJB stub and storing it in its serialized form. The stub can be re-created at anytime by deserializing the handle. However, since SFSB instances and their corresponding stub objects have different life cycles, the stub cache may not be in sync with the SFSB to which they refer. Making use of the SFSB's call-back methods (ejbActivate, ejbPassivate, ejbCreate, ejbRemove) is the key to keeping the cache in sync with the SFSBs. Figure 4 shows the complete solution using the same application shown earlier.

    Implementation Caveats
    The EJB specification states that a SFSB instance must not be accessed concurrently or an exception is thrown by the container. WebLogic has extended the spec and allows concurrent client invocations via the <allow-concurrent-calls> element in the WebLogic deployment descriptor. When activated, access is serialized at the bean level. This feature only works for SFSBs whose transactions are container-managed.

    Notice that as in solution 1, you still need to manage your own cache sessions (containing EJB headless rather than HTTP cookies).

    This can be implemented in a variety of ways. The simplest is to make it a database cache. If performance is an issue, you can purchase a third-party hardware cache box which can give you better performance, since these tend to be memory-based caches. The Java Community Process is addressing caching through its jcache component; this will certainly fill a void in J2EE if and when it becomes standard.

    Accessing State Locally
    One of the keys to this design is that, as with servlets, request processing is sent to the data rather than vice-versa. The goal is to access session state locally. To achieve this, the SFSB mediates all communications between the client (protocol adapter) and the application (a collection of EJBs replicated in the cluster). Each coarse-grain service offered by the application can only be accessed by a Java object whose sole purpose is to call the service on behalf of the client. This object is called the service executor and employs a "Strategy design pattern." The client creates an instance of the service-specific executor and passes it to the SFSB. In turn, the SFSB invokes a generic method on the executor which ultimately calls the back-end service. Using this framework guarantees that the request made from the protocol adapter will be processed on the host that contains the session's state.

    Conclusion
    Whichever solution you choose, much can be leveraged from WebLogic and J2EE. This can make a big difference in delivering your product faster using dependable, out-of-the-box components.

    Resources

  • Gridley, Michael; Woolen, Rob; Emerson, Sandra L. (2002). J2EE Applications and BEA Weblogic Server. Prentice-Hall.
  • Gamma, Erich; Helm, Richard; Johnson, Ralph; Vlissides, John. (1994). Design Patterns: Elements of Reusable Object-Oriented Software. Addison-Wesley.
  • Failover and replication in a cluster: http://e-docs.bea.com/wls/docs70/cluster/failover.html
  • Wireless Village and the IMPS initiative: www.wireless-village.org
  • The SMS forum: http://smsforum.net
  • More Stories By Nick Maiorano

    Nick Maiorano is a Sun-certified J2EE architect with over 10 years of experience in software development. He is currently a senior developer and co-architect of a wireless, instant messaging application built atop J2EE WebKogic technology.

    Comments (0)

    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.


    @ThingsExpo Stories
    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...
    SYS-CON Events announced today that TidalScale 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. TidalScale is the leading provider of Software-Defined Servers that bring flexibility to modern data centers by right-sizing servers on the fly to fit any data set or workload. TidalScale’s award-winning inverse hypervisor technology combines multiple commodity servers (including their ass...
    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...
    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.
    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.
    Amazon is pursuing new markets and disrupting industries at an incredible pace. Almost every industry seems to be in its crosshairs. Companies and industries that once thought they were safe are now worried about being “Amazoned.”. The new watch word should be “Be afraid. Be very afraid.” In his session 21st Cloud Expo, Chris Kocher, a co-founder of Grey Heron, will address questions such as: What new areas is Amazon disrupting? How are they doing this? Where are they likely to go? What are th...
    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...
    In his Opening Keynote at 21st Cloud Expo, John Considine, General Manager of IBM Cloud Infrastructure, will lead you through the exciting evolution of the cloud. He'll look 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 ...
    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.
    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 ...
    Most technology leaders, contemporary and from the hardware era, are reshaping their businesses to do software. They hope to capture value from emerging technologies such as IoT, SDN, and AI. Ultimately, irrespective of the vertical, it is about deriving value from independent software applications participating in an ecosystem as one comprehensive solution. In his session at @ThingsExpo, Kausik Sridhar, founder and CTO of Pulzze Systems, will discuss how given the magnitude of today's applicati...
    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...
    As popularity of the smart home is growing and continues to go mainstream, technological factors play a greater role. The IoT protocol houses the interoperability battery consumption, security, and configuration of a smart home device, and it can be difficult for companies to choose the right kind for their product. For both DIY and professionally installed smart homes, developers need to consider each of these elements for their product to be successful in the market and current smart homes.
    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...
    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.
    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 ...
    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...
    SYS-CON Events announced today that CAST Software 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. CAST was founded more than 25 years ago to make the invisible visible. Built around the idea that even the best analytics on the market still leave blind spots for technical teams looking to deliver better software and prevent outages, CAST provides the software intelligence that matter ...
    SYS-CON Events announced today that Daiya Industry will exhibit at the Japanese 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. Ruby Development Inc. builds new services in short period of time and provides a continuous support of those services based on Ruby on Rails. For more information, please visit https://github.com/RubyDevInc.