在springboot中使用activeMQ

生产者应用的搭建:

1、添加maven依赖

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
</dependency>

2、在application.properties文件中添加activeMQ的配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=false

其中,
broker-url是activeMQ服务器地址,端口61616是在/config/activmq.xml中name="openwire"的配置对应的端口;
user和password是在activeMQ服务器的/config/user.properites中的配置;
注意pub-sub-domain配置,如果该配置为true,表示该springboot支持topic模式的消息,如果为false,表示支持queue模式的消息。两者不能同时支持;

3、通过JmsMessagingTemplate生产消息

生产者通过JmsMessagingTemplate来生产消息,具体api为:

jmsMessagingTemplate.convertAndSend(Destination destination, String msg);
其中Destination是一个空接口,它的两个子接口为Topic和Queue,分别表示主题和队列两种模式的消息

ActiveMQ中提供了ActiveMQTopic和ActiveMQQueue两个实现类,分别实现Topic和Queue接口。这两个实现类通过new ActiveMQQueue(String name)的构造器实例化。构造器中的name表示了该队列的名称,在消费者中通过这个名称监听队列的消息;

 

消费者应用的搭建:

1、添加maven依赖
<参照前文>

2、在application.properties文件中添加activeMQ的配置

spring.activemq.broker-url=tcp://localhost:61616
spring.activemq.in-memory=true
spring.activemq.pool.enabled=false
spring.activemq.user=admin
spring.activemq.password=admin
spring.jms.pub-sub-domain=false

注意pub-sub-domain配置与生产者的配置含义一样。

3、通过@JmsListener监听消息

    @JmsListener(destination = "promoteAct")
    public void receiveQueue(String msg) {
        System.out.println(msg+"消息已经消费了,消费者1");
    }

@JmsListener注解中的destination属性,指向消息队列的name,也就是在生产者中构造消息队列时的实参。

 

其他

1、生产者和消费者并没有绝对的区分,同一个应用,既可以是生产者,也可以是消费者。
2、消费者收到消息之后,可以通过@SendTo注解再将消息继续传播出去,当使用@SendTo注解就隐性创建了一个队列(或主题,取决于原始消息的类型),这个消息队列同样可以通过@JmsListener的形式被监听到;

@JmsListener(destination = "topicAct")
@SendTo("another_topic")
public String receiveTopic(String consumer) {
    System.out.println(consumer + "___   topic 消息已经消费了,消费者2");
    return "继续转发的消息";
}

 

疑问:
1、消息的持久化
2、集群配置

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用以下步骤在Spring Boot项目集成ActiveMQ: 1. 在pom.xml添加ActiveMQ依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-activemq</artifactId> </dependency> ``` 2. 在application.properties文件添加ActiveMQ配置: ```properties spring.activemq.broker-url=tcp://localhost:61616 spring.activemq.user=admin spring.activemq.password=admin ``` 3. 创建一个消息生产者: ```java import javax.jms.ConnectionFactory; import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; @Component public class MessageProducer { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; public void send(String message) { this.jmsMessagingTemplate.convertAndSend(queue, message); } } ``` 4. 创建一个消息消费者: ```java import javax.jms.Queue; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.annotation.JmsListener; import org.springframework.stereotype.Component; @Component public class MessageConsumer { @Autowired private Queue queue; @JmsListener(destination = "${spring.activemq.queue-name}") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 5. 在应用程序使用消息生产者发送消息: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.CommandLineRunner; import org.springframework.stereotype.Component; @Component public class MyApp implements CommandLineRunner { @Autowired private MessageProducer messageProducer; @Override public void run(String... args) throws Exception { messageProducer.send("Hello, World!"); } } ``` 这样就可以在你的Spring Boot应用程序使用ActiveMQ了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值