Docsity

EJB Message-Driven Beans: Asynchronous Messaging with Java Message Service (JMS) - Prof. Y, Study notes of Engineering

Toggle sidebar
bg1
1
EJB Message-Driven Beans
Ye Wu
http://www.ise.gmu.edu/~wuye
SWE 645
Component-based Software Development
2006-9-20 © Dr. Ye Wu 2
Outline
• Introduction to Messaging
• Java Message Service (JMS)
• EJB Message-Driven Beans
2006-9-20 © Dr. Ye Wu 3
Messaging Service
• Provides communication between applications
through “messages” (one way communication)
• Message clients – any application or component
that uses a messaging service
• Decouple message clients – senders and receivers
do not need to know each other
2006-9-20 © Dr. Ye Wu 4
Why messaging service
• B2C communication
– What if server is down due to maintenance or failure?
• B2B communication
– Data have to be shared across the internet, what if some
of them are not available?
• Saving server resource
– Do you want you server running all the time to wait for
some orders?
2006-9-20 © Dr. Ye Wu 5
Communication Models
- Messaging Domain
• Point-to-point model
If there is one, and only one, message consumer for
each message
Sender
Sender
Sender
Message
Queue
Message
Queue
Receiver
Receiver
2006-9-20 © Dr. Ye Wu 6
Communication Models
• Publish/Subscribe model
Message will be delivered to more than one client
Publisher Topic
Topic
Subscriber
Publisher
Publisher Subscriber
pf3
pf4

Partial preview of the text

Download EJB Message-Driven Beans: Asynchronous Messaging with Java Message Service (JMS) - Prof. Y and more Study notes Engineering in PDF only on Docsity!

EJB Message-Driven Beans

Ye Wu

http://www.ise.gmu.edu/~wuye

SWE 645

Component-based Software Development

2006-9-20 © Dr. Ye Wu 2

Outline

  • Introduction to Messaging
  • Java Message Service (JMS)
  • EJB Message-Driven Beans

2006-9-20 © Dr. Ye Wu 3

Messaging Service

  • Provides communication between applications

through “messages” (one way communication)

  • Message clients – any application or component

that uses a messaging service

  • Decouple message clients – senders and receivers

do not need to know each other

2006-9-20 © Dr. Ye Wu 4

Why messaging service

  • B2C communication
    • What if server is down due to maintenance or failure?
  • B2B communication
    • Data have to be shared across the internet, what if some

of them are not available?

  • Saving server resource
    • Do you want you server running all the time to wait for

some orders?

2006-9-20 © Dr. Ye Wu 5

Communication Models

- Messaging Domain

  • Point-to-point model

If there is one, and only one, message consumer for

each message

Sender

Sender

Sender

Message Queue

Message Queue

Receiver

Receiver

2006-9-20 © Dr. Ye Wu 6

Communication Models

  • Publish/Subscribe model

Message will be delivered to more than one client

Publisher Topic

Topic

Subscriber

Publisher

Publisher

Subscriber

2006-9-20 © Dr. Ye Wu 7

The JMS Object Model

  • ConnectionFactory
    • Is used to create a connection with a JMS provider.
  • Destination
    • Encapsulate a provider-specific address.
  • Connection
    • Represents a JMS client’s active connection to a JMS provider
  • Session
    • A single-threaded context for producing and consuming messages.
    • Topic session and Queue session

2006-9-20 © Dr. Ye Wu 8

The JMS Object Model

  • MessageProducer
    • Used by a JMS client to send messages to a destination.
  • MessageConsumer
    • Used by a JMS client t receive message from a

destination

  • Message
    • Header, properties,body
    • TextMessage, MapMessage,ByteMessage,

StreamMesssage, ObjectMessage

2006-9-20 © Dr. Ye Wu 9

Writing JMS Clients - Sender

  • Obtain ConnectionFatory QueueConnectionFactory qConnectionFactory = (QueueConnectionFactory) ctx.lookup(“jms/QueueConnectionFactory"); /* use java:/XAConnectionFactory in JBoss 404*/
  • Create Connection qConnection = qConnectionFactory.createQueueConnection();
  • Create Session QueueSession qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);

2006-9-20 © Dr. Ye Wu 10

Writing P-2-P JMS Clients - Sender

  • Create QueeSender Object String queueName = “jms/Queue"; /* use queue/testQueue in Jboss*/ Queue queue = (Queue)ctx.lookup(queueName); QueueSender qSender = qSession.createSender(queue);
  • Prepare and Send message TextMessage message = qSession.createTextMessage(); message.setText("HelloWorld"); qSender.send(message);

2006-9-20 © Dr. Ye Wu 11

Writing JMS Clients - Receiver

  • Obtain ConnectionFatory QueueConnectionFactory qConnectionFactory = (QueueConnectionFactory) ctx.lookup(“jms/QueueConnectionFactory");
  • Create Connection qConnection = qConnectionFactory.createQueueConnection();
  • Create Session QueueSession qSession = qConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 2006-9-20 © Dr. Ye Wu 12

Writing P-2-P JMS Clients - Receiver

  • Create QueryReceiver Object String queueName = “jms/Queue"; Queue queue = (Queue)ctx.lookup(queueName); QueueReceiver qReceiver = qSession.createReceiver(queue);
  • Prepare and Receive message qConnection.start(); Message message = qReceiver.receive(1);

2006-9-20 © Dr. Ye Wu 19

Message-Driven Bean Class

public void onMessage(Message msg) { try { TextMessage message = (TextMessage)msg; System.out.println(message.getText()); }catch(Exception e) { System.out.println(e); } } }

2006-9-20 © Dr. Ye Wu 20

Additional MDB Configurations

@MessageDriven(activationConfig = { @ActivationConfigProperty(propertyName="destinationType", propertyValue="javax.jms.Queue"), @ActivationConfigProperty(propertyName=“messageSelector", propertyValue=“’MessageFormat = ‘Version 3.4’") @ActivationConfigProperty(propertyName=“acknowledgeMode", propertyValue=“Auto-acknowledge") @ActivationConfigProperty(propertyName=“subscriptionDurability", propertyValue=“Durable") })

2006-9-20 © Dr. Ye Wu 21

Message-Driven Bean LifeCycle

Method Ready onMessage()

Not Exist

  1. Class.newInstance()
  2. injection
  3. PostConstruct @Predestroy

2006-9-20 © Dr. Ye Wu 22

Connector-Based Message-Driven Bean

  • EJB venders are able to support only a small number of JMS providers.
  • Tied up with JMS programming model.
  • A lot of other messaging system available. For instance: email, SOAP.