SlideShare a Scribd company logo
Message-Driven Beans
                      1
By: Abdalla Mahmoud


Contents
            Message-Driven Beans ........................................................................ 1
             Contents ........................................................................................... 1
             1. Introduction ................................................................................... 3
             2. Synchronous vs. Asynchronous Messaging ......................................... 3
             3. The Need for Messaging................................................................... 4
             4. Java Messaging Service ................................................................... 4
               4.1. JMS Elements ........................................................................... 5
                 4.1.1. Messaging Server ................................................................ 5
                 4.1.2. Messaging Client.................................................................. 5
                   4.1.2.1. Producer ....................................................................... 0
                   4.1.2.2. Consumer ..................................................................... 0
                 4.1.3. Destination ......................................................................... 5
             5. Configuring a Destination................................................................. 6
               5.1. Topic Destination ...................................................................... 6
               5.2. Queue Destination ..................................................................... 6
             6. Message-Driven Beans (Consumer) ................................................... 7
               6.1. Consumer for Topic Destination................................................... 7
               6.2. Consumer for Queue Destination ................................................. 7
             7. JMS Application Client (Producer) ...................................................... 8
               7.1. Producer for Topic Destination .................................................... 8
               7.2. Producer for Queue Destination ................................................... 9




1. http://www.abdallamahmoud.com



                                                     1
2
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 is
really a way of sending messages to each others. There's a live contact between you and your
friend. In the later situations, you are also messaging your friend. However, there is no live
contact between you and your friend. In software design we call, the first case synchronous
messaging, the later case asynchronous messaging.


2. Synchronous vs. Asynchronous Messaging
   Synchronous messaging occurs when two parties are in touch, i.e. both are up. Receiver
processes messages instantaneously then sends a feedback to the sender to continue
functioning.

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




                                               3
3. The Need for Messaging
In software design, objects and components need to message each others, i.e. invoking each
others' services. Synchronous messaging naturally follows of method invocation. Objects and
components invoke each others' services via message passing. The message of the sender
includes the method signature and arguments to that method. The feedback of the receiver
includes the return value of that method.

In some cases, notably in distributed systems, asynchronous messaging should also be
supported. 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 messaging
systems. A JMS-compliant messaging server typically implements the JMS API. Clients use
the JMS API to access the messaging service.




                                                4
4.1. JMS Elements

4.1.1. Messaging Server

The messaging server is responsible for directly receiving the message from the producer
client and routing it to the consumer client. The messaging server is provided by the Java EE
application 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. Producer
       A messaging client sending a message. The messaging producer can be any Java
       EE enterprise component.

       4.1.2.2. Consumer
       A messaging client whose role is receiving messages. The messaging consumer in
       Java EE is a message-driven bean.


4.1.3. Destination

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

There are two types of destinations in JMS:
       Topic
       A topic is used with one-to-many messaging models (in JMS called publish-subscribe
       model, or pub/sub in short). The client is sending a message that's broadcasted to
       many consumers.




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




                                              5
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: deploymyTopic-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: deploymyQueue-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
</mbean>
</server>


6. Message-Driven Beans (Consumer)
A message-driven bean is an Enterprise JavaBean component that's responsible for receiving
messages 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
@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 environment
         InitialContext ctx = new InitialContext() ;
         //2. get a reference to the JMS connection factory
         ConnectionFactory cf = (ConnectionFactory)
ctx.lookup("ConnectionFactory") ;
         //3. get a reference to the destination topic
         Topic myTopic = (Topic) ctx.lookup("myTopic") ;
         //4. Create a connection with the provided JMS server
         Connection conn = cf.createConnection() ;
         //5. Create a thread of communication
         Session session = conn.createSession(false,
Session.AUTO_ACKNOWLEDGE) ;
         //6. Create a message producer object
         MessageProducer producer = session.createProducer(myTopic) ;
         //7. Create a text message
         TextMessage msg = session.createTextMessage() ;




                                      8
