Download JMS and Message-Driven Beans - BFH-TI / Organisation

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Berner Fachhochschule
Technik und Informatik
JMS and
Message-Driven Beans
Course Multi Tier Business Applications with Java EE
Prof. Dr. Eric Dubuis
Berner Fachhochschule
Biel
Version Jun 1, 2012
Content
●
●
●
June 1, 2012
Java Messaging Service (JMS)
– publish/subscribe communication model
– point-to-point communication model
– JMS resources and programming model
Message-Driven Bean
Examples
– Ordinary client
– Message-driven bean
MTA with Java EE / JMS and MDBs
2
JMS Illustrated
●
Applications, Message Broker, and JMS:
JMS API
Message
Broker
JMS is a standard API that can be implemented by new service providers,
or existing message-based middleware can be adapted.
JMS is a standard provided by the Java Community Process [JMS]
June 1, 2012
MTA with Java EE / JMS and MDBs
3
JMS is Asynchronous
●
●
●
●
●
A producer sends a message to a virtual channel (destination)
A producer does not have to wait for a reply
A producer does not know who will receive the message
A producer is not dependent on the availability of consumers
The transaction and security context of a producer are not propagated to
the consumers of a message
Producer
Message
Broker
New security and transaction
Contexts start here
Security and transaction
Contexts end here
June 1, 2012
Consumer
MTA with Java EE / JMS and MDBs
4
Messaging Model: Publish-and-Subscribe
●
●
●
One producer can publish a message to many consumers through a
virtual channel called topic
Consumers can subscribe to a topic and receive automatically a copy of
each message (push model)
A durable connection allows consumers to disconnect and later
reconnect and collect messages that were published meanwhile
Topic
Message
Broker
June 1, 2012
MTA with Java EE / JMS and MDBs
5
Messaging Model: Point-to-Point
●
●
●
●
Clients can send and receive messages through a virtual channel called
queue
A queue may have multiple consumers but only one consumer may
receive each message
Consumers may request messages from the queue (pull model)
Consumers may register themselves at a queue; later they'll get a
notification
Queue
Message
Broker
June 1, 2012
MTA with Java EE / JMS and MDBs
6
JMS as a Resource
Java Messaging Service (JMS) is a standard API used to access enterprise
messaging systems (“message broker”:
●
●
●
A JMS client is a Java application that accesses JMS
A JMS provider is a messaging system that handles routing and delivery
of messages
A JMS application is a business system composed of many JMS clients
and one JMS provider
●
A JMS producer is a client that sends a message
●
A JMS consumer is a client that receives a message
June 1, 2012
MTA with Java EE / JMS and MDBs
7
JMS Programming Model
Configured
resource
Configured
resource
June 1, 2012
for
ordinary JMS
client...
Destination:
either a queue
or a topic...
MTA with Java EE / JMS and MDBs
Configured
resource
8
Sending or Receiving a JMS Message (ordinary clients)
In order to send or receive a message, a JMS client has to execute the
following steps:
●
●
●
●
●
●
obtain a connection factory that provides a JMS connection to the
messaging system
obtain a destination (a topic or a queue) which is the target for the
message to send or the source of a message to consume
create an connection to the JMS provider
create a session that is used to group the actions of sending and
receiving messages (single-threaded context)
create a message producer that sends messages to the specified
destination (topic or a queue)
create a message consumer and optionally register a message listener
that receives messages from the specified destination (topic or queue)
June 1, 2012
MTA with Java EE / JMS and MDBs
9
Message Types (I)
A JMS message is a Java object with three parts:
●
The header contains delivery information and metadata
●
The properties contain additional application or vendor specific metadata
●
The body carries the application data
June 1, 2012
MTA with Java EE / JMS and MDBs
10
Message Types (II)
●
TextMessage
●
ObjectMessage
●
BytesMessage
●
StreamMessage
●
MapMessage
You create messages
via factory methods
defined by the
Session object.
June 1, 2012
MTA with Java EE / JMS and MDBs
11
Message-Driven Beans
●
●
●
●
Message-driven beans are stateless, server-side, transaction-aware
components for processing asynchronous JMS messages
Message-driven beans are integration points for other applications
interested in working with EJB applications
Message-driven beans can consume and process messages
concurrently
Message-driven beans have no component interfaces
June 1, 2012
MTA with Java EE / JMS and MDBs
12
The Life Cycle of a Message-Driven Bean
June 1, 2012
MTA with Java EE / JMS and MDBs
13
Example: An Order Processor
●
●
●
The bean class
Receiving messages
Sending messages
JEE Appserver
EJB Container
Order
Order
Processor
Processor
Bean
Bean
Client
Client
June 1, 2012
EJBx
EJBx
EJBy
EJBy
Message
Message
Broker
Broker
MTA with Java EE / JMS and MDBs
14
Example: The OrderProcessor (cont'd)
Customer
Customer
<<find>>
Order
Order
Order
Order
Creator
Creator
Order
Order
ProcessorBean
ProcessorBean
<<create>>
<<create>>
OrderItem
OrderItem
<<find>>
Product
Product
Description
Description
June 1, 2012
MTA with Java EE / JMS and MDBs
15
Example: Sender JMS Client
Getting the connection factory:
Context jndiContext = new InitialContext();
ConnectionFactory factory =
(ConnectionFactory) jndiContext
.lookup("jndiName");
for example:
jms/QueueFactory
Alternatively, you can use the @Resource annotation in an application
client:
@Resource(mappedName="jms/ConnectionFactory")
private static ConnectionFactory factory;
June 1, 2012
MTA with Java EE / JMS and MDBs
16
Example: Queue Sender JMS Client (cont'd)
Getting a destination (a queue in our example) and a connection:
Queue queue = (Queue)
jndiContext.lookup("jndiName");
for example:
jms/OrderQueue
Alternatively, you can use the @Resource annotation in an application
client:
@Resouce(mappedName="jms/OrderQueue")
private static Queue queue;
June 1, 2012
MTA with Java EE / JMS and MDBs
17
Example: Queue Sender JMS Client (cont'd)
Session, sender, message, and sending a message:
// If the sending of the message must be
// transactional then set first parameter
// to true.
Connection con= factory.createConnection();
Session session = con.createSession(false,
Session.AUTO_ACKNOWLEDGE);
MessageProducer producer =
session.createProducer(queue);
Message message =
session.createObjectMessage(orderMessage);
producer.send(message);
// If the sending of the message is
// transactional then commit.
// session.commit();
June 1, 2012
MTA with Java EE / JMS and MDBs
18
Example: Queue Sender JMS Client (cont'd)
Parameters of createSession():
●
●
first parameter: indicates whether the sending to the queue occurs within
a transaction, i.e., the queue must behave like a transactional resource.
If true when queue expects voting with transactional manager. Use false
for non-transactional client.
second parameter: acknowledge mode; this parameter doesn't matter for
senders.
June 1, 2012
MTA with Java EE / JMS and MDBs
19
Example: Queue Sender JMS Client (cont'd)
Releasing resources:
producer.close();
session.close();
con.close();
June 1, 2012
MTA with Java EE / JMS and MDBs
20
Example: Message-Driven Bean
Import statements:
import
import
import
import
import
June 1, 2012
javax.ejb.MessageDriven;
javax.jms.JMSException;
javax.jms.Message;
javax.jms.MessageListener;
javax.jms.ObjectMessage;
MTA with Java EE / JMS and MDBs
21
The Interface of a Message-Driven Bean
A message-driven bean may implement the MessageListener interface
(however, it is recommended):
MessageListener
MessageListener
onMessage()
onMessage()
Bean
Beanclass
class
June 1, 2012
MTA with Java EE / JMS and MDBs
22
Example: Message-Driven Bean (cont'd)
Bean class with annotation:
@MessageDriven(
mappedName = "jms/OrderQueue",
activationConfig = {
@ActivationConfigProperty(
propertyName = "acknowledgeMode",
propertyValue = "Auto-acknowledge"),
@ActivationConfigProperty(
propertyName = "destinationType",
propertyValue = "javax.jms.Queue")
})
public class OrderProcessorBean implements
MessageListener {
// only if needed:
Implementing
@Resource
interface
private MessageDrivenContext mdc;
}
MessageListener
is optional, but
recommended
// ...
June 1, 2012
MTA with Java EE / JMS and MDBs
23
Example: Message-Driven Bean (cont'd)
Bean class with “empty” annotation:
Here, properties are
defined in the EJB
deployment descriptor;
you could define them
here, too, see previous
slide.
@MessageDriven()
public class OrderProcessorBean implements
MessageListener {
// only if needed:
@Resource
private MessageDrivenContext mdc;
}
// ...
June 1, 2012
MTA with Java EE / JMS and MDBs
24
Example: Message-Driven Bean (cont'd)
Configuration properties, specified via the javax.ejb.MessageDriven
annotation:
●
●
●
mappedName: A product specific name (e.g. global JNDI name of a
destination) that this message-driven bean should be mapped to, e.g:
“jms/OrderQueue”
name of queue we're
using in our exercise
Others, see API
activationConfig, specified via javax.ejb.ActivationConfigProperty[]
(propertyName / propertyValue pairs):
–
–
–
June 1, 2012
acknowledgeMode
values: Auto-acknowledge, Dups-ok-acknowledge
meaningful only with bean-managed transaction
messageSelector
value: application specific
subscriptionDurability
values: Durable, NonDurable
applicable only for MDBs reading from a topic
MTA with Java EE / JMS and MDBs
25
Example: Message-Driven Bean (cont'd)
Method onMessage():
@MessageDriven()
public class OrderProcessorBean implements
MessageListener {
public void onMessage(Message message) {
// ...
}
June 1, 2012
}
typically, you have to
downcast the message to
the expected message
type and then you process
the message...
MTA with Java EE / JMS and MDBs
26
References
[EJB3.1]
JSR 318: Enterprise JavaBeans, Version 3.1
EJB Core Contracts and Requirements
http://jcp.org/aboutJava/communityprocess/final/jsr318/index.html
[JPA]
JSR 317: Java Persistence API, Version 2.0
http://jcp.org/aboutJava/communityprocess/final/jsr317/index.html
[JMS]
JSR 914: Java Message Service (JMS) API
http://jcp.org/en/jsr/detail?id=914
June 1, 2012
MTA with Java EE / JMS and MDBs
27