0% found this document useful (0 votes)
108 views7 pages

Message Queues (ActiveMQs and Kafka)

The document discusses message queues and message brokers. It explains how message queues work as middleware between applications, allowing producers to send data and consumers to receive it. It covers common use cases and types of message brokers, including Apache ActiveMQ and Apache Kafka.

Uploaded by

Java Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
108 views7 pages

Message Queues (ActiveMQs and Kafka)

The document discusses message queues and message brokers. It explains how message queues work as middleware between applications, allowing producers to send data and consumers to receive it. It covers common use cases and types of message brokers, including Apache ActiveMQ and Apache Kafka.

Uploaded by

Java Pro
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Message Queues (MQs)

Message Queue:
- It is a middleware between multiple application used to send data.

==============
Message Broker:
==============
1. The continues data flow between two systems. Producer will send data and
consumer will read by using a connector/mediator i.e. 'Message Broker'.

2. Client- Server applications


works using HTTP protocol. The client has to make request then server will
process it and gives response back to client.

3. But here, one time connection between producer-consumer is made using


Message broker, then continuous data flow is done. Consumer need not to make
request again and again. one time connection is fine.
Where MQs are use? Or Use cases?
4. Live Cricket Score
5. Foodpanda delivery Man status
6. Uber cab Driver status
7. Live Location
8. Live Stack Market data
Note: MQs concept use TCP protocol to exchange Data.
There are two Types of Message Broker
1. Basic MQs (Single Broker – No LoadBalancer)
 Examples: JMS (Java Message Service), Apache ActiveMQ, RabbitMQ… etc.
Note: Apache ActiveMQ contain single broker

2. Advance MQs (Multi Broker – LoadBalancer)


Message Queues (MQs)
 Example: Apache Kafka
==========================
Message Oriented Middleware
==========================
9. One MOM (Message Oriented Middleware) is used to connect two systems
[Producer and Consumer]. That connection is Unidirectional.

10.Here, there is no direct connection is created between producer and


consumer i.e. do not share IP/PORT details to each other.
11.So, how to connect and Send data? Using MOM. Producer send Message to
MoM and MoM send to Consumer.
12.If MOM has multiple Consumers, then how to identify exact consumer to
send data? By using Destination.
13.Producer given one Destination name and at consumer side one destination
name. Consumer side destination name must match with Producer side
destination name.
14.If multiple Consumers has same destination name, then it gives error.
There are two types of communications.
1. Peer-To-Peer Communication [P2P]
 If one message is sent to one Consumer, then it is called as P2P.
 In this case Destination type is known as: Queue.
Message Queues (MQs)

--------------------------
Application: Set Up
--------------------------
1. Download Apache ActiveMQ’s from
https://activemq.apache.org/components/classic/download/

2. Extract zip  go to bin folder  win64/win32  double click on ActiveMQ.bat to


start server

3. Open Browser and enter following url: http://locahost:8161/admin

4. Enter username: admin and password: admin

5. To stop server press ctrl+c

------------------------------------
Producer Application code
------------------------------------
1. Application Name: SpringCloud-ProducerApp
2. Add dependence: ActiveMQ’s 5
3. In properties file
 To connect with MOM
 spring.activemq.broker-url= tcp://localhost:61616
 spring.activemq.user= admin
 spring.activemq.password = admin
 Active P2P communication type
 spring.jms.pub-sub-domain=false
 Destination name
 my.app.dst-name = my-dst-q1
4. Create Two classes
 First one containing ‘sending message functionalities.
 Another is Scheduler class that generate message after a particular time
period.
5. Add Annotation: “@Scheduling” for Scheduler on top of the starter class.
Message Queues (MQs)

---------------------------------------- ProducerService code ------------------------------------


1. @Component
2. public class ProducerService {
3. @Autowired
4. private JmsTemplate jt;
5.
6. @Value("${my.app.desti-name}")
7. private String destinationName;
8.
9. public void sendMessage(String message) {
10. jt.send(destinationName,
11. session->session.createTextMessage(message));
12. System.out.println("Message sending....");
13. }
14. }

-------------------------------------- Message Scheduler code ------------------------------------


1. @Component
2. public class MessageScheduler {
3.
4. @Autowired
5. private ProducerService service;
6.
7. @Scheduled(cron = "*/10 * * * * *")
8. public void sentMSg() {
9. service.sendMessage("Hello "+ new Date());
10. }
11. }

-------------------------------------
Consumer Application code
-------------------------------------
1. Application Name: SpringCloud-ConsumerApp
2. Add dependence: ActiveMQ’s 5
3. Add annotation: “@EnableJms” on top of the starter class
4. In properties file (Same as Producer App)
 Note: Destination name must be matched with Producer Application.
5. Define One Class that Read message for Producer App.
 Add @Component annotation on top of this class.
Message Queues (MQs)
 Define one method with parameter (String) and add
@JmsListener(destination= “${key}”) annotation.
 Read message
------------------------------------------- ConsumerApp Code ---------------------------------------------
1. @Component
2. public class ConsumerApp {
3.
4. @JmsListener(destination = "${my.app.desti-name}")
5. public void readMsg(String msg) {
6. System.out.println(msg);
7. }
8. }

-----------------------
Execution Order
-----------------------
1. Run ActiveMQ
2. Run Consumer App
3. Runt Producer App

2. Publish-Subscribe Communication [Pub/Sub]


 If one message is sent to Multiple Consumers then it is called as
Pub/Sub.
 In this case Destination type is known as: Topic.
 Example: Govt messages send to public, Live cricket Score etc.
Message Queues (MQs)

Apache Kafka

15. It is an Open-Source API, Message Queue services with High-throughput and low-
latency. And also streaming platform.
 Hight throughput and low latency: fast delivery using less network.
 Message Queue Services: to send data multiple application at a time.
16. Kafka supports Pub/Sub Model only.
17. Scaling/load balancing can be done by cluster.

Full Kafka S/w is called as Ecosystem. This system contains mainly 3 parts. They are:
1. Zoo Keeper
2. Kafka Cluster
3. Topics
Message Queues (MQs)
Zoo Keeper:
18. Provide environment to run Kafka server.
19. Controls the entire Eco-System like create/manage topic, allocate a broker to
consumer, how many brokers are busy, increase cluster size… etc.
Kafka Cluster
20. Contain Group of Message Broker.
Topics
21. It is a memory that holds data in packets formats. Those are identified using index
numbers starts from zero [offset].
Message Replica (MR):
22. Massage Replica creates cloned copies of actual data to send it to consumer.

=============
Kafka Set Up
=============

1. Download Apache Kafka from https://kafka.apache.org/downloads


2. Extract zip file
3.

You might also like