Deployment Descriptor
This deployment descriptor does not differ at all from an EJB 1.1 deployment descriptor.
<?xml version="1.0"?>
<ejb-jar>
<enterprise-beans>
<entity>
<ejb-name>Measurement</ejb-name>
<home>measurements.MeasurementHome</home>
<remote>measurements.Measurement</remote>
<ejb-class>measurements.MeasurementEJB</ejb-class>
<persistence-type>Bean</persistence-type>
<prim-key-class>java.lang.Long</prim-key-class>
<reentrant>False</reentrant>
<resource-ref>
<res-ref-name>jdbc/DefaultDS</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</entity>
</enterprise-beans>
<assembly-descriptor>
<container-transaction>
<method>
<ejb-name>Measurement</ejb-name>
<method-name>*</method-name>
</method>
<trans-attribute>Required</trans-attribute>
</container-transaction>
</assembly-descriptor>
</ejb-jar>
Client
This client creates some sample measurements, and then retrieves some summary data using two
methods. First, it uses a finder method and iterates through the bean instances. Second, it uses the home
business method that delegates the summary functionality to the database:
package measurements;
import java.util.Date;
import java.util.Random;
import java.util.Collection;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.ejb.CreateException;
public class Client {
private static long currentId = 1;
private static Random random = new Random();
private static long getNextId() {
return currentId++;
}
private static void createSampleMeasurements(MeasurementHome mh,
Date timestamp, String eventName,
int quantity) throws
CreateException, RemoteException {
for (int iter = 0; iter < quantity; iter++) {
mh.create(getNextId(),
timestamp, eventName, random.nextDouble());
}
}
public static void main(String[] args) {
try {
InitialContext initial = new InitialContext();
MeasurementHome measurementHome =
(MeasurementHome) PortableRemoteObject
.narrow(initial.lookup("Measurement"),
MeasurementHome.class);
Date currentTimestamp = new Date();
createSampleMeasurements(measurementHome,
currentTimestamp,
"TestEvent", 25);
Note: Color coded lines should be on one line, they were split for formatting purposes.
With the first method, we use a finder to iterate through the measurements we care about:
Collection measurements =
measurementHome.findInDate(currentTimestamp,
currentTimestamp);
double total = 0.0;
Iterator iter = measurements.iterator();
while (iter.hasNext()) {
Measurement measurement =
(Measurement)
PortableRemoteObject.narrow(iter.next(),
Measurement.class);
total += measurement.getEventMagnitude();
}
System.out.println("Results from method one: " + total);
Note: Color coded lines should be on one line, they were split for formatting purposes.
With the second method, we use our home interface method:
System.out.println("Results from method two: "
+ measurementHome.totalMagnitudeForRange
(currentTimestamp,
currentTimestamp));
} catch (Exception e) {
e.printStackTrace();
}
}
}
Note: Color coded lines should be on one line, they were split for formatting purposes.
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.