Untitled
unknown
plain_text
2 years ago
2.4 kB
6
Indexable
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:
```xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-activemq</artifactId>
</dependency>
</dependencies>
```
2. **Configure ActiveMQ:**
In your `application.properties` or `application.yml` file, configure ActiveMQ connection properties:
```properties
spring.activemq.broker-url=tcp://localhost:61616
```
Adjust the URL based on your ActiveMQ setup.
3. **Create a Message Producer:**
```java
import org.springframework.jms.core.JmsTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class MessageProducer {
@Autowired
private JmsTemplate jmsTemplate;
public void sendMessage(String message) {
jmsTemplate.convertAndSend("myQueue", message);
System.out.println("Message sent: " + message);
}
}
```
4. **Create a Message Consumer:**
```java
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;
@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).
```java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@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.Editor is loading...
Leave a Comment