Junior — Middle
Can you explain what is meant by a message broker in system architecture?
sobes.tech AI
Answer from AI
A message broker is a component in distributed system architecture responsible for receiving, storing, and transmitting messages between different services or applications. It enables asynchronous communication, allowing the sender and receiver to operate independently of each other's availability.
Main functions of a message broker:
- Buffering messages to ensure delivery even when the receiver is temporarily unavailable.
- Routing messages through the appropriate channels or queues.
- Ensuring system reliability and scalability.
Examples of message brokers: Apache Kafka, RabbitMQ, ActiveMQ.
Example of usage in Java with RabbitMQ:
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
try (Connection connection = factory.newConnection();
Channel channel = connection.createChannel()) {
channel.queueDeclare("task_queue", true, false, false, null);
String message = "Hello World!";
channel.basicPublish("", "task_queue", MessageProperties.PERSISTENT_TEXT_PLAIN, message.getBytes());
System.out.println(" [x] Sent '" + message + "'");
}