1 Import kebergantungan yang diperlukan untuk penyepaduan:
<dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-activemq</artifactid> </dependency>
2 Cipta fail application.properties
spring.activemq.broker-url=tcp://127.0.0.1:61616 spring.activemq.user=admin spring.activemq.password=admin server.port=8080 queue=myqueue
3 Sesuaikan fail konfigurasi QueueConfig ia berdasarkan nama baris gilir
package com.example.demo; import javax.jms.Queue; import org.apache.activemq.ActiveMQConnectionFactory; import org.apache.activemq.command.ActiveMQQueue; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.jms.config.DefaultJmsListenerContainerFactory; import org.springframework.jms.core.JmsTemplate; @Configuration public class QueueConfig { @Value("${queue}") private String queue; @Bean public Queue logQueue() { return new ActiveMQQueue(queue); }}
7. Mencipta pengguna agak mudah . Anda hanya perlu mendengar baris gilir:
package com.example.demo.producter; import javax.jms.Queue; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.jms.core.JmsMessagingTemplate; import org.springframework.stereotype.Component; import com.example.demo.SpringbootActivemqApplication; @Component public class Producter { @Autowired private JmsMessagingTemplate jmsMessagingTemplate; @Autowired private Queue queue; private static Logger logger = LoggerFactory.getLogger( Producter .class); public void send() { String str = "生产者生产数据:" + System.currentTimeMillis(); jmsMessagingTemplate.convertAndSend(queue, str); logger.info("生产者数据:{}", str); } }
8
Atas ialah kandungan terperinci Bagaimana untuk mengintegrasikan Activemq dalam Springboot. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!