Welcome!

Java IoT Authors: Elizabeth White, Pat Romanski, Stackify Blog, Yeshim Deniz, Liz McMillan

Related Topics: Java IoT, Mobile IoT

Java IoT: Article

Java J2EE Lite with Spring Framework

The combined burden of EJBs and coarse-grained component design has given the term test driven design a new meaning

To further drive this point home, let's assume a physical architecture of a J2EE application that uses the EJB approach. (This by no means is the only possible architecture, but it helps make some important points.) Figure 6 shows Web apps and some EJBs collocated on the same physical machine.

Just because Web apps and EJBs are collocated, three of these runtime services are not needed:

  • The remoting of EJBs is not necessary as Web apps will call EJBs locally.
  • Clustering of EJBs is achieved via replica-aware home and remote stubs. Again, since the caller (Web app) and the called object (EJB) are on the same machine, there is not much sense in using cluster-aware stubs. Note that HttpSession replication for making Web apps cluster-aware is still desirable though.
  • Typically, if EJBs are not remoted, then you wouldn't want to impose security restrictions at the EJB tier.
As for the following runtime services:
  • Caching of entity EJBs in the container, on the other hand, is a valuable benefit that may be considered depending on the use case at hand.
  • Transactions are the most commonly used service provided declaratively by the EJB container. Many developers prefer the declarative use of Container Managed Transactions (CMT) over programmatic transaction management.
  • Persistence is now a separate specification from the EJB specification (JSR-220) and therefore, in future, will not be a direct service of the EJB container. However, it is a valuable service that can save development time, and, in some cases perform better.
We see that just collocating causes some of the services provided by the EJB container to become redundant.

Therefore applications designed using EJBs will have to interject a container every time a service is invoked, even if it doesn't need 90% of the services it offers. In addition, it will be designed coarsely with the EJB's remote interface and bandwidth considerations. Compare this with a Spring-based design where the business classes are first designed, unencumbered by EJB technology, using all the power of OO design like inheritance and coding to interfaces. Once the business design is complete, then infrastructure services are applied to them on an as-needed basis.

However, many times collocation is not a good fit. Sometimes, depending on the use case, EJBs become a desired feature. For example, middle-tier bean caching necessitates the use of entity EJBs and clustering at an EJB level necessitates the use of session EJBs. For these and other reasons, let's look at how to access SLSBs from Spring.

Defining StatelessSessionBeans in the BeanFactory
SLSBs are widely used to provide the service façade in business applications. Sometimes it's not advisable to remove all SLSBs in favor of proxied objects. In fact, the non-invasive nature of Spring makes it a good candidate for phasing in its use. For that reason or other legitimate uses of SLSBs (like third-party SLSBs), it's easy to make Spring managed beans and SLSBs coexist in the same application.

In Listing 17 we will see how an SLSB can be wrapped in a Spring-managed bean that can be retrieved from the BeanFactory. In the listing the orderContext.xml file shows how a Spring-supplied class (SimpleRemoteStatelessSessionProxyFactoryBean) is injected with a jndiName and a JNDI location for telling it where a certain EJB lives.

The second point is the businessInterface property into which OrderService interface is injected. This is different from the EJBRemote interface and is just a Java interface that is also implemented by the POJO manager OrderServiceImpl.

Beyond that, it is business as usual. The SLSB can now be accessed just like any other bean managed by the Spring bean factory by using the handle orderServiceEJBProxy.

At this point, we have demonstrated that Spring can easily straddle an application that uses SLSBs at the service layer and service objects built from scratch using Spring.

Accessing the Bean Factory from a Web App
Now that we have defined services in the bean factory, how do we actually access the instance of those services? Spring provides several generic classes to make this task easier.

The ApplicationContext interface is at the root of the hierarchy of several interfaces whose implementers read the BeanFactory (or factories) into objects accessible by clients. The WebApplicationContext is one such interface for accessing the BeanFactory from a Web app, as shown in Listing 18.

