CHAPTER 2 Starting a project There are several

CHAPTER 2 Starting a project There are several interesting things to observe in this implementation. First, it s a plain Java class with no hard dependencies on any other package. It becomes an EJB only with a single metadata annotation, @Stateless. EJBs support container- managed services, so you can apply the @PersistenceContext annotation, and the server injects a fresh EntityManager instance whenever a method on this stateless bean is called. Each method is also assigned a transaction automatically by the container. The transaction starts when the method is called, and commits when the method returns. (It would be rolled back when an exception is thrown inside the method.) You can now modify the HelloWorld main class and delegate all the work of storing and showing messages to the MessageHandler. Running the application The main class of the Hello World application calls the MessageHandler stateless session bean after looking it up in the JNDI registry. Obviously, the managed environment and the whole application server, including the JNDI registry, must be booted first. You do all of this in the main() method of HelloWorld.java (see listing 2.16). Listing 2.16 Hello World main application code, calling EJBs package hello; import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; import javax.naming.InitialContext; public class HelloWorld { public static void main(String[] args) throws Exception { // Boot the JBoss Microcontainer with EJB3 settings, automatically // loads ejb3-interceptors-aop.xml and embedded-jboss-beans.xml EJB3StandaloneBootstrap.boot(null); // Deploy custom stateless beans (datasource, mostly) EJB3StandaloneBootstrap .deployXmlResource(”META-INF/helloworld-beans.xml”); // Deploy all EJBs found on classpath (slow, scans all) // EJB3StandaloneBootstrap.scanClasspath(); // Deploy all EJBs found on classpath (fast, scans build directory) // This is a relative location, matching the substring end of one // of java.class.path locations. Print out the value of // System.getProperty(”java.class.path”) to see all paths. EJB3StandaloneBootstrap.scanClasspath(”helloworld-ejb3/bin”); // Create InitialContext from jndi.properties
Check Tomcat Web Hosting services for best quality webspace to host your web application.