mail@pastecode.io avatar
7 months ago
2.4 kB
Sure, let's adapt the example to use ActiveMQ instead of RabbitMQ. First, make sure you have ActiveMQ installed and running.

1. **Add ActiveMQ Dependencies:**
   Add the following dependencies to your `pom.xml` file:


2. **Configure ActiveMQ:**
   In your `application.properties` or `application.yml` file, configure ActiveMQ connection properties:


   Adjust the URL based on your ActiveMQ setup.

3. **Create a Message Producer:**
   import org.springframework.jms.core.JmsTemplate;
   import org.springframework.beans.factory.annotation.Autowired;
   import org.springframework.stereotype.Component;

   public class MessageProducer {

       private JmsTemplate jmsTemplate;

       public void sendMessage(String message) {
           jmsTemplate.convertAndSend("myQueue", message);
           System.out.println("Message sent: " + message);

4. **Create a Message Consumer:**
   import org.springframework.jms.annotation.JmsListener;
   import org.springframework.stereotype.Component;

   public class MessageConsumer {

       @JmsListener(destination = "myQueue")
       public void receiveMessage(String message) {
           System.out.println("Message received: " + message);
           // Add your processing logic here

5. **Run Your Application:**
   Your Spring Boot application will now connect to ActiveMQ. Make sure ActiveMQ is running, and you can access its admin console at `http://localhost:8161/admin` (default credentials: admin/admin).

   import org.springframework.boot.SpringApplication;
   import org.springframework.boot.autoconfigure.SpringBootApplication;

   public class MessageQueueApplication {

       public static void main(String[] args) {
           SpringApplication.run(MessageQueueApplication.class, args);

Now, when you call `sendMessage` in the `MessageProducer`, the message will be sent to the "myQueue" in ActiveMQ, and the `MessageConsumer` will receive and process it.
Leave a Comment