Java IoT Authors: Pat Romanski, Yeshim Deniz, Zakia Bouachraoui, Elizabeth White, Liz McMillan

Related Topics: Java IoT

Java IoT: Article

Fitting the Pieces into the Enterprise Java Puzzle

Fitting the Pieces into the Enterprise Java Puzzle

The story so far: In Part 1 (JDJ Vol. 6, issue 4), I covered servlets and gave a practical demonstration of how a basic access control mechanism for intranet applications could be built using Servlet Session Tracking and HTTP Authentication. In Part 2 (Vol. 6, issue 5), I introduced a couple of applets into the architecture and showed how a communication channel could be established between the applets and servlets that comprised the application.

The next version of my application will include the ability for an engineer to transfer tasks to another engineer when he goes on vacation or when he's sick. This added functionality requires a servlet to present the transfer form and subsequently to process the form submission. An Enterprise JavaBean (session) will perform the actual transfer function.

Task Transfer Servlet
Figure 1 shows what the Task Transfer Form looks like. You can type the name of a recipient engineer and submit the form to initiate the transfer. If the transfer succeeds, the user is presented with a confirmation page (not shown).

I will be using a single servlet to support two functions: presenting the transfer form, and handling the form submission. One servlet, two uses, distinguished by invoking the servlet through its doGet() or doPost() method.

The initial form presentation is performed by the doGet() method of the new servlet, which I've called TransferServlet. Listing 1 provides the code for this method, and for clarity I've delegated the construction of the HTML form to a separate displayForm() function to facilitate the representation of the form in the case of user error. The first time around, the form is displayed with a null error message via the call displayForm(out.user,null).

When the user submits the form, the submission is handled by the same servlet's doPost() method given in Listing 2. This method first gets hold of the engineer to which the transfer should be targeted. If no engineer was specified, it redisplays the form with an error message - displayForm(out,user, "You must specify an engineer") - otherwise the transfer goes ahead courtesy of an Enterprise JavaBean.

Simple EJB Solution
I've created the simplest possible Enterprise JavaBean to perform the transfer of tasks from one engineer to another. The TaskManager EJB is referenced by name via JNDI, and its home interface is obtained. The home interface is used to create an instance of the bean, and this instance is instructed to transfer the tasks. I said that this is the simplest possible EJB, and it is: the remote interface has only one method - transferTasks(...).

The EJB implementation in Listing 3 is a minimal session bean with no transactional attributes, and a single method with a single JDBC statement to manipulate the usertasks database table. I've kept it simple not only for illustration, but also because it's often the best approach. The sole SQL statement succeeds and is committed - or it fails and is rolled back. Either way, transactional integrity is assured.

When presented like this, EJBs look easy, so to spice things up I'll suggest another way of achieving exactly the same functionality.

The Alternative EJB Solution
This time, I'll use an entity bean to represent each individual engineer and I'll rework the TaskManager session bean to coordinate the two entity beans that represent the two engineers. The remote interface for an engineer (entity) bean has the method definitions shown in Listing 4. For any given engineer you can count his tasks, get his tasks, delete them, and assign new ones.

In case you're wondering, the sometimesFail parameter on the assignTasks() method was my way of testing this. If set to true, this parameter causes the method to throw an exception once in a while, which triggers the entire transfer operation to be rolled back.

The TaskManager session bean now has a revised version of the transferTasks method, given in Listing 5. It looks up the two entity beans representing the from- and to- Engineer(s), gets the tasks of the fromEngineer, deletes them, and then adds them one-by-one to the toEngineer.

Adopting this approach introduces many points at which the transactional integrity might be compromised. The deleteTasks call - or any one of the assignTask calls - might fail, leaving the database in an inconsistent state. Rest assured that you could make it work by setting the transactional properties of the participating session and entity beans, in which case the EJB container will call the ejbLoad and ejbStore methods (assuming bean-managed persistence) on the entity beans at appropriate times to assure the transactional integrity.

This leaves us with (at least) two ways to implement the transferring of tasks between engineers using Enterprise JavaBeans; one way being considerably easier to implement - and more reliable - and the other showing the power and complexity of EJBs. In general, my suggestion would be: beware the consultant who dives straight into the second approach without considering the simpler solution, although, of course, there are times when only a complex solution will do.

Fitting the Pieces into the Puzzle
Having presented the three major architectures offered by the J2EE (the servlet, applet, and EJB) in a way that hopefully shows that these three approaches may be complementary rather than competitive, you might now have some good ideas about the direction in which to take your project. But you might still be a little unsure about the answer to the essential question: Which horse for which course?

I can only give an opinion, but the order in which I have covered these technologies is no accident. For any Internet/intranet application, I would, by default, assume the servlet-only approach initially as providing the thinnest possible clients and the lowest common denominator for compatibility with client Web browsers.

For very limited client-side validation and control, I might stick with the HTML/servlet approach, add some JavaScript, and stop there. In all other situations that necessitated highly interactive or graphical clients, I would use applets with the straightforward applet-servlet communication mechanism based on serialized objects.

