Welcome!

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

Related Topics: Weblogic

Weblogic: Article

J2EE Framework for Services

J2EE Framework for Services

This article presents a lightweight framework for building a service-oriented architecture (SOA) on top of J2EE.

The architecture is a subset of a framework that we have successfully applied in various J2EE applications. The framework is built from a combination of commonly known design patterns (GoF = Gang of Four; j2eecp = J2EE Core Patterns):

  • Factory (GoF)
  • Proxy (GoF)
  • Service Locator (j2eecp)
  • Business Delegate (j2eecp)
  • Service Facade (j2eecp)
At first glance, the framework may seem to require too many artifacts, but we will justify these as we go along. We will also see that most of the artifacts can be generated.

What Is Service-Oriented Architecture?
SOA is a framework for building IT solutions in the form of services. A service encapsulates some functionality exposed through one or more well-documented interfaces. The interfaces may be published and discoverable through repositories.

Could We Use Web Services?
A popular technique for communication, publishing, and discovery of services is the use of Web services and UDDI. Web services work well as an interapplication framework where the service interfaces provide only coarse-grained methods. However, Web services are too resource intense for intra-application communication. To leverage the idea of services inside a J2EE application, we need a framework with much less overhead.

Example
To illustrate the framework, we will create a very simple service that implements a number guessing game.