msg.setText("Hello from the Topic Client") ;
           //8. Send the message
           producer.send(msg) ;
           //9. Close the Connection
           conn.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 environment
         InitialContext ctx = new InitialContext() ;
         //2. get a reference to the JMS connection factory
         ConnectionFactory cf = (ConnectionFactory)
ctx.lookup("ConnectionFactory") ;
         //3. get a reference to the destination queue
         Queue myQueue = (Queue) ctx.lookup("myQueue") ;
         //4. Create a connection with the provided JMS server
         Connection conn = cf.createConnection() ;
         //5. Create a thread of communication
         Session session = conn.createSession(false,
Session.AUTO_ACKNOWLEDGE) ;
         //6. Create a message producer object
         MessageProducer producer = session.createProducer(myQueue) ;
         //7. Create a text message
         TextMessage msg = session.createTextMessage() ;
         msg.setText("Hello from the Queue Client") ;
         //8. Send the message
         producer.send(msg) ;
         //9. Close the Connection
         conn.close() ;
      }
      catch(Exception e) {
         e.printStackTrace() ;
      }




                                       9
}

}




        10

More Related Content

What's hot

JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging Service
Peter R. Egli
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
Matt Stine
 
JMS-Java Message Service
JMS-Java Message ServiceJMS-Java Message Service
JMS-Java Message Service
Kasun Madusanke
 
ActiveMQ Configuration
ActiveMQ ConfigurationActiveMQ Configuration
ActiveMQ Configuration
Ashish Mishra
 
test
testtest
test
techweb08
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Services
kumar gaurav
 
Java Message Service
Java Message ServiceJava Message Service
Java Message Service
AMIT YADAV
 
Jms using j boss
Jms using j bossJms using j boss
Jms using j boss
Skillwise Group
 
Jms слайды
Jms слайдыJms слайды
Jms слайды
Sergey D
 
Understanding JMS Integration Patterns
Understanding JMS Integration Patterns Understanding JMS Integration Patterns
Understanding JMS Integration Patterns
WSO2
 
Enterprise messaging with jms
Enterprise messaging with jmsEnterprise messaging with jms
Enterprise messaging with jms
Sridhar Reddy
 

What's hot (12)

JMS - Java Messaging Service
JMS - Java Messaging ServiceJMS - Java Messaging Service
JMS - Java Messaging Service
 
Introduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOsIntroduction to JMS and Message-Driven POJOs
Introduction to JMS and Message-Driven POJOs
 
JMS-Java Message Service
JMS-Java Message ServiceJMS-Java Message Service
JMS-Java Message Service
 
ActiveMQ Configuration
ActiveMQ ConfigurationActiveMQ Configuration
ActiveMQ Configuration
 
test
testtest
test
 
test
testtest
test
 
Java Messaging Services
Java Messaging ServicesJava Messaging Services
Java Messaging Services
 
Java Message Service
Java Message ServiceJava Message Service
Java Message Service
 
Jms using j boss
Jms using j bossJms using j boss
Jms using j boss
 
Jms слайды
Jms слайдыJms слайды
Jms слайды
 
Understanding JMS Integration Patterns
Understanding JMS Integration Patterns Understanding JMS Integration Patterns
Understanding JMS Integration Patterns
 
Enterprise messaging with jms
Enterprise messaging with jmsEnterprise messaging with jms
Enterprise messaging with jms
 

Viewers also liked

Summer training java
Summer training javaSummer training java
Summer training java
Arshit Rai
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMS
Bruce Snyder
 
Java J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus ChecklistJava J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus Checklist
Sunil Kumar Gunasekaran
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
Federico Paparoni
 
JAVA Training Syllabus Course
JAVA Training Syllabus CourseJAVA Training Syllabus Course
JAVA Training Syllabus Course
TOPS Technologies
 
Advanced java programming-contents
Advanced java programming-contentsAdvanced java programming-contents
Advanced java programming-contents
Self-Employed
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
slire
 
Core java slides
Core java slidesCore java slides
Core java slides
Abhilash Nair
 

Viewers also liked (8)

Summer training java
Summer training javaSummer training java
Summer training java
 
Enterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMSEnterprise Messaging With ActiveMQ and Spring JMS
Enterprise Messaging With ActiveMQ and Spring JMS
 
Java J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus ChecklistJava J2EE Complete Syllabus Checklist
Java J2EE Complete Syllabus Checklist
 
Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)Tutorial su JMS (Java Message Service)
Tutorial su JMS (Java Message Service)
 
JAVA Training Syllabus Course
JAVA Training Syllabus CourseJAVA Training Syllabus Course
JAVA Training Syllabus Course
 
Advanced java programming-contents
Advanced java programming-contentsAdvanced java programming-contents
Advanced java programming-contents
 
Advance Java Topics (J2EE)
Advance Java Topics (J2EE)Advance Java Topics (J2EE)
Advance Java Topics (J2EE)
 
Core java slides
Core java slidesCore java slides
Core java slides
 

Similar to Message Driven Beans (6)

Jms
JmsJms
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
test
testtest
test
techweb08
 
Test DB user
Test DB userTest DB user
Test DB user
techweb08
 
test validation
test validationtest validation
test validation
techweb08
 
Servlets
ServletsServlets
Servlets
Abdalla Mahmoud
 
Jms queues
Jms queuesJms queues
Jms queues
Karnam Karthik
 
Persistence
PersistencePersistence
Persistence
Abdalla Mahmoud
 
M messaging 2
M messaging 2M messaging 2
M messaging 2
Vasanthii Chowdary
 
M messaging 1
M messaging 1M messaging 1
M messaging 1
Vasanthii Chowdary
 
Mule jms queues
Mule jms queuesMule jms queues
Mule jms queues
Gandham38
 
Jms topics
Jms topicsJms topics
Jms topics
Ravinder Singh
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
Lilia Sfaxi
 
Weblogic - Introduction to configure JMS
Weblogic  - Introduction to configure JMSWeblogic  - Introduction to configure JMS
Weblogic - Introduction to configure JMS
Vibrant Technologies & Computers
 
#8 (Java Message Service)
#8 (Java Message Service)#8 (Java Message Service)
#8 (Java Message Service)
Ghadeer AlHasan
 

Similar to Message Driven Beans (6) (20)

Jms
JmsJms
Jms
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
test
testtest
test
 
Test DB user
Test DB userTest DB user
Test DB user
 
test validation
test validationtest validation
test validation
 
Servlets
ServletsServlets
Servlets
 
Jms queues
Jms queuesJms queues
Jms queues
 
Persistence
PersistencePersistence
Persistence
 
M messaging 2
M messaging 2M messaging 2
M messaging 2
 
M messaging 1
M messaging 1M messaging 1
M messaging 1
 
Mule jms queues
Mule jms queuesMule jms queues
Mule jms queues
 
Jms topics
Jms topicsJms topics
Jms topics
 
Server-side JS with NodeJS
Server-side JS with NodeJSServer-side JS with NodeJS
Server-side JS with NodeJS
 
Weblogic - Introduction to configure JMS
Weblogic  - Introduction to configure JMSWeblogic  - Introduction to configure JMS
Weblogic - Introduction to configure JMS
 
#8 (Java Message Service)
#8 (Java Message Service)#8 (Java Message Service)
#8 (Java Message Service)
 

More from Abdalla Mahmoud

JavaServer Pages
JavaServer PagesJavaServer Pages
JavaServer Pages
Abdalla Mahmoud
 
Java EE Services
Java EE ServicesJava EE Services
Java EE Services
Abdalla Mahmoud
 
Introduction to the World Wide Web
Introduction to the World Wide WebIntroduction to the World Wide Web
Introduction to the World Wide Web
Abdalla Mahmoud
 
Introduction to Java Enterprise Edition
Introduction to Java Enterprise EditionIntroduction to Java Enterprise Edition
Introduction to Java Enterprise Edition
Abdalla Mahmoud
 
Object-Oriented Concepts
Object-Oriented ConceptsObject-Oriented Concepts
Object-Oriented Concepts
Abdalla Mahmoud
 
