ssm整合ActiveMQ消息中间件

模块之间的依赖也称之为耦合。而耦合越多,之后的维护工作就越困难。那么如果改善系统模块调用关系、减少模块之间的耦合呢?我们接下来就介绍一种解决方案----消息中间件

1. Spring整合ActiveMQ

环境配置

  • 官网地址: https://activemq.apache.org/
  • 下载成功后解压,进入bin目录执行 ./active start即可
  • web 服务器地址:8161
  • 服务调用接口 : 61616
  • 用户名密码均为 admin

1.1 环境准备

  • pom文件引入坐标依赖
	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-context</artifactId>
  		<version>4.1.7.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-test</artifactId>
  		<version>4.1.7.RELEASE</version>
  	</dependency>
  	<dependency>
  		<groupId>junit</groupId>
  		<artifactId>junit</artifactId>
  		<version>4.12</version>
  	</dependency>
	<!--Spring整合activeMQ-->
  	<dependency>
  		<groupId>org.apache.activemq</groupId>
  		<artifactId>activemq-all</artifactId>
  		<version>5.14.0</version>
  	</dependency>
  	<dependency>
  		<groupId>org.springframework</groupId>
  		<artifactId>spring-jms</artifactId>
  		<version>4.1.7.RELEASE</version>
  	</dependency>
  • resources/spring/application.xml 配置文件中整合activeMq
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 把AcitveMQ连接工厂交给spring管理 -->
	<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.120:61616"></constructor-arg>
	</bean>

	<!-- 用springjms工厂接管activeMQ-->
	<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
	</bean>

	<!-- 创建springjms提供的模板对象 -->
	<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
		<property name="connectionFactory" ref="singleConnectionFactory"></property>
	</bean>

	<!-- 指定消息发送空间 点对点模式-->
	<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="queue"></constructor-arg>
	</bean>

	<!-- 指定消息发送空间 点对点模式-->
	<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="topic"></constructor-arg>
	</bean>

</beans>

1.2 spring整合后发送消息

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 14:44
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:spring/applicationContext-mq-provider.xml")
public class SpringProvider {

    // 注入消息模板对象
    @Autowired
    private JmsTemplate jmsTemplate;

    // 注入点对点消息空间对象
    @Autowired
    private ActiveMQQueue activeMQQueue;

    // 注入发布订阅消息空间对象
    @Autowired
    private ActiveMQTopic activeMQTopic;


    /*
    * 模式: 点对点
    * 需求发送消息
    * */
    @Test
    public void sendMessageQueue(){
        jmsTemplate.send(activeMQQueue, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("spring整合activeMQ,点对点模式发送消息");
            }
        });
    }
    
    /*
     * 模式: 发布订阅
     * 需求发送消息
     * */
    @Test
    public void sendMessageTopic(){
        jmsTemplate.send(activeMQTopic, new MessageCreator() {
            @Override
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage("spring整合activeMQ,发布订阅模式发送消息");
            }
        });
    }
}

  • 结果展示

在这里插入图片描述在这里插入图片描述

5.2 spring整合后接收消息

  • application配置文件中需要配置接收消息的参数
  • 更改点对点和发布订阅模式只需要更改配置文件即可
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dubbo="http://code.alibabatech.com/schema/dubbo" xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

	<!-- 把AcitveMQ连接工厂交给spring管理 -->
	<bean id="activeMQConnectionFactory" class="org.apache.activemq.ActiveMQConnectionFactory">
		<constructor-arg name="brokerURL" value="tcp://192.168.25.120:61616"></constructor-arg>
	</bean>

	<!-- 用springjms工厂接管activeMQ-->
	<bean id="singleConnectionFactory" class="org.springframework.jms.connection.SingleConnectionFactory">
		<property name="targetConnectionFactory" ref="activeMQConnectionFactory"></property>
	</bean>


	<!-- 指定消息发送空间 点对点模式-->
	<bean id="queue" class="org.apache.activemq.command.ActiveMQQueue">
		<constructor-arg value="queue"></constructor-arg>
	</bean>

	<!-- 指定消息发送空间 点对点模式-->
	<bean id="topic" class="org.apache.activemq.command.ActiveMQTopic">
		<constructor-arg value="topic"></constructor-arg>
	</bean>

	<!--监听器对象 接收消息 自己定义-->
	<bean id="myMessageListener" class="com.tysc.search.service.listener.MyMessageListener"></bean>

	<!-- 监听器出发监听容器,spring jms提供的-->
	<bean id="listenerContainer" class="org.springframework.jms.listener.DefaultMessageListenerContainer">
		<property name="connectionFactory" ref="singleConnectionFactory"></property>
		<!-- 指定接收消息的服务地址-->
		<property name="destination" ref="queue"></property>
		<!-- 指定消息监听器-->
		<property name="messageListener" ref="myMessageListener"></property>
	</bean>

</beans>

定义监听器

package com.tysc.search.service.listener;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.MessageListener;
import javax.jms.TextMessage;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 15:02
 */
public class MyMessageListener implements MessageListener {
    @Override
    public void onMessage(Message message) {
        // 获取消息
        if (message instanceof TextMessage){
            TextMessage msg = (TextMessage) message;
            try {
                String text = msg.getText();
                System.out.println("接收消息: "+text);
            } catch (JMSException e) {
                e.printStackTrace();
            }
        }
    }
}

接收消息

import org.apache.activemq.command.ActiveMQQueue;
import org.apache.activemq.command.ActiveMQTopic;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

/**
 * @ Description
 * @ auther          宁宁小可爱
 * @ create          2020-02-05 14:44
 */
public class SpringConsumer {
    /*
     * 需求: 接收消息
     * 因为我们有自己定义的监听器,所以不需要做任何事情,只需要加载配置文件即可
     *  */
    @Test
    public void recvMessageTopic() throws Exception{
        ApplicationContext app = new ClassPathXmlApplicationContext("classpath*:spring/applicationContext-mq-consumer.xml");
        System.in.read();
    }
}

  • 结果展示

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叫我三胖哥哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值