天天看點

SpringBoot整合ActiveMQ:專業負責MQ20年~

SpringBoot整合ActiveMQ:專業負責MQ20年~

ACTIVEMQ

ActiveMQ

下載下傳MQ後啟動一下(Windows版本)

#啟動資訊
...
jvm 1    |  INFO | Listening for connections at ws://JackWen:61614?maximumConnections=1000&wireFormat.maxFrameSize=104857600
jvm 1    |  INFO | Connector ws started
jvm 1    |  INFO | Apache ActiveMQ 5.14.0 (localhost, ID:JackWen-3764-1535341346100-0:1) started
jvm 1    |  INFO | For help or more information please see: http://activemq.apache.org
jvm 1    |  WARN | Store limit is 102400 mb (current store usage is 4 mb). The data directory: D:\ActiveMQ\apache-activemq-5.14.0\bin\win32\..\..\data\kahadb only has 56508 mb of usable space. - resetting to maximum available disk space: 56508 mb
jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
jvm 1    |  INFO | ActiveMQ WebConsole available at http://0.0.0.0:8161/
jvm 1    |  INFO | ActiveMQ Jolokia REST API available at http://0.0.0.0:8161/api/jolokia/
jvm 1    |  INFO | Initializing Spring FrameworkServlet 'dispatcher'
jvm 1    |  INFO | No Spring WebApplicationInitializer types detected on classpath
jvm 1    |  INFO | jolokia-agent: Using policy access restrictor classpath:/jolokia-access.xml
jvm 1    |  INFO | Connector vm://localhost started
jvm 1    |  WARN | Transport Connection to: tcp://127.0.0.1:4013 failed: java.net.SocketException: Connection reset

           
通路: http://localhost:8161/

與SpringBoot項目整合

pom.xml

<!--activeMQ-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-activemq</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-pool</artifactId>
        </dependency>
           

application.properties

#ActiveMQ
spring.activemq.broker-url=tcp://localhost:61616
           
SpringBoot整合ActiveMQ:專業負責MQ20年~

mq

生産者:

implements MessageCreator

/**
 * 消息生産者
 */
public class Producer implements MessageCreator {
    @Override
    public Message createMessage(Session session) throws JMSException {
        return session.createTextMessage("消息生産者建立的一條消息");
    }
}
           
消費者:

@JmsListener(destination = "xxx")

@Component
public class Consumer {
    @JmsListener(destination = "message_LPMQ")
    public void getMessage(String message){
        System.out.println(message);
    }
}
           
SpringBoot啟動類測試:

@Autowired JmsTemplate and implements CommandLineRunner

@SpringBootApplication
public class SpringbootdemoApplication implements CommandLineRunner {
 public static void main(String[] args) {

        SpringApplication.run(SpringbootdemoApplication.class, args);
    }

    /**
     * MQ: 發送消息
     */
    @Autowired
    private JmsTemplate jmsTemplate;

  
    @Override
    public void run(String... args) throws Exception {
        jmsTemplate.send("message_LPMQ",new Producer());
    }
}
           
SpringBoot整合ActiveMQ:專業負責MQ20年~

test

SpringBoot整合ActiveMQ:專業負責MQ20年~

消息中心