Java Reference
In-Depth Information
msgList[0] = "Hello!";
msgList[1] = "Welcome to the EJB world.";
msgList[2] = "Enjoy reading.";
}
public void ejbRemove() {
System.out.println ("ejbRemove called.");
}
public void ejbPassivate() {
System.out.println("ejbPassivate called.");
}
public void ejbActivate() {
System.out.println("ejbActivate() called.");
}
// Business methods serving the client's need
public String[] getAllWelcomeMsgs() {
return msgList;
}
public String getWelcomeMsg(int i) {
// first make sure index is not out of range
i = (i >= msgList.length) ? (msgList.length - 1) : i;
i = (i < 0) ? 0 : i;
// return welcome message
return msgList[i];
}
public int getNumberOfWelcomeMsgs() {
return msgList.length;
}
}
Where is the database-access code? After all, this is a book on Java database programming. Since EJB
itself is complex enough, I have deliberately kept the first EJB example as simple as possible (the spirit
of the HelloWorld example). As soon as you have this piece of code running, you can easily extend it
to fulfill your needs, such as accessing databases using the JDBC programming skills you have learned
from previous chapters. For example, the welcome messages may be stored in a database table. Then
in the ejbCreate method, you do not need to initialize the msgList array; instead, an instance of
javax.sql.DataSource should be initialized. And the business methods access the DataSource
and retrieve the welcome message (or count the number of rows) from the message table. You can find
code samples of initializing and accessing DataSource objects in next chapter .
After the bean classes are coded, they need to be packaged and deployed. Most application servers
have built-in tools for EJB (and enterprise application) deployment. The key artifacts of the deployment
process are the deployment descriptors, the XML files that contain the declarative information about the
EJBs and the enterprise application. Most of the container vendors use some proprietary technology to
enhance the performance of their product. As a consequence, the deployment descriptor usually
contains two files: one is the strictly J2EE standard, and the other is vendor specific. As an example, the
Search WWH ::




Custom Search