ssm集成activeMq消息中间件

一、由于项目上需要和其他项目进行消息对接,对方项目使用的是activeMq,所有下面贴下本人使用的经验,,之前是从来没有接触过,

二、这个只是接受消息的,至于发送消息,请问度娘,本人在此没有做研究

三、对接采用mqtt协议,采取发布/订阅模式

四、配置文件如下:

1,spring-mqtt.xml

<?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:int="http://www.springframework.org/schema/integration"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:int-mqtt="http://www.springframework.org/schema/integration/mqtt"
	xsi:schemaLocation="
        http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
        http://www.springframework.org/schema/integration/mqtt http://www.springframework.org/schema/integration/mqtt/spring-integration-mqtt-4.1.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.1.xsd">

	<!-- mqtt客户端 -->
	<bean id="clientFactory"
		class="org.springframework.integration.mqtt.core.DefaultMqttPahoClientFactory">
		<property name="userName" value="${mqtt.userName}" />
		<property name="password" value="${mqtt.passWord}" />
		<property name="cleanSession" value="${mqtt.cleanSession}" />
	</bean>

	<!-- 消息适配器 -->
	<int-mqtt:message-driven-channel-adapter
		id="mqttInbound" 
		client-id="${mqtt.client-id}" 
		url="${mqtt.brokerURL}"
		topics="${mqtt.topics}" 
		qos="${mqtt.qos}"
		client-factory="clientFactory" 
		auto-startup="true" 
		send-timeout="${mqtt.send-timeout}"
		channel="startCase" />
	<int:channel id="startCase"/>
	
	<!-- 消息处理类 ,必须返回ture才算处理完消息,false或则null都不行-->
	<int:service-activator id="startCaseService" input-channel="startCase" ref="mqttCaseService" method="startCase"/>
	<bean id="mqttCaseService" class="com.o2o.affairAndroid.controller.MqttService" />
</beans>

 

2,spring-mybatis.xml:

<?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:tx="http://www.springframework.org/schema/tx"   
    xsi:schemaLocation="http://www.springframework.org/schema/beans    
                        http://www.springframework.org/schema/beans/spring-beans-3.1.xsd    
                        http://www.springframework.org/schema/context    
                        http://www.springframework.org/schema/context/spring-context-3.1.xsd    
                        http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-3.1.xsd ">  
    <!-- 自动扫描 -->  
     <context:component-scan base-package="com.o2o.affair.service" />
     <context:component-scan base-package="com.o2o.affair.docking"/>
     <context:component-scan base-package="com.o2o.work.service" />
     <!-- 引入配置文件 -->
	<bean id="PropertyPlaceholderConfigurer"
		class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
		<property name="order" value="1" />
		<property name="ignoreUnresolvablePlaceholders" value="true" />
		<property name="locations">
			<list>
				<!--读取src目录的db.properties文件 -->
				<value>classpath:jdbc.properties</value>
				<!--读取src目录的activiMq.properties文件 -->
				<value>classpath:activiMq.properties</value>
			</list>
		</property>
	</bean>    
  
    <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"  
        destroy-method="close">  
        <property name="driverClassName" value="${driver}" />  
        <property name="url" value="${url}" />  
        <property name="username" value="${username}" />  
        <property name="password" value="${password}" />  
        <!-- 初始化连接大小 -->  
        <property name="initialSize" value="${initialSize}"></property>  
        <!-- 连接池最大数量 -->  
        <property name="maxActive" value="${maxActive}"></property>  
        <!-- 连接池最大空闲 -->  
        <property name="maxIdle" value="${maxIdle}"></property>  
        <!-- 连接池最小空闲 -->  
        <property name="minIdle" value="${minIdle}"></property>  
        <!-- 获取连接最大等待时间 -->  
        <property name="maxWait" value="${maxWait}"></property>
        <!-- 用于保持连接的测试语句 -->
        <property name="validationQuery" value="${validationQuery}"></property>
    </bean>  
  
    <!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->  
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">  
        <property name="dataSource" ref="dataSource" />  
        <!-- 自动扫描mapping.xml文件 -->  
        <property name="mapperLocations">
        	<array>
        		<value>classpath:com/o2o/affair/dao/*.xml</value>
        		<value>classpath:com/o2o/session/dao/*.xml</value>
        		<value>classpath:com/o2o/work/dao/*.xml</value>
        	</array>
        </property>  
        <property name="configLocation" value="classpath:mybatis-config.xml" />
    </bean>  
  
    <!-- DAO接口所在包名,Spring会自动查找其下的类 -->  
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">  
        <property name="basePackage" value="com.o2o.affair.dao,com.o2o.session.dao,com.o2o.work.dao" />  
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>  
    </bean>  
  
    <!-- (事务管理)transaction manager, use JtaTransactionManager for global tx -->  
    <bean id="transactionManager"  
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
        <property name="dataSource" ref="dataSource" />  
    </bean>  
  	<!-- 注解方式配置事物 -->
	<tx:annotation-driven transaction-manager="transactionManager" />
	
	<import resource="classpath:spring-mqtt.xml"/>
</beans>  

 

3,activeMq配置文件:

####################################ActiveMQ参数配置###################################
#MQTT-地址
#mqtt.brokerURL=tcp://127.0.0.1:1883
#MQTT-登陆名
mqtt.userName=admin
#MQTT-密码
mqtt.passWord=admin
#配合qos设置是否持久化订阅消息
mqtt.cleanSession=false
#监听级别,非0属于持久化订阅
mqtt.qos=2
#MQTT-客户端名称
mqtt.client-id=localhost
#MQTT-监听的主题
mqtt.topics=hj/#
#超时时间
mqtt.send-timeout=20000

 

4,方法处理类:

package com.o2o.affairAndroid.controller;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.alibaba.fastjson.JSONObject;
import com.o2o.affair.dao.AffairHajiaDataMapper;
import com.o2o.affair.model.AffairHajiaData;
import com.o2o.affair.service.IAffairWorkService;

/**
 * @Description:工单信息推送处理类
 * @author:Administrator
 * @time:2019年12月2日 下午6:24:00
 */
@Component
public class MqttService {
	
	private static final Logger logger=Logger.getLogger(MqttService.class);
	
	/**
	 * @Description:此方法属于配置文件制定的接收方法,不可更改名字
	 * @param message
	 * @exception:
	 * @author: Administrator
	 * @time:2019年12月2日 下午6:24:28
	 */
	public void startCase(String message) {
		try {
            //处理消息


		} catch (Exception e) {
			e.printStackTrace();
			logger.info("解析数据失败");
		}
	}
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值