Java IoT Authors: Elizabeth White, Yeshim Deniz, Zakia Bouachraoui, Liz McMillan, William Schmarzo

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
Nicolas Fierro is CEO of MIMIR Blockchain Solutions. He is a programmer, technologist, and operations dev who has worked with Ethereum and blockchain since 2014. His knowledge in blockchain dates to when he performed dev ops services to the Ethereum Foundation as one the privileged few developers to work with the original core team in Switzerland.
In his general session at 19th Cloud Expo, Manish Dixit, VP of Product and Engineering at Dice, discussed how Dice leverages data insights and tools to help both tech professionals and recruiters better understand how skills relate to each other and which skills are in high demand using interactive visualizations and salary indicator tools to maximize earning potential. Manish Dixit is VP of Product and Engineering at Dice. As the leader of the Product, Engineering and Data Sciences team at D...
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...
Bill Schmarzo, author of "Big Data: Understanding How Data Powers Big Business" and "Big Data MBA: Driving Business Strategies with Data Science," is responsible for setting the strategy and defining the Big Data service offerings and capabilities for EMC Global Services Big Data Practice. As the CTO for the Big Data Practice, he is responsible for working with organizations to help them identify where and how to start their big data journeys. He's written several white papers, is an avid blogge...
René Bostic is the Technical VP of the IBM Cloud Unit in North America. Enjoying her career with IBM during the modern millennial technological era, she is an expert in cloud computing, DevOps and emerging cloud technologies such as Blockchain. Her strengths and core competencies include a proven record of accomplishments in consensus building at all levels to assess, plan, and implement enterprise and cloud computing solutions. René is a member of the Society of Women Engineers (SWE) and a m...
Andrew Keys is Co-Founder of ConsenSys Enterprise. He comes to ConsenSys Enterprise with capital markets, technology and entrepreneurial experience. Previously, he worked for UBS investment bank in equities analysis. Later, he was responsible for the creation and distribution of life settlement products to hedge funds and investment banks. After, he co-founded a revenue cycle management company where he learned about Bitcoin and eventually Ethereal. Andrew's role at ConsenSys Enterprise is a mul...
Whenever a new technology hits the high points of hype, everyone starts talking about it like it will solve all their business problems. Blockchain is one of those technologies. According to Gartner's latest report on the hype cycle of emerging technologies, blockchain has just passed the peak of their hype cycle curve. If you read the news articles about it, one would think it has taken over the technology world. No disruptive technology is without its challenges and potential impediments t...
If a machine can invent, does this mean the end of the patent system as we know it? The patent system, both in the US and Europe, allows companies to protect their inventions and helps foster innovation. However, Artificial Intelligence (AI) could be set to disrupt the patent system as we know it. This talk will examine how AI may change the patent landscape in the years to come. Furthermore, ways in which companies can best protect their AI related inventions will be examined from both a US and...
Bill Schmarzo, Tech Chair of "Big Data | Analytics" of upcoming CloudEXPO | DXWorldEXPO New York (November 12-13, 2018, New York City) today announced the outline and schedule of the track. "The track has been designed in experience/degree order," said Schmarzo. "So, that folks who attend the entire track can leave the conference with some of the skills necessary to get their work done when they get back to their offices. It actually ties back to some work that I'm doing at the University of San...
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...