1 Module 10 Developing Java EE Applications using Messaging Java EE Apps. using Messaging 10 272 Ω Omega Ω Developing Applications for Java EE Platform ► Describe JMS API technology ► Write a message producer ► Write an asynchronous message consumer ► Write a synchronous message consumer ► List the capabilities and limitations of EJB components as messaging clients Objectives
16
Embed
Module 10 Developing Java EE Applications using Messaging
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
1
Module 10Developing Java EE Applications using Messaging
Java
EE
App
s. u
sing
Mes
sagi
ng10
272 Ω Omega ΩDeveloping Applications for Java EE Platform
Describe JMS API technology
Write a message producer
Write an asynchronous message consumer
Write a synchronous message consumer
List the capabilities and limitations of EJB components as messaging clients
Objectives
2
Java
EE
App
s. u
sing
Mes
sagi
ng10
273 Ω Omega ΩDeveloping Applications for Java EE Platform
Messaging System Participants
Java
EE
App
s. u
sing
Mes
sagi
ng10
274 Ω Omega ΩDeveloping Applications for Java EE Platform
Administered Objects
3
Java
EE
App
s. u
sing
Mes
sagi
ng10
275 Ω Omega ΩDeveloping Applications for Java EE Platform
Messaging Clients
The synchronous client blocks until a message arrives.
Java
EE
App
s. u
sing
Mes
sagi
ng10
276 Ω Omega ΩDeveloping Applications for Java EE Platform
Messages
4
Java
EE
App
s. u
sing
Mes
sagi
ng10
277 Ω Omega ΩDeveloping Applications for Java EE Platform
Point-to-Point Messaging Architecture
Java
EE
App
s. u
sing
Mes
sagi
ng10
278 Ω Omega ΩDeveloping Applications for Java EE Platform
Publish/Subscribe Messaging Architecture
5
Java
EE
App
s. u
sing
Mes
sagi
ng10
279 Ω Omega ΩDeveloping Applications for Java EE Platform
Creating a Queue Message Producer
Java
EE
App
s. u
sing
Mes
sagi
ng10
280 Ω Omega ΩDeveloping Applications for Java EE Platform
1. Obtain a connection factory using injection or the JNDIAPI.
2. Obtain the message queue using injection or the JNDI API.
3. Create a Connection object using the connection factory.
4. Create a Session object using the Connection object.
5. Create a QueueSender object using the Session object.
6. Create one or more Message objects using the Session object.
Creating a Queue Message Producer
6
Java
EE
App
s. u
sing
Mes
sagi
ng10
281 Ω Omega ΩDeveloping Applications for Java EE Platform
7. Populate message with text using the setText method of the TextMessage object.
8. Send one or more Message objects using the QueueSender object.
Creating a Queue Message Producer
Java
EE
App
s. u
sing
Mes
sagi
ng10
282 Ω Omega ΩDeveloping Applications for Java EE Platform
import javax.jms.*;import javax.naming.*;public class SimpleQSender
catch (NamingException e) System.out.println("JNDI API lookup failed: " +
e.getMessage());System.exit(1);
/** Create connection.* Create session from connection; false means session is* not transacted.* Create sender and text message.* Send messages, varying text slightly.* Send end-of-messages message.
Example Code for Queue Message Producer
8
Java
EE
App
s. u
sing
Mes
sagi
ng10
285 Ω Omega ΩDeveloping Applications for Java EE Platform
System.out.println("JNDI API lookup failed: " +e.getMessage());
System.exit(1);
/** Create connection.* Create session from connection; false means session is* not transacted.* Create receiver, then start message delivery.* Receive all text messages from queue until* a non-text message is received indicating end of* message stream.* Close connection.*/try
Example Code for Synchronous Queue Consumer
12
Java
EE
App
s. u
sing
Mes
sagi
ng10
293 Ω Omega ΩDeveloping Applications for Java EE Platform
299 Ω Omega ΩDeveloping Applications for Java EE Platform
catch (NamingException e) System.out.println("JNDI API lookup failed: " +
e.getMessage());System.exit(1);
/** Create connection.* Create session from connection; false means session is* not transacted.* Create receiver, then start message delivery.* Receive all text messages from queue until* a non-text message is received indicating end of* message stream.* Close connection.*/
Example Code for Asynchronous Queue ConsumerJa
va E
E A
pps.
usi
ng M
essa
ging
10
300 Ω Omega ΩDeveloping Applications for Java EE Platform