Web項目容器集成ActiveMQ & SpringBoot整合ActiveMQ


  集成tomcat就是隨項目啟動而啟動tomcat,最簡單的方法就是監聽器監聽容器創建之后以Broker的方式啟動ActiveMQ。

1.web項目中Broker啟動的方式進行集成

  在這里采用Listener監聽ServletContext創建和銷毀進行Broker的啟動和銷毀。

0.需要的jar包:

1.listener實現ServletContextListener接口

package cn.qlq.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.broker.BrokerService;

public class ActiveMQListener implements ServletContextListener {

    private static final BrokerService brokerService = new BrokerService();

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            brokerService.stop();
            System.out.println("broker 停止");
        } catch (Exception e) {

        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            brokerService.setUseJmx(true);
            brokerService.addConnector("tcp://localhost:61616");
            brokerService.start();
            System.out.println("broker 啟動");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

 

web.xml進行監聽器的配置:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>MyWeb</display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>

    <listener>
        <listener-class>cn.qlq.listener.ActiveMQListener</listener-class>
    </listener>

</web-app>

 

測試方法:向容器中發送和接收消息,成功證明整合成功。

2.SpringBoot中以Broker方式啟動ActiveMQ

1.maven中增加如下配置:

        <dependency>
            <groupId>org.apache.activemq</groupId>
            <artifactId>activemq-core</artifactId>
            <version>5.5.1</version>
        </dependency>

 

2.編寫Listener

package cn.qlq.listener;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.activemq.broker.BrokerService;

public class ActiveMQListener implements ServletContextListener {
    private static final BrokerService brokerService = new BrokerService();

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        try {
            brokerService.stop();
            System.out.println("broker 停止");
        } catch (Exception e) {

        }
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            brokerService.setUseJmx(true);
            brokerService.addConnector("tcp://localhost:61616");
            brokerService.start();
            System.out.println("broker 啟動");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

 

3.注冊Listener到容器中:

package cn.qlq.config;

import org.springframework.boot.web.servlet.ServletListenerRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import cn.qlq.listener.ActiveMQListener;/**
 * 
 * @author Administrator
 *
 */
@Configuration
public class ListenerConfig {
    
    @Bean
    public ServletListenerRegistrationBean<ActiveMQListener> listenerRegist3() {
        ServletListenerRegistrationBean<ActiveMQListener> srb = new ServletListenerRegistrationBean<ActiveMQListener>();
        srb.setListener(new ActiveMQListener());
        return srb;
    }

}

 

 測試方法:向容器中發送和接收消息,成功證明整合成功。

 

3.SpringBoot整合ActiveMQ進行開發

 pom.xml

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

 

application.properties增加activeMQ配置

spring.activemq.brokerUrl=tcp://127.0.0.1:61616

#spring.activemq.user=admin
#spring.activemq.password=123456
#spring.activemq.in-memory=true
#spring.activemq.pool.enabled=false

 

編寫生產者代碼:

package cn.qlq.activemq;

import javax.jms.Destination;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsMessagingTemplate;
import org.springframework.stereotype.Component;

@Component("producer")
public class Producer {
    // 也可以注入JmsTemplate,JmsMessagingTemplate對JmsTemplate進行了封裝
    @Autowired
    private JmsMessagingTemplate jmsTemplate;

    // 發送消息,destination是發送到的隊列,message是待發送的消息
    public void sendMessage(Destination destination, final String message) {
        jmsTemplate.convertAndSend(destination, message);
    }
}

 

消費者代碼:

package cn.qlq.activemq;

import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

@Component
public class Consumer {

    // 使用JmsListener配置消費者監聽的隊列,其中text是接收到的消息
    @JmsListener(destination = "myQueue")
    public void receiveQueue(String text) {
        System.out.println("Consumer收到的報文為:" + text);
    }

}

 

 測試代碼:

import javax.jms.Destination;

import org.apache.activemq.command.ActiveMQQueue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import cn.qlq.MySpringBootApplication;
import cn.qlq.activemq.Producer;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
public class PlainTest {
    @Autowired
    private Producer producer;

    @Test
    public void contextLoads() throws InterruptedException {
        Destination destination = new ActiveMQQueue("myQueue");

        for (int i = 0; i < 5; i++) {
            producer.sendMessage(destination, "message" + i);
        }
    }
}

 

  當然了這種方式也可以使用外部的ActiveMQ,也就是不用Broker方式啟動ActiveMQ,以bat文件啟動ActiveMQ之后整合方式同上。

  git地址:https://github.com/qiao-zhi/springboot-ssm.git 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM