Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  Tutorial: Direct network traffic of EJBs - JavaWorld November 1999

Direct network traffic of EJBs - JavaWorld November 1999

Tutorial Details:

Direct network traffic of EJBs
Direct network traffic of EJBs
By: By Thomas Davis
Avoid bottlenecking by encapsulating bean properties into a single object
efore we get into the heart of this article, there are some things to keep in mind. You're going to need a firm understanding of Enterprise JavaBeans or remote objects in general to follow the discussion here. In order to avoid confusion, the source-code snippets are color-coded : green for the bean, red for the client, and blue for objects that are shared by both.
Network traffic
When dealing with remote objects, such as Enterprise JavaBeans (EJB), some of the biggest performance problems can be traced to network traffic. A method call that has to be marshaled over a socket connection is going to be much slower than a method call issued in local memory. The following piece of client code, which assumes that bean is a handle to an EJB, makes six remote calls (remember, red here stands for client code):
bean.setFirstName( "Thomas" );
bean.setLastName( "Davis" );
bean.setCity( "Boca Raton" );
bean.setState( "Florida" );
bean.setZipCode( "33487" );
bean.setCountry( "United States" );
This code has the potential to be a performance nightmare. Six calls to bean methods means six requests over the network. In the EJB implementations with which I have worked, all of the client-to-EJB communication within a single virtual machine instance takes place through a single socket connection. This is a bottleneck. When one client is talking to a bean, other clients (within the same virtual machine, sharing the single socket) are waiting for their turn. Waiting is bad. You want to avoid remote calls whenever possible.
Property encapsulation
One solution to this problem, the solution I am going to promote in this article, is to encapsulate the bean properties into a single object, and then send that entire object back and forth between client and server (over the network). This lets you put all of the get / set methods into the transported object and have them executed within the client's virtual machine -- in local memory rather than over the network. The properties object must be serializable in order to be transported over the network, and it should be as compact as possible to ensure the fastest possible transfer rate. Here is an example that encapsulates the properties that the above code modified (note that it's blue because it is shared between the client and the bean):
public class BeanProperties implements java.io.Serializable {
private String firstName, lastName, city, state, zipCode;
public void setFirstName( String firstName ){
this.firstName = firstName;
}
// more 'set' methods for the other string values...
}
Using this technique, you can reduce the previous client example to only two remote calls. The first call retrieves the properties object from the bean, while the second sends the properties object back to the bean:
BeanProperties props = bean.getProperties(); // remote call #1
props.setFirstName( "Thomas" );
props.setLastName( "Davis" );
props.setCity( "Boca Raton" );
props.setState( "Florida" );
props.setZipCode( "33487" );
props.setCountry( "United States" );
bean.setProperties( props ); // remote call #2
This code is going to run more quickly due to the reduced amount of network calls. And any other processes that are sharing the same network connection will also run more quickly because they won't be waiting as often for this client to relinquish the socket.
Synchronization issues
The properties encapsulation is a major improvement in performance, but it comes at a price -- we now have a synchronization issue. If two clients are working with the same bean, it is possible for the properties to get out of sync. I'll demonstrate this momentarily.
When a client requests the properties from the bean, the client actually receives a copy of the properties object. If a single client requests the properties object twice, it will receive two different instances of that object.
Observe the following code, explained below:
BeanProperties propsA = bean.getProperties();
BeanProperties propsB = bean.getProperties();
propsA.setFirstName( "Edward" );
propsB.setState( "Ohio" );
bean.setProperties( propsA );
bean.setProperties( propsB );
BeanProperties propsC = bean.getProperties();
System.out.println( propsC.getFirstName() ); // "Thomas" !!!
The client retrieves a copy of the bean's properties and assigns them to propsA , then immediately requests the properties again and assigns them to propsB . The client now has two copies of the bean's properties.
The client modifies the first name value in propsA , then changes the state value of propsB . Now each copy of the properties is unique -- one has a different first name and the other contains a different value for the state.
Next, the client sends the propsA copy back to the bean. This means that the remote bean now has a first name value of "Edward" . But then the client sends up the propsB copy. Now the bean has the state set to "Ohio" ... but the value for the first name has been reset to its original value! Why? Because propsB was a copy of the properties, and the first name was never changed for it. When we sent the propsB version back to the bean, the propsA version was overwritten; thus, the change to the first name was lost.
This is exactly what can happen when two different clients start modifying the properties of the same bean. This is called a race condition. Each client is essentially racing the other to update the bean's properties.
Synchronization solutions
If all of the clients were running in a single virtual machine, we could use the synchronization keyword in a strategic manner to ensure that only one client accessed the remote bean's properties at a time. However, the clients will most likely be running on completely different machines, and thus not within the same virtual machine.
One way to tackle this is to have the bean timestamp the properties object and check that timestamp each time the properties get passed back from the client. If the timestamps don't match up, in the case of the client passing back an old copy of the properties, then a race condition has occurred. Let's add some more logic to the BeanProperties class to facilitate a timestamp:
private long timestamp;
public void updateTimestamp(){
timestamp = System.currentTimeMillis();
}
public long getTimestamp(){
return timestamp;
}
Now the bean can brand the properties object with the current time. This will be done as soon as the object is created (which happens in the ejbLoad() method for beans):
private BeanProperties props;
public void ejbLoad() throws RemoteException {
// perform normal bean stuff, including loading state from database...
props = new BeanProperties();
// initialize properties based on database query...
props.updateTimestamp();
}
Now the bean can compare the timestamp of its local properties with the properties copies that are sent back from the client (via the setProperties() method). If the timestamps do not match, then there was a synchronization issue resulting from a race condition. Rather than apply the changes, and possibly lose data by overwriting the newer properties, the bean can throw an exception. Here's how the setProperties() method might look:
public void setProperties( BeanProperties newProps ) throws RemoteException, RaceConditionException {
if( newProps.getTimestamp() != props.getTimestamp() )
{
throw( new RaceConditionException( "Client attempted to apply old properties!" ) );
}
else
{
props = newProps;
props.updateTimestamp();
}
}
Addendum: November 5, 1999
In theory, there are two problems with this solution. I'll discuss both problems and then provide a uniform solution.
First, the bean could have been passivated after the properties were retrieved and then reactivated when the properties were returned. In this case, the time stamp would be reset, and the client would receive a false race condition exception. Thanks to reader Mike Klumpenaar for pointing this out.
Second, the ejbLoad() method is supposed to execute before every client-called method. In all of the EJB servers that I have evaluated, the container is intelligent enough to not call the load method unless the bean is dirty. However, because the code should be cross-platform and in compliance with the EJB specification, I should have accounted for such a scenario. Thanks to reader Richard Monson-Haefel, author of Enterprise JavaBeans (O'Reilly & Associates, 1999), for pointing this out.
The solution to both of these problems is to store the time stamp in the database along with the bean's state. This approach ensures that time stamp survives bean passivation/activation and precursory calls to the ejbLoad() method.
This solution requires that the client deal with all synchronization conflicts. In this particular case, the client has two options. If there is any user interaction, it can inform the user of the problem and query for its next action (for example, cancel or retry). If there is no user interaction, the client will have to retrieve the properties (the latest version of them) again from the bean, reapply the changes, and finally resend the properties back to the bean. Of course, it is possible that another client has outraced this client yet again, so this client may have to repeat this procedure until it finally succeeds. Here's an example of such a loop:
boolean bad = true;
while( bad ){
BeanProperties props = bean.getProperties();
props.setFirstName( "Thomas" );
// more properties setting...
try {
bean.setProperties( props );
bad = false;
} catch( RaceConditionException e ) {
// we have to loop again
}
}
Intelligence on the bean side
In order to relieve the stress of the client and avoid the possible looping scenario discussed in the last paragraph, we can add a little intelligence to the bean. The bean can figure out which p


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
Direct network traffic of EJBs - JavaWorld November 1999

View Tutorial:
Direct network traffic of EJBs - JavaWorld November 1999

Related Tutorials:

Displaying 1 - 50 of about 574 Related Tutorials.

Network Monitoring for Serious eCommerce
businesses use firewalls to protect their internal network from un-authorized traffic... Network Monitoring for Serious eCommerce Network Monitoring for Serious eCommerce
 
Eclipse plugin-Network
Eclipse plugin-Network Eclipse plugin-Network...; VNClipse-Network VNClipse offers a vnc implementation for eclipse. You can... to smaller traffic.          
 
Network Security Toolkit 1.4.0 is available is now
) into a system designed for network traffic analysis, intrusion detection, network... Network Security Toolkit 1.4.0 is available is now Network Security Toolkit 1.4.0 is available is now We
 
