2. Using the Tutorial Examples 3. Getting Started with Web Applications 5. JavaServer Pages Technology 7. JavaServer Pages Standard Tag Library 10. JavaServer Faces Technology 11. Using JavaServer Faces Technology in JSP Pages 12. Developing with JavaServer Faces Technology 13. Creating Custom UI Components 14. Configuring JavaServer Faces Applications 15. Internationalizing and Localizing Web Applications 16. Building Web Services with JAX-WS 17. Binding between XML Schema and Java Classes 19. SOAP with Attachments API for Java 21. Getting Started with Enterprise Beans 23. A Message-Driven Bean Example 24. Introduction to the Java Persistence API 25. Persistence in the Web Tier 26. Persistence in the EJB Tier 27. The Java Persistence Query Language 28. Introduction to Security in the Java EE Platform 29. Securing Java EE Applications 31. The Java Message Service API 32. Java EE Examples Using the JMS API A Java EE Application That Uses the JMS API with a Session Bean Writing the Application Components for theclientsessionmdb Example Coding the Application Client:MyAppClient.java Coding the Publisher Session Bean Coding the Message-Driven Bean:MessageBean.java Creating Resources for theclientsessionmdb Example Building, Deploying, and Running theclientsessionmdb Example Using NetBeans IDE Building, Deploying, and Running theclientsessionmdb Example Using Ant A Java EE Application That Uses the JMS API with an Entity Overview of theclientmdbentity Example Application Writing the Application Components for theclientmdbentity Example Coding the Application Client:HumanResourceClient.java Coding the Message-Driven Beans for theclientmdbentity Example Coding the Entity Class for theclientmdbentity Example Creating Resources for theclientmdbentity Example Building, Deploying, and Running theclientmdbentity Example Using NetBeans IDE Building, Deploying, and Running theclientmdbentity Example Using Ant An Application Example That Consumes Messages from a Remote Server Overview of theconsumeremote Example Modules Writing the Module Components for theconsumeremote Example Creating Resources for theconsumeremote Example Using Two Application Servers for theconsumeremote Example Building, Deploying, and Running theconsumeremoteModules Using NetBeans IDE Building, Deploying, and Running theconsumeremote Modules Using Ant An Application Example That Deploys a Message-Driven Bean on Two Servers Overview of thesendremote Example Modules Writing the Module Components for thesendremote Example Coding the Application Client:MultiAppServerClient.java Coding the Message-Driven Bean:ReplyMsgBean.java Creating Resources for thesendremote Example Using Two Application Servers for thesendremote Example Building, Deploying, and Running thesendremote Modules Using NetBeans IDE Building, Deploying, and Running thesendremote Modules Using Ant 36. The Coffee Break Application | A Java EE Application That Uses the JMS API with an EntityThis section explains how to write, compile, package, deploy, and run a JavaEE application that uses the JMS API with an entity. The application usesthe following components:
This section covers the following topics:
You will find the source files for this section in the directorytut-install/javaeetutorial5/examples/jms/clientmdbentity/. Path names in this section are relative to this directory. Overview of theclientmdbentity Example ApplicationThis application simulates, in a simplified way, the work flow of a company’shuman resources (HR) department when it processes a new hire. This application alsodemonstrates how to use the Java EE platform to accomplish a task thatmany JMS client applications perform. A JMS client must often wait for several messages from various sources. Itthen uses the information in all these messages to assemble a messagethat it then sends to another destination. The common term for this processisjoining messages. Such a task must be transactional, with all the receives andthe send as a single transaction. If not all the messages are receivedsuccessfully, the transaction can be rolled back. For a client example that illustratesthis task, seeA Local Transaction Example. A message-driven bean can process only one message at a time ina transaction. To provide the ability to join messages, a Java EE applicationcan have the message-driven bean store the interim information in an entity. Theentity can then determine whether all the information has been received; when it has,the entity can report this back to one of the message-driven beans,which then creates and sends the message to the other destination. After ithas completed its task, the entity can be removed. The basic steps of the application are as follows.
Figure 32-2 illustrates the structure of this application. Of course, an actual HR applicationwould have more components; other beans could set up payroll and benefits records, scheduleorientation, and so on. Figure 32-2 assumes thatOfficeMDB is the first message-driven bean to consume themessage from the client.OfficeMDB then creates and persists theSetupOffice entity andstores the office information.EquipmentMDB then finds the entity, stores the equipmentinformation, and learns that the entity has completed its work.EquipmentMDB then sends themessage to the reply queue and removes the entity. Figure 32-2 A Java EE Application: Client to Message-Driven Beans to Entity ![]() Writing the Application Components for theclientmdbentity ExampleWriting the components of the application involves the following: Coding the Application Client:HumanResourceClient.javaThe application client program,clientmdbentity-app-client/src/java/HumanResourceClient.java, performs the following steps:
The message listener,HRListener, waits for messages that contain the assigned office andequipment for each employee. When a message arrives, the message listener displays theinformation received and determines whether all five messages have arrived. When they have,the message listener notifies the main program, which then exits. Coding the Message-Driven Beans for theclientmdbentity ExampleThis example uses two message-driven beans:
The beans take the following steps:
Coding the Entity Class for theclientmdbentity ExampleTheSetupOffice class,SetupOffice.java, is an entity class. The entity and the message-drivenbeans are packaged together in an EJB JAR file. The entity class isdeclared as follows: @Entitypublic class SetupOffice implements Serializable {The class contains a no-argument constructor and a constructor that takes two arguments,the employee ID and name. It also contains getter and setter methods forthe employee ID, name, office number, and equipment list. The getter method forthe employee ID has the@Id annotation to indicate that this field isthe primary key: @Id public String getEmployeeId() { return id;}The class also implements the two business methods,doEquipmentList anddoOfficeNumber, andtheir helper method,checkIfSetupComplete. The message-driven beans call the business methods and the getter methods. Thepersistence.xml file for the entity specifies the most basic settings: <persistence> <persistence-unit name="clientmdbentity"> <jta-data-source>jdbc/__default</jta-data-source> <class>eb.SetupOffice</class> <properties> <property name="toplink.ddl-generation" value="drop-and-create-tables"/> </properties> </persistence-unit></persistence> Creating Resources for theclientmdbentity ExampleThis example uses the connection factoryjms/ConnectionFactory and the topicjms/Topic, both ofwhich you used inA Java EE Application That Uses the JMS API with a Session Bean. It also uses the JDBC resource namedjdbc/__default, which is enabled by default when you start the Application Server. If you deleted the connection factory or topic, you can create them againusing targets in thebuild.xml file for this example. Use the following commandsto create the resources: ant create-cf ant create-topic Building, Deploying, and Running theclientmdbentity Example Using NetBeans IDETo build, deploy, and run the application using NetBeans IDE, do the following:
The output of the application client in the Output pane looks something likethis: PUBLISHER: Setting hire ID to 25, name Gertrude Bourbon, position Senior ProgrammerPUBLISHER: Setting hire ID to 26, name Jack Verdon, position ManagerPUBLISHER: Setting hire ID to 27, name Fred Tudor, position ManagerPUBLISHER: Setting hire ID to 28, name Fred Martin, position ProgrammerPUBLISHER: Setting hire ID to 29, name Mary Stuart, position ManagerWaiting for 5 message(s)New hire event processed: Employee ID: 25 Name: Gertrude Bourbon Equipment: Laptop Office number: 183Waiting for 4 message(s)New hire event processed: Employee ID: 26 Name: Jack Verdon Equipment: Pager Office number: 20Waiting for 3 message(s)New hire event processed: Employee ID: 27 Name: Fred Tudor Equipment: Pager Office number: 51Waiting for 2 message(s)New hire event processed: Employee ID: 28 Name: Fred Martin Equipment: Desktop System Office number: 141Waiting for 1 message(s)New hire event processed: Employee ID: 29 Name: Mary Stuart Equipment: Pager Office number: 238 The output from the message-driven beans and the entity class appears in theserver log, wrapped in logging information. For each employee, the application first creates the entity and then finds it.You may see runtime errors in the server log, and transaction rollbacks mayoccur. The errors occur if both of the message-driven beans discover at thesame time that the entity does not yet exist, so they bothtry to create it. The first attempt succeeds, but the second fails becausethe bean already exists. After the rollback, the second message-driven bean tries againand succeeds in finding the entity. Container-managed transactions allow the application to run correctly,in spite of these errors, with no special programming. You can run the application client repeatedly. Undeploy the application after you finish running the client. To undeploy the application,follow these steps:
To remove the generated files, right-click theclientmdbentity project and choose Clean. Building, Deploying, and Running theclientmdbentity Example Using AntTo create and package the application using Ant, perform these steps:
Theant command creates the following:
To deploy the application and run the client, use the following command: ant run Ignore the message that states that the application is deployed at a URL. The program output in the terminal window looks something like this: running application client container.PUBLISHER: Setting hire ID to 25, name Gertrude Bourbon, position Senior ProgrammerPUBLISHER: Setting hire ID to 26, name Jack Verdon, position ManagerPUBLISHER: Setting hire ID to 27, name Fred Tudor, position ManagerPUBLISHER: Setting hire ID to 28, name Fred Martin, position ProgrammerPUBLISHER: Setting hire ID to 29, name Mary Stuart, position ManagerWaiting for 5 message(s)New hire event processed: Employee ID: 25 Name: Gertrude Bourbon Equipment: Laptop Office number: 183Waiting for 4 message(s)New hire event processed: Employee ID: 26 Name: Jack Verdon Equipment: Pager Office number: 20Waiting for 3 message(s)New hire event processed: Employee ID: 27 Name: Fred Tudor Equipment: Pager Office number: 51Waiting for 2 message(s)New hire event processed: Employee ID: 28 Name: Fred Martin Equipment: Desktop System Office number: 141Waiting for 1 message(s)New hire event processed: Employee ID: 29 Name: Mary Stuart Equipment: Pager Office number: 238 The output from the message-driven beans and the entity class appears in theserver log, wrapped in logging information. For each employee, the application first creates the entity and then finds it.You may see runtime errors in the server log, and transaction rollbacks mayoccur. The errors occur if both of the message-driven beans discover at thesame time that the entity does not yet exist, so they bothtry to create it. The first attempt succeeds, but the second fails becausethe bean already exists. After the rollback, the second message-driven bean tries againand succeeds in finding the entity. Container-managed transactions allow the application to run correctly,in spite of these errors, with no special programming. Undeploy the application after you finish running the client: ant undeploy To remove the generated files, use the following command in theclientmdbentity,clientmdbentity-app-client, andclientmdbentity-ejb directories: ant clean Copyright © 2010, Oracle and/or its affiliates. All rights reserved.Legal Notices |