An Application That Uses the JMS API with a Session Bean (original) (raw)
2. Using the Tutorial Examples
3. Getting Started with Web Applications
4. JavaServer Faces Technology
7. Using JavaServer Faces Technology in Web Pages
8. Using Converters, Listeners, and Validators
9. Developing with JavaServer Faces Technology
10. JavaServer Faces Technology: Advanced Concepts
11. Using Ajax with JavaServer Faces Technology
12. Composite Components: Advanced Topics and Example
13. Creating Custom UI Components and Other Custom Objects
14. Configuring JavaServer Faces Applications
16. Uploading Files with Java Servlet Technology
17. Internationalizing and Localizing Web Applications
18. Introduction to Web Services
19. Building Web Services with JAX-WS
20. Building RESTful Web Services with JAX-RS
21. JAX-RS: Advanced Topics and Example
23. Getting Started with Enterprise Beans
24. Running the Enterprise Bean Examples
25. A Message-Driven Bean Example
26. Using the Embedded Enterprise Bean Container
27. Using Asynchronous Method Invocation in Session Beans
Part V Contexts and Dependency Injection for the Java EE Platform
28. Introduction to Contexts and Dependency Injection for the Java EE Platform
29. Running the Basic Contexts and Dependency Injection Examples
30. Contexts and Dependency Injection for the Java EE Platform: Advanced Topics
31. Running the Advanced Contexts and Dependency Injection Examples
32. Introduction to the Java Persistence API
33. Running the Persistence Examples
34. The Java Persistence Query Language
35. Using the Criteria API to Create Queries
36. Creating and Using String-Based Criteria Queries
37. Controlling Concurrent Access to Entity Data with Locking
38. Using a Second-Level Cache with Java Persistence API Applications
39. Introduction to Security in the Java EE Platform
40. Getting Started Securing Web Applications
41. Getting Started Securing Enterprise Applications
42. Java EE Security: Advanced Topics
Part VIII Java EE Supporting Technologies
43. Introduction to Java EE Supporting Technologies
45. Resources and Resource Adapters
46. The Resource Adapter Example
47. Java Message Service Concepts
48. Java Message Service Examples
Writing Simple JMS Applications
A Simple Example of Synchronous Message Receives
Writing the Clients for the Synchronous Receive Example
JMS Administered Objects for the Synchronous Receive Example
Running the Clients for the Synchronous Receive Example
A Simple Example of Asynchronous Message Consumption
Writing the Clients for the Asynchronous Receive Example
To Build and Package the AsynchConsumer Client Using NetBeans IDE
To Deploy and Run the Clients for the Asynchronous Receive Example Using NetBeans IDE
To Build and Package the AsynchConsumer Client Using Ant
A Simple Example of Browsing Messages in a Queue
Writing the Client for the QueueBrowser Example
To Run the MessageBrowser Client Using NetBeans IDE
To Run the MessageBrowser Client Using Ant and the appclient Command
Running JMS Clients on Multiple Systems
To Create Administered Objects for Multiple Systems
Changing the Default Host Name
To Run the Clients Using NetBeans IDE
To Run the Clients Using Ant and the appclient Command
Undeploying and Cleaning the Simple JMS Examples
Writing Robust JMS Applications
A Message Acknowledgment Example
To Run ackequivexample Using NetBeans IDE
To Run ackequivexample Using Ant
A Durable Subscription Example
To Run durablesubscriberexample Using NetBeans IDE
To Run durablesubscriberexample Using Ant
To Run transactedexample Using NetBeans IDE
To Run transactedexample Using Ant and the appclient Command
An Application That Uses the JMS API with an Entity
Overview of the clientmdbentity Example Application
Writing the Application Components for the clientmdbentity Example
Coding the Application Client: HumanResourceClient.java
Coding the Message-Driven Beans for the clientmdbentity Example
Coding the Entity Class for the clientmdbentity Example
Creating Resources for the clientmdbentity Example
Running the clientmdbentity Example
To Run the clientmdbentity Example Using NetBeans IDE
To Run the clientmdbentity Example Using Ant
An Application Example That Consumes Messages from a Remote Server
Overview of the consumeremote Example Modules
Writing the Module Components for the consumeremote Example
Creating Resources for the consumeremote Example
Using Two Application Servers for the consumeremote Example
Running the consumeremote Example
To Run the consumeremote Example Using NetBeans IDE
To Run the consumeremote Example Using Ant
An Application Example That Deploys a Message-Driven Bean on Two Servers
Overview of the sendremote Example Modules
Writing the Module Components for the sendremote Example
Coding the Application Client: MultiAppServerClient.java
Coding the Message-Driven Bean: ReplyMsgBean.java
Creating Resources for the sendremote Example
To Enable Deployment on the Remote System
To Use Two Application Servers for the sendremote Example
Running the sendremote Example
To Run the sendremote Example Using NetBeans IDE
To Run the sendremote Example Using Ant
To Disable Deployment on the Remote System
49. Bean Validation: Advanced Topics
50. Using Java EE Interceptors
51. Duke's Bookstore Case Study Example
52. Duke's Tutoring Case Study Example
53. Duke's Forest Case Study Example
This section explains how to write, compile, package, deploy, and run an application that uses the JMS API in conjunction with a session bean. The application contains the following components:
- An application client that invokes a session bean
- A session bean that publishes several messages to a topic
- A message-driven bean that receives and processes the messages using a durable topic subscriber and a message selector
You will find the source files for this section in the tut-install/examples/jms/clientsessionmdb/directory. Path names in this section are relative to this directory.
Writing the Application Components for the clientsessionmdb Example
This application demonstrates how to send messages from an enterprise bean (in this case, a session bean) rather than from an application client, as in the example in Chapter 25, A Message-Driven Bean Example. Figure 48-3 illustrates the structure of this application.
Figure 48-3 An Enterprise Bean Application: Client to Session Bean to Message-Driven Bean
The Publisher enterprise bean in this example is the enterprise-application equivalent of a wire-service news feed that categorizes news events into six news categories. The message-driven bean could represent a newsroom, where the sports desk, for example, would set up a subscription for all news events pertaining to sports.
The application client in the example injects the Publisher enterprise bean’s remote home interface and then calls the bean’s business method. The enterprise bean creates 18 text messages. For each message, it sets a String property randomly to one of six values representing the news categories and then publishes the message to a topic. The message-driven bean uses a message selector for the property to limit which of the published messages it receives.
Coding the Application Client: MyAppClient.java
The application client, clientsessionmdb-app-client/src/java/MyAppClient.java, performs no JMS API operations and so is simpler than the client in Chapter 25, A Message-Driven Bean Example. The client uses dependency injection to obtain the Publisher enterprise bean’s business interface:
@EJB(name="PublisherRemote") static private PublisherRemote publisher;
The client then calls the bean’s business method twice.
Coding the Publisher Session Bean
The Publisher bean is a stateless session bean that has one business method. The Publisher bean uses a remote interface rather than a local interface because it is accessed from the application client.
The remote interface, clientsessionmdb-ejb/src/java/sb/PublisherRemote.java, declares a single business method, publishNews.
The bean class, clientsessionmdb-ejb/src/java/sb/PublisherBean.java, implements the publishNews method and its helper method chooseType. The bean class also injects SessionContext, ConnectionFactory, and Topic resources and implements @PostConstructand @PreDestroy callback methods. The bean class begins as follows:
@Stateless @Remote({PublisherRemote.class}) public class PublisherBean implements PublisherRemote {
@Resource
private SessionContext sc;
@Resource(lookup = "jms/ConnectionFactory")
private ConnectionFactory connectionFactory;
@Resource(lookup = "jms/Topic")
private Topic topic;
...
The @PostConstruct callback method of the bean class, makeConnection, creates the Connection used by the bean. The business method publishNews creates a Session and a MessageProducerand publishes the messages.
The @PreDestroy callback method, endConnection, deallocates the resources that were allocated by the@PostConstruct callback method. In this case, the method closes the Connection.
Coding the Message-Driven Bean: MessageBean.java
The message-driven bean class, clientsessionmdb-ejb/src/java/mdb/MessageBean.java, is almost identical to the one inChapter 25, A Message-Driven Bean Example. However, the @MessageDriven annotation is different, because instead of a queue, the bean is using a topic with a durable subscription, and it is also using a message selector. Therefore, the annotation sets the activation config properties messageSelector,subscriptionDurability, clientId, and subscriptionName, as follows:
@MessageDriven(mappedName = "jms/Topic", activationConfig = { @ActivationConfigProperty(propertyName = "messageSelector", propertyValue = "NewsType = 'Sports' OR NewsType = 'Opinion'") , @ActivationConfigProperty(propertyName = "subscriptionDurability", propertyValue = "Durable") , @ActivationConfigProperty(propertyName = "clientId", propertyValue = "MyID") , @ActivationConfigProperty(propertyName = "subscriptionName", propertyValue = "MySub") })
Note - For a message-driven bean, the destination is specified with the mappedName element instead of the lookup element.
The JMS resource adapter uses these properties to create a connection factory for the message-driven bean that allows the bean to use a durable subscriber.
Creating Resources for the clientsessionmdb Example
This example uses the topic named jms/Topic and the connection factory jms/ConnectionFactory, which are also used in previous examples. If you deleted the connection factory or topic, they will be recreated when you deploy the example.
Running the clientsessionmdb Example
You can use either NetBeans IDE or Ant to build, package, deploy, and run the clientsessionmdb example.
To Run the clientsessionmdb Example Using NetBeans IDE
- To compile and package the project, follow these steps:
- From the File menu, choose Open Project.
- In the Open Project dialog, navigate to:
tut-install/examples/jms/ - Select the clientsessionmdb folder.
- Select the Open as Main Project check box and the Open Required Projects check box.
- Click Open Project.
- In the Projects tab, right-click the clientsessionmdb project and select Build.
This task creates the following:- An application client JAR file that contains the client class file and the session bean’s remote interface, along with a manifest file that specifies the main class and places the EJB JAR file in its classpath
- An EJB JAR file that contains both the session bean and the message-driven bean
- An application EAR file that contains the two JAR files
- Right-click the project and select Run.
This command creates any needed resources, deploys the project, returns a JAR file named clientsessionmdbClient.jar, and then executes it.
The output of the application client in the Output pane looks like this (preceded by application client container output):
To view the bean output,
check /domains/domain1/logs/server.log.
The output from the enterprise beans appears in the server log (domain-dir/logs/server.log), wrapped in logging information. The Publisher session bean sends two sets of 18 messages numbered 0 through 17. Because of the message selector, the message-driven bean receives only the messages whose NewsType property is Sports or Opinion.
To Run the clientsessionmdb Example Using Ant
- Go to the following directory:
tut-install/examples/jms/clientsessionmdb/ - To compile the source files and package the application, use the following command:
ant
The ant command creates the following:- An application client JAR file that contains the client class file and the session bean’s remote interface, along with a manifest file that specifies the main class and places the EJB JAR file in its classpath
- An EJB JAR file that contains both the session bean and the message-driven bean
- An application EAR file that contains the two JAR files
The clientsessionmdb.ear file is created in the dist directory.
- To create any needed resources, 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 client displays these lines (preceded by application client container output):
To view the bean output,
check /domains/domain1/logs/server.log.
The output from the enterprise beans appears in the server log file, wrapped in logging information. The Publisher session bean sends two sets of 18 messages numbered 0 through 17. Because of the message selector, the message-driven bean receives only the messages whose NewsType property is Sports or Opinion.
Copyright © 2013, Oracle and/or its affiliates. All rights reserved. Legal Notices