一、由於項目上需要和其他項目進行消息對接,對方項目使用的是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("解析數據失敗");
}
}
}