One-Hour Java Talk
One-Hour Java TalkOne-Hour Java Talk
One-Hour Java Talk
Abdalla Mahmoud
 
Being Professional
Being ProfessionalBeing Professional
Being Professional
Abdalla Mahmoud
 

More from Abdalla Mahmoud (7)

JavaServer Pages
JavaServer PagesJavaServer Pages
JavaServer Pages
 
Java EE Services
Java EE ServicesJava EE Services
Java EE Services
 
Introduction to the World Wide Web
Introduction to the World Wide WebIntroduction to the World Wide Web
Introduction to the World Wide Web
 
Introduction to Java Enterprise Edition
Introduction to Java Enterprise EditionIntroduction to Java Enterprise Edition
Introduction to Java Enterprise Edition
 
Object-Oriented Concepts
Object-Oriented ConceptsObject-Oriented Concepts
Object-Oriented Concepts
 
One-Hour Java Talk
One-Hour Java TalkOne-Hour Java Talk
One-Hour Java Talk
 
Being Professional
Being ProfessionalBeing Professional
Being Professional
 

Recently uploaded

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
Chart Kalyan
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
alexjohnson7307
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
Ivanti
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Wask
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Tatiana Kojar
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
fredae14
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
Pravash Chandra Das
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
Brandon Minnick, MBA
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
saastr
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
Hiike
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
Zilliz
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
GDSC PJATK
 

Recently uploaded (20)

How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdfHow to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
How to Interpret Trends in the Kalyan Rajdhani Mix Chart.pdf
 
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
leewayhertz.com-AI in predictive maintenance Use cases technologies benefits ...
 
June Patch Tuesday
June Patch TuesdayJune Patch Tuesday
June Patch Tuesday
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Digital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying AheadDigital Marketing Trends in 2024 | Guide for Staying Ahead
Digital Marketing Trends in 2024 | Guide for Staying Ahead
 
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
Skybuffer AI: Advanced Conversational and Generative AI Solution on SAP Busin...
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Recommendation System using RAG Architecture
Recommendation System using RAG ArchitectureRecommendation System using RAG Architecture
Recommendation System using RAG Architecture
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Operating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptxOperating System Used by Users in day-to-day life.pptx
Operating System Used by Users in day-to-day life.pptx
 
Choosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptxChoosing The Best AWS Service For Your Website + API.pptx
Choosing The Best AWS Service For Your Website + API.pptx
 
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStrDeep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
Deep Dive: Getting Funded with Jason Jason Lemkin Founder & CEO @ SaaStr
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - HiikeSystem Design Case Study: Building a Scalable E-Commerce Platform - Hiike
System Design Case Study: Building a Scalable E-Commerce Platform - Hiike
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
Fueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte WebinarFueling AI with Great Data with Airbyte Webinar
Fueling AI with Great Data with Airbyte Webinar
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!Finale of the Year: Apply for Next One!
Finale of the Year: Apply for Next One!
 

