10

Click here to load reader

Message Driven Beans (6)

Embed Size (px)

Citation preview

Page 1: Message Driven Beans (6)

Message-Driven Beans

By: Abdalla Mahmoud1

Contents

Message-Driven Beans........................................................................ 1Contents ........................................................................................... 11. Introduction................................................................................... 32. Synchronous vs. Asynchronous Messaging ......................................... 33. The Need for Messaging................................................................... 44. Java Messaging Service ................................................................... 4

4.1. JMS Elements ........................................................................... 54.1.1. Messaging Server ................................................................ 54.1.2. Messaging Client.................................................................. 5

4.1.2.1. Producer ....................................................................... 04.1.2.2. Consumer ..................................................................... 0

4.1.3. Destination ......................................................................... 55. Configuring a Destination................................................................. 6

5.1. Topic Destination ...................................................................... 65.2. Queue Destination..................................................................... 6

6. Message-Driven Beans (Consumer) ................................................... 76.1. Consumer for Topic Destination................................................... 76.2. Consumer for Queue Destination ................................................. 7

7. JMS Application Client (Producer)...................................................... 87.1. Producer for Topic Destination .................................................... 87.2. Producer for Queue Destination................................................... 9

1. http://www.abdallamahmoud.com

1

Page 2: Message Driven Beans (6)

2

Page 3: Message Driven Beans (6)

1. Introduction

The concept of messaging in computer science is very close to the real-world's concept.Suppose the following situations:

• You talk with your friend face to face.• You talk with your friend on cell phone.• You talk with your friend online.• You leave a written message for your friend with her mom.• You send an SMS to your friend.• You send a written mail to your friend.• You send an e-Mail to your friend.

In the first three situations, you are doing a conversation with your friend. A conversation isreally a way of sending messages to each others. There's a live contact between you and yourfriend. In the later situations, you are also messaging your friend. However, there is no livecontact between you and your friend. In software design we call, the first case synchronousmessaging, the later case asynchronous messaging.

2. Synchronous vs. Asynchronous Messaging

Synchronous messaging occurs when two parties are in touch, i.e. both are up. Receiverprocesses messages instantaneously then sends a feedback to the sender to continuefunctioning.

Asynchronous messaging occurs when two parties are not in touch, i.e. no party is requiredto be up in the same time. Receiver processes messages whenever it receives it, and doesnot send a feedback to the sender.

3

Page 4: Message Driven Beans (6)

3. The Need for Messaging

In software design, objects and components need to message each others, i.e. invoking eachothers' services. Synchronous messaging naturally follows of method invocation. Objects andcomponents invoke each others' services via message passing. The message of the senderincludes the method signature and arguments to that method. The feedback of the receiverincludes the return value of that method.

In some cases, notably in distributed systems, asynchronous messaging should also besupported. Examples of such cases where asynchronous messaging include:

• System or business errors reporting.• System performance reports.• Service activation queues.• Business events tracking.• Business dashboards.

4. Java Messaging Service

Java Messaging Service, or JMS, is an API for accessing enterprise asynchronous messagingsystems. A JMS-compliant messaging server typically implements the JMS API. Clients usethe JMS API to access the messaging service.

4

Page 5: Message Driven Beans (6)

4.1. JMS Elements

4.1.1. Messaging Server

The messaging server is responsible for directly receiving the message from the producerclient and routing it to the consumer client. The messaging server is provided by the Java EEapplication server.

4.1.2. Messaging Client

A messaging client is either a sender to a message or a receiver to a message.

4.1.2.1. ProducerA messaging client sending a message. The messaging producer can be any JavaEE enterprise component.

4.1.2.2. ConsumerA messaging client whose role is receiving messages. The messaging consumer inJava EE is a message-driven bean.

4.1.3. Destination

Messages are sent to logical destinations rather than physical destinations. The producerand consumer do not know about each others. The producer sends the message to a logicaldestination, where the consumer is registered to this logical destination. The messagingserver is responsible for routing messages sent to a specific destination to its registeredconsumers.

There are two types of destinations in JMS:

TopicA topic is used with one-to-many messaging models (in JMS called publish-subscribemodel, or pub/sub in short). The client is sending a message that's broadcasted tomany consumers.

QueueA queue is used with one-to-one messaging models (in JMS called point-to-pointmodel, p2p or PTP in short). The client is sending a message to only one consumer.

5

Page 6: Message Driven Beans (6)

5. Configuring a Destination

5.1. Topic Destination

• Create the following file in the JBoss deploy folder.• File name should ends with -service.xml• Text in bold is any given name to the topic destination.