In situations that demand true, stateful, remotely referenced server-side objects I would consider Enterprise JavaBeans. I would try to keep my EJBs as simple as possible, and that's where the irony is. The real benefits of the EJB model come through using the standard services for transactions, security, and so on. So if you're not going to use these services - in the name of keeping it simple - you might be better off sticking with simple RMI. In a nutshell, don't treat EJBs as a half-hearted solution; either find an easier method or commit to the EJB approach with all your heart.

I've not yet mentioned CORBA as a candidate for client/server communication or even as an alternative to EJBs. In the time that it took RMI to mature and EJB to take off, CORBA - in the form of the Visigenic (Visibroker) or Iona (Orbix) Java ORB implementations - provided a solution that was more comprehensive than RMI and ahead of EJB. Now that niche has been filled by pure Java technologies and I see CORBA as being limited to the one remaining niche for which it is uniquely suited - legacy integration.

The Three Approaches
I've taken a tour of the three major architecture styles for application development that are available to Enterprise Java developers - namely servlets, applets, and EJB - and I've presented my ideas on how applications may be built comprising all three approaches, with some interesting diversions into areas such as access control. I've fitted the big pieces into the puzzle by offering my advice on which pieces should be inserted first and which later, if at all.

I'll leave you with one final thought. There are a few smaller pieces such as XML and JSP that could be placed into the gaps between the big ones to complete the final puzzle, as shown in Figure 2.

More Stories By Tony Loton

Tony Loton works through his company - LOTONtech Limited (www.lotontech.com) - as an independent consultant, course instructor, and technical author. He has a degree in computer science and management and has spent 10 years in I.T. The last five years have been devoted almost exclusively to Java, UML, and related technologies.

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.

IoT & Smart Cities Stories
CloudEXPO | DevOpsSUMMIT | DXWorldEXPO are the world's most influential, independent events where Cloud Computing was coined and where technology buyers and vendors meet to experience and discuss the big picture of Digital Transformation and all of the strategies, tactics, and tools they need to realize their goals. Sponsors of DXWorldEXPO | CloudEXPO benefit from unmatched branding, profile building and lead generation opportunities.
All in Mobile is a place where we continually maximize their impact by fostering understanding, empathy, insights, creativity and joy. They believe that a truly useful and desirable mobile app doesn't need the brightest idea or the most advanced technology. A great product begins with understanding people. It's easy to think that customers will love your app, but can you justify it? They make sure your final app is something that users truly want and need. The only way to do this is by ...
Digital Transformation and Disruption, Amazon Style - What You Can Learn. Chris Kocher is a co-founder of Grey Heron, a management and strategic marketing consulting firm. He has 25+ years in both strategic and hands-on operating experience helping executives and investors build revenues and shareholder value. He has consulted with over 130 companies on innovating with new business models, product strategies and monetization. Chris has held management positions at HP and Symantec in addition to ...
Dynatrace is an application performance management software company with products for the information technology departments and digital business owners of medium and large businesses. Building the Future of Monitoring with Artificial Intelligence. Today we can collect lots and lots of performance data. We build beautiful dashboards and even have fancy query languages to access and transform the data. Still performance data is a secret language only a couple of people understand. The more busine...
The challenges of aggregating data from consumer-oriented devices, such as wearable technologies and smart thermostats, are fairly well-understood. However, there are a new set of challenges for IoT devices that generate megabytes or gigabytes of data per second. Certainly, the infrastructure will have to change, as those volumes of data will likely overwhelm the available bandwidth for aggregating the data into a central repository. Ochandarena discusses a whole new way to think about your next...
DXWorldEXPO LLC announced today that Big Data Federation to Exhibit at the 22nd International CloudEXPO, colocated with DevOpsSUMMIT and DXWorldEXPO, November 12-13, 2018 in New York City. Big Data Federation, Inc. develops and applies artificial intelligence to predict financial and economic events that matter. The company uncovers patterns and precise drivers of performance and outcomes with the aid of machine-learning algorithms, big data, and fundamental analysis. Their products are deployed...
Cell networks have the advantage of long-range communications, reaching an estimated 90% of the world. But cell networks such as 2G, 3G and LTE consume lots of power and were designed for connecting people. They are not optimized for low- or battery-powered devices or for IoT applications with infrequently transmitted data. Cell IoT modules that support narrow-band IoT and 4G cell networks will enable cell connectivity, device management, and app enablement for low-power wide-area network IoT. B...
The hierarchical architecture that distributes "compute" within the network specially at the edge can enable new services by harnessing emerging technologies. But Edge-Compute comes at increased cost that needs to be managed and potentially augmented by creative architecture solutions as there will always a catching-up with the capacity demands. Processing power in smartphones has enhanced YoY and there is increasingly spare compute capacity that can be potentially pooled. Uber has successfully ...
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...
When talking IoT we often focus on the devices, the sensors, the hardware itself. The new smart appliances, the new smart or self-driving cars (which are amalgamations of many ‘things'). When we are looking at the world of IoT, we should take a step back, look at the big picture. What value are these devices providing. IoT is not about the devices, its about the data consumed and generated. The devices are tools, mechanisms, conduits. This paper discusses the considerations when dealing with the...