First the web.xml of the Web app has the following lines supplied that tell the ServletContext about Spring's bean factory. The ContextLoaderListener loads up the Bean Factory configuration at Web app startup time. Next, the Order.jsp (that lives in the bundled Web app made2order.war) loads up the context from the ServletContext as shown in Listing 19. orderService and myProxiedServiceWithSeveralInterceptors are instances of singleton services that are defined in the BeanFactory.

Similarly, ClassPathXmlApplicationContext(paths) is another helper class that gets services from the BeanFactory as is used in the JUnit test suite's setup() method (see Listing 20). Note that a new BeanFactory can be swapped in at runtime by placing it in the classpath and destroying the current context.

Testing
Encouraging good testing practices is one of the value propositions of Spring. IoC is a good mechanism to encourage integration testing, whereas coding to interfaces is really what facilitates Unit testing.

In our example, jdbcOrderDAO can be easily replaced by a hibernateOrderDAO for conducting JUnit integration tests from within Eclipse, for example.

Similarly, instead of using a POJO manager implementation of OrderService, you can just as easily replace the implementation with the OrderEJB (which also implements the OrderService interface).

For unit testing on the other hand, EasyMock has been used. The class OrderServiceUnitTest only tests the service implementation by injecting a mock dao into the service layer. Since OrderDAO is an interface, this is easily achieved by casting the MockControl.getMock() method to return an OrderDAO instance. Then, by injecting the mockDAO to the service (setter injection), we can only test the code in the service. EasyMock uses the concept of recording and playback. The exact calls to the mockDAO are recorded first and then the test case is run. EasyMock asserts whether or not the playback is exactly the same as the one recorded.

For more detailed examples, look at the class OrderServiceUnitTest.java.

Because of the simple nature of this application, it certainly doesn't make it a good candidate for an example of test driven design. However, all else being equal, easily testable code will more be more likely to drive design than code that is hard to test.

Where Do We Go from Here
Spring provides an IoC and AOP-based framework to build infrastructure services. Even though we identified some EJB container-based services that are not needed; sometimes, depending on the use case, those services provide value. In those cases, remoting, security, and persistence can be applied to Spring proxy using the same mechanism that was used by transactions or auditing. This allows for open-ended growth where services are applied on an as-needed basis to certain Spring-managed service beans. The sample application does not show these additional features, however, the interested reader can explore Spring supplied support classes for:

  • Remoting using Caucho's Hessian and Burlap protocols
  • Security using Acegi
  • Persistence using Hibernate
There are several Spring classes for other services, which are not mentioned here.

Summary
We have implemented the separation-of-concerns pattern by isolating business code from infrastructure code via method interception and Spring proxies. In addition, using IoC and the convenience of a BeanFactory, we are able to inject different implementations into different service contracts.

By moving to a POJO-based business tier, our design is not encumbered by bandwidth considerations as with EJB design. The design of the applica-tion can be as object-oriented and fine-grained as needed because we are not dealing with issues of multiple inheritance or layering.

We looked at the scalability issues and concluded that we can apply several enterprise-level services on an as-needed basis while still remaining cluster-able at a Web app level.

We looked at testing strategies and showed that coding to interfaces allows for the easy use of third-party testing tools like EasyMock. IoC via interfaces allows for integration testing using a configurable bean factory.

In the end, made2order is easier to maintain because we have removed infrastructure code from our business code. It is easier to test because of ease of configuration using a bean factory approach and coding to interfaces. Last, it performs better compared to an EJB-based solution because we are not interjecting a one-size-fits-all container in our business operations.

References

More Stories By Pankaj Tandon

Pankaj Tandon is a software engineer at Crowncastle. His interests include object-oriented design and development and architecting the software process using open source technologies. He is a Sun Certified Java developer for the Java 2 platform. He lives in Pittsburgh, PA, plays guitar, and mixes music in his spare time.

Comments (6) 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
Pankaj Tandon 02/21/06 10:38:18 AM EST

I just realized that if you are here then you have already found the online version of the article.. here is the link to the downloadable artifact then..
http://res.sys-con.com/story/feb06/180377/source.html

Click on "Additional Code II" to get the full webapp+ code.

Pankaj Tandon 02/17/06 05:48:23 PM EST