VoIP Long Distance
; VoIP Designing a Long-Distance Network The long-distance Voice over IP (VoIP) network solution is a set of network design... traffic distributed over VoIP. Calls originate in the Public Switched Telephone
 
VoIP Management
a full VOIP system's traffic across the network. Such measurements provide..., and maintain reliability across the VoIP network. Voice traffic is considerably more sensitive to network conditions than normal data traffic and your management
 
VoIP Firewall
. Under pressure from direct enterprise sales of VoIP platforms to their best... behind NATs and NAT-enabled firewalls. NATAccess performs network address... users, NATAccess allows authorized streaming VoIP traffic?and only authorized
 
Location Based Service (LBS)
on a different navigation technologies provided by the mobile communication network. Its... the vehicle operator and the service provider can also direct the taxi operator to the customer. The use of this service increases the communication traffic
 
IP Filter Example
to determining what traffic passes through the firewall based on IP address details. This protects the secure network from outsiders. A filter is an object
 
Open Source Firewall
network. A firewall can prevent unwanted access to departmental systems while... traffic patterns including denied connections. Installing and operating a firewall to protect a departmental LAN is only one of many criteria that Network
 
EasyEclipse Server Java
Java applications, such as JavaServer Pages, EJBs and Web Services. EasyEclipse
 
