spring boot 和activimq 整合

添加maven依赖

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

<dependency>
   <groupId>org.apache.activemq</groupId>
   <artifactId>activemq-web</artifactId>
   <version>5.14.0</version>
</dependency>

创建配置类 ActiveMqconfig

@Configuration
public class ActiveMqConfig {

    @Bean
    public ServletRegistrationBean ajaxServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new AjaxServlet());
        registration.setLoadOnStartup(1);
        //registration.setEnabled(true);
        registration.addUrlMappings("/amq/*");
        return registration;
    }


    @Bean
    public ServletContextInitializer initializer() {
        return new ServletContextInitializer() {

            @Override
            public void onStartup(ServletContext servletContext) throws ServletException {
                servletContext.setInitParameter("org.apache.activemq.brokerURL",    "tcp://localhost:61616");
            }
        };
    }
}

监听mq

@Component
public class ActiveMqServer {

    @JmsListener(destination = "topic")
    public void receiveTopic(String message) {
        System.out.println("监听topic=============监听topic");
        System.out.println(message);

    }

    @JmsListener(destination = "queue")
    public void receiveQueue(String message) {
        System.out.println("监听queue=============监听queue");
        System.out.println(message);

    }
}

发送消息

@Autowired
private JmsTemplate jmsTemplate;
jmsTemplate.convertAndSend(new ActiveMQTopic("topic"),"发送的topic数据!"+Math.random());


html 接收消息 和 发送

扫描二维码关注公众号,回复: 1850464 查看本文章

<link rel="stylesheet" th:href="@{/css/css.css}"/>
<script type="text/javascript" th:src="@{/js/jquery-1.4.2.min.js}"></script>
<script type="text/javascript" th:src="@{/js/amq_jquery_adapter.js}"></script>
<script type="text/javascript" th:src="@{/js/amq.js}"></script>

<body>
测试<br/>
<span id="test"></span><br>
<button type="button" id="send">发送</button>
</body>
<script th:inline="javascript">
    window.onload = function () {
        var msg = "<msg type='common'>"
            + "<id>msg1</id>"
            + "<content>This is test content"+Math.random()+"</content>"
            + "</msg>";

        var amq = org.activemq.Amq;
        amq.init({uri: '/activemq/amq', logging: false, timeout: 45, clientId: 'test1'});
        var myHandler = {
            rcvMessage: function (message) {
                //接收到消息后,自己的业务处理逻辑
                console.log(message);
                $("#test").text(message.data);
            }
        };
        var myHandler2 = {
            rcvMessage: function (message) {
                //接收到消息后,自己的业务处理逻辑
                console.log(message);
                $("#test").text(message.data);
            }
        };
        amq.addListener('test1', 'topic://topic', myHandler.rcvMessage);
        amq.addListener('test1', 'topic://TEST', myHandler2.rcvMessage);
        //test1为消费者的一个ID,接受到消息回调时会用到作为标识
        //topic://msg表示主题订阅目的地
        $("#send").click(function () {
                amq.sendMessage("topic://TEST", msg);
        });
    };
</script>



猜你喜欢

转载自blog.csdn.net/qq_33842795/article/details/80323004