Hi all,
I have received several emails from folks telling me about this snafu on JDJs part.
The link is there burried somewhere in the JDJ labyrinth.
You can get to it and some performance analysis at:
http://technochord.blogspot.com/2006/02/j2ee-lite-with-spring-framework....

Bob Raker 02/16/06 11:37:12 AM EST

This was a very good article. But, where the hell does one go to download listing 9-20. Thanks.

Rob Moore 02/14/06 12:14:11 PM EST

Is the sample code available?

Thanks,

Rob

SYS-CON Brazil News Desk 02/14/06 09:46:50 AM EST

J2EE applications of late have become weight conscious. The combined burden of EJBs and coarse-grained component design has given the term test driven design a new meaning: technology driven design! Fortunately a host of lightweight solutions are emerging, such as PicoContainer, Spring Framework, Hivemind, Hibernate, Castor, and Webwork. In this article I'll discuss my experience with the Spring Framework and how it can be used to make a J2EE application more maintainable, testable, and better performing.

SYS-CON India News Desk 02/13/06 02:55:13 PM EST

J2EE applications of late have become weight conscious. The combined burden of EJBs and coarse-grained component design has given the term test driven design a new meaning: technology driven design! Fortunately a host of lightweight solutions are emerging, such as PicoContainer, Spring Framework, Hivemind, Hibernate, Castor, and Webwork. In this article I'll discuss my experience with the Spring Framework and how it can be used to make a J2EE application more maintainable, testable, and better performing.