Multicast in Java
Multicast Broadcast 1. Unicast: This method solves network traffic problems... is not efficient for network traffic problem because it supports one-to-one transmission...: It provides better facility to solve a network traffic problems than unicast
 
Open Source program
Open Source Program Open Source program Applications for Open Sound System SLab Direct to Disk Recording Studio. Mixer 64-16-8-4-2 stereo/quadraphonic outputs. Includes WaveEditing, effects send busses
 
VoIP Papers
of Internet and voice traffic to form a single managed network.   ...; VoIP Tuning Services Packet The transfer of voice traffic over packet.... VoIP brings a new environment to the network technician that requires expanded
 
Ensemble Glider for Eclipse
J2EE application development and debugging. Create and test EJBs and JSPs without...-time environment for EJBs, JSPs and other J2EE components. Glider?s runtime
 
VoIP Data Networking
their tolerance to disruption. If other traffic traversing the network delays... networking lines. It's adapted to a packet-switched network just like your computer... network, including networks that don't connect to the rest of the Internet
 
Linux Firewall
Network (VPN), intrusion detection, and traffic management functions... to prevent some unauthorized Internet access from a private network prohibited by the security policy. It is  generally used to protect data from one network from
 
Tips: WiFi Security for Home Networks
concern for anyone setting up a WiFi network, as anyone who is close enough... network, whether at home or workplace. Here are a few tips that can help you... network, with a standard wireless router and one or more roaming access points
 
How GPS Can Be Used
; Fleet management and tracking Using the common cellular phone network... in the world. Traffic police can also use this in monitoring the high speed of any
 
VoIP Getting Started
With a converged network that supports both data and voice traffic, you must..., voice traffic travels over a public switched telephone network (PSTN), while...) networking comprises many components and network managers might find themselves
 
Java: Map Iteration
Java: Map Iteration Java: Map Iteration No direct iteration over Maps -- Get Set of keys or key-value pairs from Mapt Maps do not provide an iterator() method as do Lists and Sets
 
Map Iteration
Java: Map Iteration Java: Map Iteration No direct iteration over Maps -- Get Set of keys or key-value pairs from Mapt Maps do not provide an iterator() method as do Lists and Sets
 
Client Socket Information
information. This is a very simple program of java network. Here, we have used... =-1 TCP no Delay =false Traffic class =0 channel is =null
 
What is OSI Model?
Model is used to describe networks and network application. Layers of OSI ... messaging e-mail, virtual terminal access and network management. 6) Presentation... the host in the network. This layer is responsible for establishing and ending
 
Open Source Jobs
Open Source Jobs Open Source Jobs Open Source Professionals Search firm specializing in the placement of Linux / Open Source professionals, providing both contract and direct hire
 