The number guessing game has two methods:

  • The method start starts (or restarts) the game and sets the upper and lower boundary for possible guesses. The service generates a random secret number that it stores internally.
  • The method guess is called when a client wants to guess the value of the secret number. The method returns the value -1 if the guess is too low, 0 if the guess is correct, and 1 if the guess is too high.

    To implement this simple service, we create the following artifacts:

  • Service Interface: Declaration of the interface for the service
  • Service Factory: Used by service clients and session beans to instantiate services
  • Session Bean Proxy: Client-side implementation of the service interface that forwards to a session bean
  • Business Bean: The "real" service implementation
  • Mockup Bean: A "fake" service implementation
  • Session Bean: A standard EJB session bean enabling enterprise-strength declaration of services (security, transactions, remote communication, etc.)

    The Client's View
    One goal of the framework is to make it easy for clients to obtain and use services. A client could be a Web component (JSP, servlet, or Struts action), a stand-alone application, or a business component running inside the EJB tier.

    In our framework, the clients obtain services through a service factory. The service factory returns an object implementing an interface that the client expects. This shields the clients from the complexity of J2EE (JNDI lookup, narrowing, etc.). The following code fragment shows how a client obtains the number guessing game service:

    NumberGuessingGame game = (NumberGuessingGame)
    ServiceFactory.lookup("NumberGuessingGame");

    With the clients' independence of J2EE artifacts now achieved, it is significantly easier to change the underlying implementation. This allows us to post-optimize the implementation of the number guessing game without affecting the client.

    The Implementer's View
    Ideally, the implementer of the business logic should also be shielded from the details of the EJB technology. The framework enables the business logic to be implemented as a standard JavaBean (see Figure 3).

    The implementer would typically provide two implementations of the service interface. The first implementation is a simple mockup that allows its clients to develop their logic without waiting for the real implementation to be complete. For this simple game, such a mockup is not justified, but it is very useful in enterprise applications where the development of the service may take a long time. A mockup reduces the risk of integration problems, especially when several teams concurrently implement services (see Figure 4).

    A simple mockup implementation of the guess method could generate a random number from -1 to 1:

    class NumberGuessingGameMockup implements NumberGuessingGame {
    public NumberGuessingGameMockup () {}
    public void start(int min, int max) {}
    public int guess(int aGuess) throws
    TechnicalException {
    return (((int)(Math.random() * 3.0)) - 1);
    }
    }

    Later we would follow up with the real implementation shown in Listing 1.

    The J2EE Connector Implementation
    To enable a remote client to communicate with the business logic, we must provide an implementation of a proxy that has the service interface, but forwards to the business logic. This requires several artifacts in J2EE.

    A proper port into the EJB container is a session bean, which provides server-side activation of the business logic. It receives remote business requests from clients, looks up the business logic bean, and forwards the business requests to it. The session bean is implemented according to standard EJB rules (see Figure 5).

    Notice that the session bean has a reference to a service implementing the NumberGuessingGame interface. When the session bean is activated, it uses a server-side service factory to create the service:

    void ejbCreate() {
    this.service =
    (NumberGuessingGame) ServiceFactory.lookup("NumberGuessingGame");
    }

    When a business method is invoked, the session bean simply forwards the request to the service:

    int guess(int theGuess) throws TechnicalException {
    return this.service.guess( theGuess );
    }

    To provide the client view we discussed earlier, we must also provide a client-side implementation that hides the use of the session bean from the client. We call this implementation a session bean proxy. In the core J2EE patterns, this implementation corresponds to the business delegate pattern.

    The session bean proxy created for the client acts quite similarly to the session bean. Upon creation, it accesses JNDI to look up the home and creates the remote proxy from the home (see Listing 2).

    The implementation of business methods on the session bean proxy forwards requests to the remote object. If a remote exception occurs, the exception is rethrown as a technical exception. For example:

    public int guess(int theGuess) throws TechnicalException {
    try {
    return this.remote.guess( theGuess );
    }
    catch (RemoteException e ) {
    throw new TechnicalException( e );
    }
    }

    We rethrow the remote exception to avoid exposing the underlying technology.

    The Service Factory
    The service factory is responsible for instantiating the correct implementation of a service based on the context in which it is being requested. There are many possible implementations. For our example we use simple property files to store name-value pairs. The name identifies the service and the value holds the name of the class implementing the service (the business object, the mockup implementation, or the session bean proxy). The service factory uses reflection to instantiate the proper implementation (see Listing 3).

    Every J2EE container has its own instance of service factory with a different configuration (defined by the property files).

    Putting It Together
    The framework can be seen as a layered architecture. The real communication is between the client and the business implementation. To add J2EE enterprise services (such as transactions, security, etc.), we may instantiate a session bean proxy/session bean layer. If the client and the business logic live in two different containers, we may use remote proxies (remote object + ejb object) provided by J2EE.

    The collaboration diagram in Figure 7 illustrates a runtime scenario. In this scenario, a client running in a remote container uses a business service implemented in an EJB container. The business service uses another service (dependent business service) running in the same container.

    In Figure 7:
    1.   The client requests a service from the client-side service factory.
    2.   The service factory creates the session bean proxy.
    3.   The service factory uses JNDI to look up the home and create the remote object.
    4.   The client invokes a business method on the session bean proxy.
    5.   The session bean proxy uses the remote object for the session bean obtained in step 3 to forward the business request.
    6.   The session bean uses the server-side factory to create the business service.
    7.   The server-side factory creates the business service and returns it to the session bean.
    8.   The session bean forwards the business request to the business service.
    9.   The business service needs another service to fulfill the business request. It asks the server-side factory for the dependent service.
    10.   The server-side factory creates the requested service (no session bean is required).
    11.   The business service calls business services on its dependent service.

    In our simple example, the number guessing game does not need help from other services, so steps 9 through 11 are not required.

    Generative Programming
    We seem to have created many artifacts to implement a simple service. The effort can be greatly minimized by use of generative programming. We can write a program to generate the session bean (home interface, remote interface, and the session bean implementation; XML fragments for the deployment descriptor) and the session bean proxy. Here are a few approaches that we have had success with:

  • Create a simple program that uses reflection upon the business logic bean or the service interface
  • Write a script in our favorite case tool
  • Write an explicit tool to generate the J2EE connectors (as in OMG's Model Driven Architecture)

    Conclusion
    In this article, we have presented an approach to service-oriented architecture that yields the following benefits:

  • Shields developers from the complexity of J2EE: The J2EE code can be generated.
  • Clients only see the factory and the interface to the service.
  • Business logic developers use standard JavaBeans to implement the interface.
  • Offers support for concurrent development: Service providers can offer mockup implementations for the service early in the project's life cycle that enable dependents to continue their work prior to the completion of the real service implementation.
  • Avoids session bean call overhead on service-to-service calls: The server-side factory would only issue business logic beans, removing the overhead of calling through a session bean (this advantage is minimal with EJB 2.0's local beans).
  • Minimize impact of post-optimization: We now have a layer of code running on the client side. We can easily move algorithms and data to the client side (either by adding implementation to the session bean proxy or by instantiating the service on the client side).

    A complete implementation of the example can be downloaded from www.sys-con.com/weblogic/sourcec.cfm.

  • More Stories By Petter Graff

    Petter Graff is Vice President of InferData Corporation. He has more than 20 years of experience building object-oriented software solutions. The last 10 years he has been teaching and consulting on enterprise architectures for Fortune 500 companies worldwide.

    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
    "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.
    It is of utmost importance for the future success of WebRTC to ensure that interoperability is operational between web browsers and any WebRTC-compliant client. To be guaranteed as operational and effective, interoperability must be tested extensively by establishing WebRTC data and media connections between different web browsers running on different devices and operating systems. In his session at WebRTC Summit at @ThingsExpo, Dr. Alex Gouaillard, CEO and Founder of CoSMo Software, presented ...