file: deploy\myTopic-service.xml

<server><mbean code="org.jboss.mq.server.jmx.Topic"

name="jboss.mq.destination:service=Topic,name=myTopic"><attribute name="JNDIName">myTopic</attribute><depends optional-attribute-name="DestinationManager">

jboss.mq:service=DestinationManager</depends>

</mbean></server>

5.2. Queue Destination

• Create the following file in the JBoss deploy folder.• File name should ends with -service.xml• Text in bold is any given name to the queue destination.

file: deploy\myQueue-service.xml

<server><mbean code="org.jboss.mq.server.jmx.Queue"

name="jboss.mq.destination:service=Queue,name=myQueue"><attribute name="JNDIName">myQueue</attribute><depends optional-attribute-name="DestinationManager">

jboss.mq:service=DestinationManager</depends>

6

Page 7: Message Driven Beans (6)

</mbean></server>

6. Message-Driven Beans (Consumer)

A message-driven bean is an Enterprise JavaBean component that's responsible for receivingmessages from a specific destination.

6.1. Consumer for Topic Destination

file: hellomsg/MyTopicMessageBean.java

package hellomsg ;

import javax.jms.* ;import javax.ejb.* ;

@MessageDriven(activationConfig={@ActivationConfigProperty(

propertyName="destination",propertyValue="myTopic")

,@ActivationConfigProperty(

propertyName="destinationType",propertyValue="javax.jms.Topic")

})

public class MyTopicMessageBean implements MessageListener{

public void onMessage(Message message) {System.out.println("MESSAGE RECIEVED....!!!") ;

}

}

6.2. Consumer for Queue Destination

file: hellomsg/MyQueueMessageBean.java

package hellomsg ;

import javax.jms.* ;import javax.ejb.* ;

7

Page 8: Message Driven Beans (6)

@MessageDriven(activationConfig={@ActivationConfigProperty(

propertyName="destination",propertyValue="myQueue")

,@ActivationConfigProperty(

propertyName="destinationType",propertyValue="javax.jms.Queue")

})

public class MyQueueMessageBean implements MessageListener{

public void onMessage(Message message) {System.out.println("MESSAGE RECIEVED....!!!") ;

}

}

7. JMS Application Client (Producer)

7.1. Producer for Topic Destination

file: TopicClient.java

import javax.naming.* ;import javax.jms.* ;

public class TopicClient {

public static void main(String[] args) {

try {//1. get a reference to the JNDI environmentInitialContext ctx = new InitialContext() ;//2. get a reference to the JMS connection factoryConnectionFactory cf = (ConnectionFactory)

ctx.lookup("ConnectionFactory") ;//3. get a reference to the destination topicTopic myTopic = (Topic) ctx.lookup("myTopic") ;//4. Create a connection with the provided JMS serverConnection conn = cf.createConnection() ;//5. Create a thread of communicationSession session = conn.createSession(false,

Session.AUTO_ACKNOWLEDGE) ;//6. Create a message producer objectMessageProducer producer = session.createProducer(myTopic) ;//7. Create a text messageTextMessage msg = session.createTextMessage() ;

8

Page 9: Message Driven Beans (6)

msg.setText("Hello from the Topic Client") ;//8. Send the messageproducer.send(msg) ;//9. Close the Connectionconn.close() ;

}catch(Exception e) {

e.printStackTrace() ;}

}

}

7.2. Producer for Queue Destination

file: QueueClient.java

import javax.naming.* ;import javax.jms.* ;

public class QueueClient {

public static void main(String[] args) {

try {//1. get a reference to the JNDI environmentInitialContext ctx = new InitialContext() ;//2. get a reference to the JMS connection factoryConnectionFactory cf = (ConnectionFactory)

ctx.lookup("ConnectionFactory") ;//3. get a reference to the destination queueQueue myQueue = (Queue) ctx.lookup("myQueue") ;//4. Create a connection with the provided JMS serverConnection conn = cf.createConnection() ;//5. Create a thread of communicationSession session = conn.createSession(false,

Session.AUTO_ACKNOWLEDGE) ;//6. Create a message producer objectMessageProducer producer = session.createProducer(myQueue) ;//7. Create a text messageTextMessage msg = session.createTextMessage() ;msg.setText("Hello from the Queue Client") ;//8. Send the messageproducer.send(msg) ;//9. Close the Connectionconn.close() ;

}catch(Exception e) {

e.printStackTrace() ;}

9

Page 10: Message Driven Beans (6)

}

}

10