Message Driven Beans (6)

  • 1. Message-Driven Beans 1 By: Abdalla Mahmoud Contents Message-Driven Beans ........................................................................ 1 Contents ........................................................................................... 1 1. Introduction ................................................................................... 3 2. Synchronous vs. Asynchronous Messaging ......................................... 3 3. The Need for Messaging................................................................... 4 4. Java Messaging Service ................................................................... 4 4.1. JMS Elements ........................................................................... 5 4.1.1. Messaging Server ................................................................ 5 4.1.2. Messaging Client.................................................................. 5 4.1.2.1. Producer ....................................................................... 0 4.1.2.2. Consumer ..................................................................... 0 4.1.3. Destination ......................................................................... 5 5. Configuring a Destination................................................................. 6 5.1. Topic Destination ...................................................................... 6 5.2. Queue Destination ..................................................................... 6 6. Message-Driven Beans (Consumer) ................................................... 7 6.1. Consumer for Topic Destination................................................... 7 6.2. Consumer for Queue Destination ................................................. 7 7. JMS Application Client (Producer) ...................................................... 8 7.1. Producer for Topic Destination .................................................... 8 7.2. Producer for Queue Destination ................................................... 9 1. http://www.abdallamahmoud.com 1
  • 2. 2
  • 3. 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 is really a way of sending messages to each others. There's a live contact between you and your friend. In the later situations, you are also messaging your friend. However, there is no live contact between you and your friend. In software design we call, the first case synchronous messaging, the later case asynchronous messaging. 2. Synchronous vs. Asynchronous Messaging Synchronous messaging occurs when two parties are in touch, i.e. both are up. Receiver processes messages instantaneously then sends a feedback to the sender to continue functioning. Asynchronous messaging occurs when two parties are not in touch, i.e. no party is required to be up in the same time. Receiver processes messages whenever it receives it, and does not send a feedback to the sender. 3
  • 4. 3. The Need for Messaging In software design, objects and components need to message each others, i.e. invoking each others' services. Synchronous messaging naturally follows of method invocation. Objects and components invoke each others' services via message passing. The message of the sender includes the method signature and arguments to that method. The feedback of the receiver includes the return value of that method. In some cases, notably in distributed systems, asynchronous messaging should also be supported. 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 messaging systems. A JMS-compliant messaging server typically implements the JMS API. Clients use the JMS API to access the messaging service. 4
  • 5. 4.1. JMS Elements 4.1.1. Messaging Server The messaging server is responsible for directly receiving the message from the producer client and routing it to the consumer client. The messaging server is provided by the Java EE application 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. Producer A messaging client sending a message. The messaging producer can be any Java EE enterprise component. 4.1.2.2. Consumer A messaging client whose role is receiving messages. The messaging consumer in Java EE is a message-driven bean. 4.1.3. Destination Messages are sent to logical destinations rather than physical destinations. The producer and consumer do not know about each others. The producer sends the message to a logical destination, where the consumer is registered to this logical destination. The messaging server is responsible for routing messages sent to a specific destination to its registered consumers. There are two types of destinations in JMS: Topic A topic is used with one-to-many messaging models (in JMS called publish-subscribe model, or pub/sub in short). The client is sending a message that's broadcasted to many consumers. Queue A queue is used with one-to-one messaging models (in JMS called point-to-point model, p2p or PTP in short). The client is sending a message to only one consumer. 5
  • 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: deploymyTopic-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: deploymyQueue-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
  • 7. </mbean> </server> 6. Message-Driven Beans (Consumer) A message-driven bean is an Enterprise JavaBean component that's responsible for receiving messages 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
  • 8. @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 environment InitialContext ctx = new InitialContext() ; //2. get a reference to the JMS connection factory ConnectionFactory cf = (ConnectionFactory) ctx.lookup("ConnectionFactory") ; //3. get a reference to the destination topic Topic myTopic = (Topic) ctx.lookup("myTopic") ; //4. Create a connection with the provided JMS server Connection conn = cf.createConnection() ; //5. Create a thread of communication Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE) ; //6. Create a message producer object MessageProducer producer = session.createProducer(myTopic) ; //7. Create a text message TextMessage msg = session.createTextMessage() ; 8
  • 9. msg.setText("Hello from the Topic Client") ; //8. Send the message producer.send(msg) ; //9. Close the Connection conn.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 environment InitialContext ctx = new InitialContext() ; //2. get a reference to the JMS connection factory ConnectionFactory cf = (ConnectionFactory) ctx.lookup("ConnectionFactory") ; //3. get a reference to the destination queue Queue myQueue = (Queue) ctx.lookup("myQueue") ; //4. Create a connection with the provided JMS server Connection conn = cf.createConnection() ; //5. Create a thread of communication Session session = conn.createSession(false, Session.AUTO_ACKNOWLEDGE) ; //6. Create a message producer object MessageProducer producer = session.createProducer(myQueue) ; //7. Create a text message TextMessage msg = session.createTextMessage() ; msg.setText("Hello from the Queue Client") ; //8. Send the message producer.send(msg) ; //9. Close the Connection conn.close() ; } catch(Exception e) { e.printStackTrace() ; } 9
  • 10. } } 10