1、引入依赖的jar

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>
    </dependencies>
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.

 

2、application配置

spring.activemq.broker-url=tcp://localhost:61616 
spring.activemq.in-memory=true
spring.activemq.pool.enabled=true
  • 1.
  • 2.
  • 3.

 

3、单向发送消息

@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = MqApplication.class)
public class MqTest {

    @Autowired
    JmsMessagingTemplate jmsMessagingTemplate;
    
    //发送String消息
    @Test
    public void testStr() {
        //往luna队列发送消息
        jmsMessagingTemplate.convertAndSend("luna","to luna queue");
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.

 

     接收消息

@Component
@Slf4j
public class Consumer {
    @JmsListener(destination = "luna")
    public void receiveQueue(String text){
        log.info("receive:{}",text);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

 

   查看ActiveMQ控制台

【springboot】之整合ActiveMQ_spring

 

 控制台打印

【springboot】之整合ActiveMQ_spring_02

 

4、双向发送消息

例如 A发送B消费   B消费完毕将结果放在某一个队列中,A再去消费

 我们将上面代码继续改造一下

@Component
@Slf4j
public class Consumer {

    @JmsListener(destination = "luna")
    @SendTo("luna_ret")//将消费结果返回
    public String receiveQueue(String text){
        log.info("receive:{}",text);
        return "SUCCESS--"+text;
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.

写一个消费luna_ret的queue

@Component
@Slf4j
public class ConsumerRet {

    @JmsListener(destination = "luna_ret")
    public void receiveQueue(String text){
        log.info("receive:{}",text);
    }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

 

ok,我们再执行上面的发送queues代码

ActiveMQ控制台显示

【springboot】之整合ActiveMQ_ci_03

 

控制台日志显示

【springboot】之整合ActiveMQ_ci_04

 

 由上可知,

@SendTo 注解

可以将返回值发送到指定的queue

----------------------------------------------- 

springboot整合ActiveMQ安全配置

 

【ActiveMQ】之安全机制(一)管控台安全设置 

【ActiveMQ】之安全机制(二)客户端连接安全