VoIP Phone Systems
voice calls are sent over a computer network instead of traditional phone lines... phone traffic worldwide will be IP-based.       ... network like the Internet. VoIP equipment converts analog voice signals to data
 
VoIP Equipment
using a LAN or a data network like the Internet. VoIP equipment converts analog... or a complete set of network equipment including routers and phone adapters. Here... to blur as network equipment vendors move from proprietary hardware and software
 
VoIP Management Software
diverse network can all be billed and routed from one central web page. Converge..., cost-effective IP network delivery of voice, data and multimedia content... customers' quality of experience (QoE). For example, heavy traffic volume
 
Free Web Hosting - Why Its not good Idea
for hosting your web sites, specially when you are looking for getting high traffic... end paying for your hosting and you might loose traffic to your web site due
 
Java EJB 3.0 Tutorials

 
Netwiser
; Netwiser is a unique platform for network software development. It includes a graphical user interface, a network simulator, and a portable C++ framework for creating all types of network-intensive applications. Netwiser eases
 
Web Site promotion services at roseindia.net
in major search engines and directory of the world. Our own site traffic
 
Open Source Jobs
Open Source Jobs Open Source Jobs Open Source Professionals Search firm specializing in the placement of Linux / Open Source professionals, providing both contract and direct hire
 
Get IP Example
; This example shows you ip address of your network interface. Description of the code : NetworkInterface.getNetworkInterfaces() : Network Interface... InterfaceAddresses of this network interface. NetworkInterface.getDisplayName
 
Get IP Address Example
;   This example shows you ip address of your network interface...() : Network Interface class is used for get name, and a list of IP addresses assigned... of all InterfaceAddresses of this network interface
 
JViz
is Document, the other is Network. For more information: http
 
JSF Global Navigation Example
about the JSF Global navigation which makes the direct navigation between two
 
Core Java Interview Question Page 1
: How could Java classes direct program messages to the system console
 
Enhancements in Networking Features
a Network Interface and it is made up of a name and a IP addresses list assigned... to system's network adapters. This includes information like broadcast address, list... boolean isUp() Above method returns true if the network interface is up
 
Open Source Jobs
Open Source Jobs Open Source Jobs Open Source Professionals Search firm specializing in the placement of Linux / Open Source professionals, providing both contract and direct hire
 
WiFi Internet
to connect to the internet without a cabled network. WiFi technology uses... that these devices are interoperable. In a WiFi network, computers with a WiFi network card can connect wirelessly to a WiFi router. The router is connected
 
Get System Ip
to a different system involved in the network for communication. The Format for a IP address... NetworkInterface,this class represents a Network Interface made up of a name...; -  This method returns you name of the network interface. 3)getInet Addresses
 
LBS
navigation technologies provided by the mobile communication network. Its... components like mobile devices, mobile communication network, service provider like... services by means of a wireless device functioning through common cellular network
 
LBS
navigation technologies provided by the mobile communication network. Its... components like mobile devices, mobile communication network, service provider like... services by means of a wireless device functioning through common cellular network
 
Open Source Router
can direct data traffic for a giant corporation as easily as it can manage a home Wi-Fi network. And that?s what makes it as disruptive as a leaf blower... for every type of network users, from academic researchers to enterprises and home users
 
JSP Interview Questions
? Answer: JSP actions are XML tags that direct the server to use existing
 
What is SNTP?
; SNTP is the abbreviation of  Simple Network Time Protocol that is used... internet. SNTP is the less complicated version of  NTP (Network Time... Standard. This system is suitable for  less complicated network system
 
Java example program to get IP address of own system
identification number which is assigned to the devices participating in the network
 
Serializable Exception
that  is transmitted through the network in the encoded stream of byte form. This Encoded form can be reconstructed from the network by reading from byte... the object in file and transmitting the object  through network. It does not write
 
Open Source Code
network, normally using HTTP. SOAP forms the foundation layer of the Web services... the most common is the Remote Procedure Call (RPC) pattern, where one network node... We believe that the Gnutella Network could be and should be one of the core
 
Java - Drawing Shapes Example in java
but it is a network level program. The Applet class is a super class  of any applet
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.