@ThingsExpo Stories
The current age of digital transformation means that IT organizations must adapt their toolset to cover all digital experiences, beyond just the end users’. Today’s businesses can no longer focus solely on the digital interactions they manage with employees or customers; they must now contend with non-traditional factors. Whether it's the power of brand to make or break a company, the need to monitor across all locations 24/7, or the ability to proactively resolve issues, companies must adapt to...
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.
Artificial intelligence, machine learning, neural networks. We’re in the midst of a wave of excitement around AI such as hasn’t been seen for a few decades. But those previous periods of inflated expectations led to troughs of disappointment. Will this time be different? Most likely. Applications of AI such as predictive analytics are already decreasing costs and improving reliability of industrial machinery. Furthermore, the funding and research going into AI now comes from a wide range of com...
"When we talk about cloud without compromise what we're talking about is that when people think about 'I need the flexibility of the cloud' - it's the ability to create applications and run them in a cloud environment that's far more flexible,” explained Matthew Finnie, CTO of Interoute, in this SYS-CON.tv interview at 20th Cloud Expo, held June 6-8, 2017, at the Javits Center in New York City, NY.
Internet of @ThingsExpo, taking place October 31 - November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA, is co-located with 21st Cloud Expo and will feature technical sessions from a rock star conference faculty and the leading industry players in the world. The Internet of Things (IoT) is the most profound change in personal and enterprise IT since the creation of the Worldwide Web more than 20 years ago. All major researchers estimate there will be tens of billions devic...
SYS-CON Events announced today that MobiDev, a client-oriented software development company, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. MobiDev is a software company that develops and delivers turn-key mobile apps, websites, web services, and complex software systems for startups and enterprises. Since 2009 it has grown from a small group of passionate engineers and business...
SYS-CON Events announced today that GrapeUp, the leading provider of rapid product development at the speed of business, will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Grape Up is a software company, specialized in cloud native application development and professional services related to Cloud Foundry PaaS. With five expert teams that operate in various sectors of the market acr...
SYS-CON Events announced today that Ayehu will exhibit at SYS-CON's 21st International Cloud Expo®, which will take place on October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara California. Ayehu provides IT Process Automation & Orchestration solutions for IT and Security professionals to identify and resolve critical incidents and enable rapid containment, eradication, and recovery from cyber security breaches. Ayehu provides customers greater control over IT infras...
In this presentation, Striim CTO and founder Steve Wilkes will discuss practical strategies for counteracting fraud and cyberattacks by leveraging real-time streaming analytics. In his session at @ThingsExpo, Steve Wilkes, Founder and Chief Technology Officer at Striim, will provide a detailed look into leveraging streaming data management to correlate events in real time, and identify potential breaches across IoT and non-IoT systems throughout the enterprise. Strategies for processing massive ...
SYS-CON Events announced today that Cloud Academy named "Bronze Sponsor" of 21st International Cloud Expo which will take place October 31 - November 2, 2017 at the Santa Clara Convention Center in Santa Clara, CA. Cloud Academy is the industry’s most innovative, vendor-neutral cloud technology training platform. Cloud Academy provides continuous learning solutions for individuals and enterprise teams for Amazon Web Services, Microsoft Azure, Google Cloud Platform, and the most popular cloud com...
In his session at Cloud Expo, Alan Winters, an entertainment executive/TV producer turned serial entrepreneur, presented a success story of an entrepreneur who has both suffered through and benefited from offshore development across multiple businesses: The smart choice, or how to select the right offshore development partner Warning signs, or how to minimize chances of making the wrong choice Collaboration, or how to establish the most effective work processes Budget control, or how to ma...
SYS-CON Events announced today that Enzu will exhibit at SYS-CON's 21st Int\ernational Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. Enzu’s mission is to be the leading provider of enterprise cloud solutions worldwide. Enzu enables online businesses to use its IT infrastructure to their competitive advantage. By offering a suite of proven hosting and management services, Enzu wants companies to focus on the core of their ...
We build IoT infrastructure products - when you have to integrate different devices, different systems and cloud you have to build an application to do that but we eliminate the need to build an application. Our products can integrate any device, any system, any cloud regardless of protocol," explained Peter Jung, Chief Product Officer at Pulzze Systems, in this SYS-CON.tv interview at @ThingsExpo, held November 1-3, 2016, at the Santa Clara Convention Center in Santa Clara, CA
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 CA Technologies has been named "Platinum Sponsor" of SYS-CON's 21st International Cloud Expo®, which will take place October 31-November 2, 2017, at the Santa Clara Convention Center in Santa Clara, CA. CA Technologies helps customers succeed in a future where every business - from apparel to energy - is being rewritten by software. From planning to development to management to security, CA creates software that fuels transformation for companies in the applic...
Amazon started as an online bookseller 20 years ago. Since then, it has evolved into a technology juggernaut that has disrupted multiple markets and industries and touches many aspects of our lives. It is a relentless technology and business model innovator driving disruption throughout numerous ecosystems. Amazon’s AWS revenues alone are approaching $16B a year making it one of the largest IT companies in the world. With dominant offerings in Cloud, IoT, eCommerce, Big Data, AI, Digital Assista...
Multiple data types are pouring into IoT deployments. Data is coming in small packages as well as enormous files and data streams of many sizes. Widespread use of mobile devices adds to the total. In this power panel at @ThingsExpo, moderated by Conference Chair Roger Strukhoff, panelists looked at the tools and environments that are being put to use in IoT deployments, as well as the team skills a modern enterprise IT shop needs to keep things running, get a handle on all this data, and deliver...
In his session at @ThingsExpo, Eric Lachapelle, CEO of the Professional Evaluation and Certification Board (PECB), provided an overview of various initiatives to certify the security of connected devices and future trends in ensuring public trust of IoT. Eric Lachapelle is the Chief Executive Officer of the Professional Evaluation and Certification Board (PECB), an international certification body. His role is to help companies and individuals to achieve professional, accredited and worldwide re...
With the introduction of IoT and Smart Living in every aspect of our lives, one question has become relevant: What are the security implications? To answer this, first we have to look and explore the security models of the technologies that IoT is founded upon. In his session at @ThingsExpo, Nevi Kaja, a Research Engineer at Ford Motor Company, discussed some of the security challenges of the IoT infrastructure and related how these aspects impact Smart Living. The material was delivered interac...
IoT solutions exploit operational data generated by Internet-connected smart “things” for the purpose of gaining operational insight and producing “better outcomes” (for example, create new business models, eliminate unscheduled maintenance, etc.). The explosive proliferation of IoT solutions will result in an exponential growth in the volume of IoT data, precipitating significant Information Governance issues: who owns the IoT data, what are the rights/duties of IoT solutions adopters towards t...