springboot + activeMQ基本使用

尼祥哥發表於2020-12-02

一,maven引入依賴

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

二,配置訊息佇列

@Configuration
public class SystemConfig {
   
    //訊息佇列名稱
    @Bean(name = "queue")
    public Queue queue2() {
        return new ActiveMQQueue("sysLog-send");
    }
    @Bean
    public JmsListenerContainerFactory<?> jmsListenerContainerTopic() {
        DefaultJmsListenerContainerFactory bean = new DefaultJmsListenerContainerFactory();
        bean.setPubSubDomain(true);
        bean.setConnectionFactory(activeMQConnectionFactory);
        return bean;
    }

}

三,application.yml引入activeMQ配置

spring:
    activemq:
        broker-url: tcp://10.246.186.94:61616
        pool:
          enabled: true
          max-connections: 100
        in-memory: true

四,傳送端(生產者)

@RestController()
public class avtiveMQControllerTest {
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    @RequestMapping(value="/show/{name}", method = RequestMethod.GET)
    private void sendMessage(@PathVariable("name") String name){
        jmsTemplate.convertAndSend(destiationName, message);
        Map<String, Object> jmsNoticeMap = new HashMap<>(15);
        jmsNoticeMap.put("name", name);
        this.jmsTemplate.convertAndSend("sysLog-send", JSON.toJSONString(jmsNoticeMap, SerializerFeature.WriteMapNullValue, SerializerFeature.WriteNullStringAsEmpty, SerializerFeature.WriteNullNumberAsZero));
    }
}

五,接收端(消費者)

@Component
@Slf4j
public class activeMQJms {
    
    @JmsListener(destination = "sysLog-send")
    public void receiveScanResult2(String msg) {
        log.info("收到訊息:" + msg);
    }
}

 

相關文章