项目结构
添加相关的jar包: pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.zgp.alarm</groupId>
<artifactId>AlarmStatusManageNew</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.7</maven.compiler.source>
<maven.compiler.target>1.7</maven.compiler.target>
<junit.version>4.11</junit.version>
<aspectj.version>1.6.8</aspectj.version>
<spring.version>5.0.7.RELEASE</spring.version>
<servlet.version>3.1.0</servlet.version>
<jsp.version>2.3.1</jsp.version>
<jstl.version>1.2</jstl.version>
<slf4j.version>1.7.12</slf4j.version>
<log4j.version>1.2.17</log4j.version>
<mysql.version>5.1.6</mysql.version>
<mybatis.version>3.4.5</mybatis.version>
<mybatis-spring.version>1.3.1</mybatis-spring.version>
<pagehelper.version>5.0.3</pagehelper.version>
<sqlserver.version>4.0</sqlserver.version>
<druid.version>1.1.9</druid.version>
<jackson.version>2.9.5</jackson.version>
<!-- 整合RabbitMQ -->
<rabbitmq.version>2.1.2.RELEASE</rabbitmq.version>
<!-- apache 公共资源包 -->
</properties>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- 添加Spring支持 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jms</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- spring redis配置: 注意 spring5以上需要配置 spring-data-redis 2.0 -->
<!-- https://docs.spring.io/spring-data/redis/docs/2.2.12.RELEASE/reference/html/#new-features -->
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.0.0.RELEASE</version>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<!-- 添加Servlet Api -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>${servlet.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>${jsp.version}</version>
<scope>provided</scope>
</dependency>
<!-- 添加jtl支持 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>${jstl.version}</version>
</dependency>
<!-- 添加日志支持 -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
</dependency>
<!-- 添加mybatis支持 -->
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
<version>${mybatis.version}</version>
</dependency>
<dependency>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
<version>${mybatis-spring.version}</version>
</dependency>
<!--MyBatis的PageHelper插件-->
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper</artifactId>
<version>${pagehelper.version}</version>
</dependency>
<!-- sqlserver驱动 -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>sqljdbc4</artifactId>
<version>${sqlserver.version}</version>
</dependency>
<!-- 添加连接池druid支持 -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>${druid.version}</version>
</dependency>
<!-- apache公共包 -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.11</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.2</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<!-- 文件上传 commons-fileupload 、commons-io -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.3</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
<!--Jackson required包-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson.version}</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>${jackson.version}</version>
</dependency>
<!-- 配置 quartz 定时器 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
<artifactId>quartz</artifactId>
<version>2.3.0</version>
</dependency>
<!-- 使用httpclient需要的maven依赖 -->
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpcore</artifactId>
<version>4.4.10</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient -->
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<version>4.5.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml-schemas</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-scratchpad</artifactId>
<version>4.1.0</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>ooxml-schemas</artifactId>
<version>1.4</version>
</dependency>
<!-- ********************************** 版本冲突问题 ************************************ -->
<!--
activemq-all-5.14.3 与 spring 5.X 冲突
解决办法
1、activemq-all-5.14.3 与 spring 4.2.4 冲突
2、spring5.X 、activemq-all-5.11.1(没有spring依赖)、javax.jms-api
-->
<!-- 配置activemq -->
<!-- https://mvnrepository.com/artifact/org.apache.activemq/activemq-all -->
<!-- <dependency>-->
<!-- <groupId>org.apache.activemq</groupId>-->
<!-- <artifactId>activemq-all</artifactId>-->
<!-- <version>5.11.1</version>-->
<!-- </dependency>-->
<!-- <!– https://mvnrepository.com/artifact/javax.jms/javax.jms-api –>-->
<!-- <dependency>-->
<!-- <groupId>javax.jms</groupId>-->
<!-- <artifactId>javax.jms-api</artifactId>-->
<!-- <version>2.0.1</version>-->
<!-- </dependency>-->
<!-- RabbitMQ -->
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>${rabbitmq.version}</version>
</dependency>
<!-- APP消息推送工具: 个推 -->
<dependency>
<groupId>com.getui.push</groupId>
<artifactId>restful-sdk</artifactId>
<version>1.0.0.0</version>
</dependency>
<!-- ehcache 相关依赖
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.8.2</version>
</dependency>
-->
<!-- 添加shiro支持
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-core</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-web</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-spring</artifactId>
<version>1.3.2</version>
</dependency>
-->
<!-- Shiro框架中配置的ehcache
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache-core</artifactId>
<version>2.6.11</version>
</dependency>
<dependency>
<groupId>org.apache.shiro</groupId>
<artifactId>shiro-ehcache</artifactId>
<version>1.3.2</version>
</dependency>
-->
<!-- jdbc驱动包
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.37</version>
</dependency>
-->
<!-- 添加百度编辑器ueditor支持
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.1</version>
</dependency>
-->
<!-- json-lib 依赖包
<dependency>
<groupId>net.sf.ezmorph</groupId>
<artifactId>ezmorph</artifactId>
<version>1.0.6</version>
</dependency>
-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>AlarmStatusManage</finalName>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<!-- 配置tomcat -->
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<port>8080</port>
<path>/AlarmStatusManage</path>
<uriEncoding>UTF-8</uriEncoding>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
配置文件及java类
1、config.properties、log4j.properties
config.properties
#RabbitMQ Settings
# rabbit ip
rabbit.server=192.168.0.103
# rabbit 端口
rabbit.port=5672
# virtual-host 默认为 /
rabbit.vhost=/
# rabbit 用户名
rabbit.username=admin
# rabbit 密码
rabbit.password=admin
# 队列名称
rabbit.fanoutQueue=fanoutQueue
# Exchange名称
rabbit.fanoutExchange=fanoutExchange
# jdbc connection
jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver
jdbc.url=jdbc:sqlserver://192.168.0.103:1433;DatabaseName=alarm
jdbc.username=sa
jdbc.password=123456
onfig.properties
log4j.rootLogger=INFO,ERROR,Console
#Console #输出到控制台
log4j.appender.Console = org.apache.log4j.ConsoleAppender
log4j.appender.Console.layout = org.apache.log4j.PatternLayout
log4j.appender.Console.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.Console.Threshold = DEBUG
log4j.appender.Console.ImmediateFlush = TRUE
log4j.appender.Console.Target = System.out
#输出到文件
log4j.appender.logFile = org.apache.log4j.FileAppender
log4j.appender.logFile.layout = org.apache.log4j.PatternLayout
log4j.appender.logFile.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.logFile.Threshold = DEBUG
log4j.appender.logFile.ImmediateFlush = TRUE
log4j.appender.logFile.Append = TRUE
log4j.appender.logFile.File = ${catalina.base}/logs/AlarmStatus_INFO.log
log4j.appender.logFile.Encoding= UTF-8
#按DatePattern输出到文件
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.ImmediateFlush = TRUE
log4j.appender.ERROR.Append = TRUE
log4j.appender.ERROR.File = ${catalina.base}/logs/AlarmStatus_ERROR.log
log4j.appender.ERROR.DatePattern = '_'yyyy-MM-dd'.log'
#设定文件大小输出到文件
log4j.appender.INFO = org.apache.log4j.RollingFileAppender
log4j.appender.INFO.layout = org.apache.log4j.PatternLayout
log4j.appender.INFO.layout.ConversionPattern = [%-5p][%-22d{yyyy/MM/dd HH:mm:ssS}][%l]%n%m%n
log4j.appender.INFO.Threshold = INFO
log4j.appender.INFO.ImmediateFlush = TRUE
log4j.appender.INFO.Append = TRUE
log4j.appender.INFO.File = ${catalina.base}/logs/AlarmStatus_INFO.log
log4j.appender.INFO.MaxFileSize = 20MB
log4j.appender.INFO.MaxBackupIndex = 10
2、RabbitMQ配置文件: spring-rabbitmq-server.xml、spring-rabbitmq-client.xml
spring-rabbitmq-server.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:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- rabbitMq 发送端 -->
<!-- <context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true" />-->
<!-- 配置连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbit.server}"
port="${rabbit.port}"
username="${rabbit.username}"
password="${rabbit.password}"
virtual-host="${rabbit.vhost}"
publisher-confirms="true"
publisher-returns="true"
channel-cache-size="5" />
<!-- 定义mq管理 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 声明队列 -->
<!-- <rabbit:queue name="queue" auto-declare="true" durable="true" />-->
<rabbit:queue name="${rabbit.fanoutQueue}" auto-declare="true" durable="true" />
<!-- 配置广播模式:fanout -->
<!-- 定义fanout exchange,绑定queue -->
<rabbit:fanout-exchange name="${rabbit.fanoutExchange}" auto-declare="true" durable="true">
<rabbit:bindings>
<rabbit:binding queue="${rabbit.fanoutQueue}"/>
</rabbit:bindings>
</rabbit:fanout-exchange>
<!-- 定义交换机绑定队列(路由模式)
<rabbit:direct-exchange name="IExchange" id="IExchange">
<rabbit:bindings>
<rabbit:binding queue="queue" key="queuekey" />
</rabbit:bindings>
</rabbit:direct-exchange>
-->
<!-- 消息对象json转换类
<bean id="jsonMessageConverter"
class="org.springframework.amqp.support.converter.Jackson2JsonMessageConverter" />
-->
<!-- 定义模版
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory" exchange="${rabbit.fanoutExchange}"
message-converter="jsonMessageConverter" />
-->
<rabbit:template id="rabbitTemplate"
connection-factory="connectionFactory" exchange="${rabbit.fanoutExchange}"
/>
</beans>
spring-rabbitmq-client.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:rabbit="http://www.springframework.org/schema/rabbit"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- <context:property-placeholder location="classpath:config.properties" ignore-unresolvable="true" />-->
<!-- 配置连接工厂 -->
<rabbit:connection-factory id="connectionFactory"
host="${rabbit.server}"
port="${rabbit.port}"
username="${rabbit.username}"
password="${rabbit.password}"
virtual-host="${rabbit.vhost}"
publisher-confirms="true"
publisher-returns="true"
channel-cache-size="5" />
<!-- 定义mq管理: 包括队列、交换器等 -->
<rabbit:admin connection-factory="connectionFactory" />
<!-- 声明队列 -->
<!-- <rabbit:queue name="queue" auto-declare="true" durable="true" />-->
<!-- 声明队列,广播模式
<rabbit:queue name="${rabbit.fanoutQueue}" auto-declare="true" durable="true" />
-->
<!-- 定义消费者 -->
<bean name="queuehandler" class="com.zgp.alarm.rabbitMq.client.RecvHandler" />
<!-- 定义消费者监听队列 -->
<rabbit:listener-container
connection-factory="connectionFactory">
<rabbit:listener ref="queuehandler" queues="${rabbit.fanoutQueue}" />
</rabbit:listener-container>
</beans>
3、RabbitMQ消息发送端类(消息发布):StatusSchedule.java (主要是(使用)注入 rabbitTemplate 类发送消息、当然也可以注入将rabbitTemplate 类到后台控制器类或工具类中或配置类中等)
package com.zgp.alarm.schedule;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zgp.alarm.bean.*;
import com.zgp.alarm.service.IAlarmStatusService;
import com.zgp.alarm.utils.CommonUtils;
import com.zgp.alarm.utils.RandomUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class StatusSchedule {
private static Logger logger = LoggerFactory.getLogger(StatusSchedule.class);
private ObjectMapper mapper = new ObjectMapper();
private RabbitTemplate rabbitTemplate;
@Autowired
private IAlarmStatusService alarmStatusService;
//处理警情接收确认状态
public void processAlarmStauts() {
try{
logger.info("【处理警情接收确认状态】:开始处理......");
List<AlarmStatus> datalist = alarmStatusService.getAlarmStatus(1);
if (datalist != null && datalist.size() > 0){
AlarmStatus updateModel = datalist.get(0);
for(int i = 0;i < datalist.size();i++){
AlarmStatus currentModel = datalist.get(i);
MsgContentStatusResult msgContentResult = new MsgContentStatusResult();
msgContentResult.setUnitName(currentModel.getUnitName());
msgContentResult.setMessageType("1");//消息类型 0:送达 1:确认
//消息内容
AlarmStatusResult alarmStatusResult = new AlarmStatusResult();
alarmStatusResult.setMsgid(RandomUtils.getUUID());//消息ID
alarmStatusResult.setMsgsendtime(CommonUtils.getCurrentDateTime());//发送消息时间
alarmStatusResult.setMsgreceiver("");
alarmStatusResult.setMsgsender("ALARM");//消息发送者(系统)
alarmStatusResult.setMsgtype("DPHZ");//调派回执信息
alarmStatusResult.setMsgcontent(msgContentResult);//消息体
// rabbitTemplate.convertAndSend(alarmStatusResult);
rabbitTemplate.convertAndSend(mapper.writeValueAsString(alarmStatusResult));
//保存发送记录。
AlarmStatusSend statusSend = new AlarmStatusSend(
1,
updateModel.getId(),
updateModel.getJjdid(),
updateModel.getConfirmTime(),
alarmStatusResult.getMsgid(),
alarmStatusResult.getMsgsendtime(),
alarmStatusResult.getMsgsender(),
alarmStatusResult.getMsgreceiver(),
alarmStatusResult.getMsgtype(),
mapper.writeValueAsString(msgContentResult)
);
Integer saveFlag = alarmStatusService.saveAlarmStatusSend(statusSend);
logger.info("【处理警情接收确认状态】:保存状态发送记录表 saveFlag={} , uploadId={} , msgid={} , msgsendtime = {}"
, saveFlag , statusSend.getUploadId() , statusSend.getMsgid() ,statusSend.getMsgsendtime());
}
//更新记录
Integer updateFlag = alarmStatusService.updateAlarmStatus(updateModel);
logger.info("【处理警情接收确认状态】:更新警情接收确认表 updateFlag={} , 当前记录={}", updateFlag , mapper.writeValueAsString(updateModel));
}else{
logger.info("【处理警情接收确认状态】:本次没有查询到待发转态记录.....");
}
logger.info("【处理警情接收确认状态】:结束处理......");
}catch (Exception e){
e.printStackTrace();
logger.error("【处理警情接收确认状态】:出现异常,异常信息为:{}",e.getMessage());
}
}
public RabbitTemplate getRabbitTemplate() {
return rabbitTemplate;
}
public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
this.rabbitTemplate = rabbitTemplate;
}
}
4、ActiveMQ消息接收端(监听)类(订阅): RecvHandler.java
package com.zgp.alarm.rabbitMq.client;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zgp.alarm.bean.AlarmStatus;
import com.zgp.alarm.bean.AlarmStatusResult;
import com.zgp.alarm.bean.Response;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
public class RecvHandler implements MessageListener {
private static final ObjectMapper mapper = new ObjectMapper();
@Override
public void onMessage(Message message) {
try {
// msg就是rabbitmq传来的消息,需要的同学自己打印看一眼
// 使用jackson解析\
String text = new String(message.getBody());
// String text2 = mapper.readValue(message.getBody() , String.class);
System.out.println("接收服务器消息1:" + text);
AlarmStatusResult response = mapper.readValue(text , AlarmStatusResult.class);
System.out.println("接收服务器消息2:msgid=" + response.getMsgid() + " , message=");
// JsonNode jsonData = MAPPER.readTree(message.getBody());
// System.out.println("我是可爱的小猪,我的id是" + jsonData.get("id").asText() + ",我的名字是" + jsonData.get("name").asText());
} catch (Exception e) {
e.printStackTrace();
}
}
}
5、spring主配置文件 applicationContext.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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-4.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">
<!-- 配置数据源 SQLServer -->
<!--
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource">
<property name="url" value="jdbc:sqlserver://192.168.0.103:1433;DatabaseName=MicroStation"/>
<property name="username" value="sa"/>
<property name="password" value="123456"/>
</bean>
-->
<!--
<context:property-placeholder location="classpath:jdbc.properties" ignore-resource-not-found="true" ></context:property-placeholder>
-->
<!--
导入配置文件 config.properties 、redis.properties
注意:如果项目中有多个属性文件,要合并在一起加载同一管理。如果分散在不同的配置文件中,只能按文件顺序加载第一个配置文件中的属性文件。
<context:property-placeholder location="classpath*:config.properties"></context:property-placeholder>
-->
<context:property-placeholder location="classpath*:config.properties"></context:property-placeholder>
<!-- 配置数据库 alarm -->
<bean id="dataSource"
class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}"/>
<property name="username" value="${jdbc.username}"/>
<property name="password" value="${jdbc.password}"/>
<!-- 配置初始化大小、bai最小、最大 -->
<property name="initialSize" value="1" />
<property name="minIdle" value="1" />
<property name="maxActive" value="20" />
<!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="60000" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="300000" />
<property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
<!-- 配置监控统计拦截的filters,去掉后监控界面sql无法统计 -->
<property name="filters" value="stat" />
</bean>
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="mapperLocations" value="classpath:/mapper/*.xml"></property>
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="plugins">
<array>
<bean class="com.github.pagehelper.PageInterceptor">
<property name="properties">
<value>
helperDialect=sqlserver2012
reasonable=true
</value>
</property>
</bean>
</array>
</property>
</bean>
<!-- DAO接口所在包名,Spring会自动查找其下的类 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.zgp.alarm.dao"></property>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 开启注解: 自动扫描 -->
<context:annotation-config></context:annotation-config>
<!-- 配置事务(注解方式) -->
<!-- 使用Annotation自动注册Bean,解决事物失效问题:在主容器中不扫描@Controller注解,在SpringMvc中只扫描@Controller注解。 -->
<!-- base-package 如果多个,用“,”分隔 -->
<context:component-scan base-package="com.zgp.alarm.service">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 定义事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 配置 Annotation 驱动,扫描@Transactional注解的类定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
<!-- MyBatis end -->
<!-- -->
<context:component-scan base-package="com.zgp.alarm.service.impl" />
<context:component-scan base-package="com.zgp.alarm.dao" />
<!-- rabbitmq 服务端 -->
<import resource="classpath:spring-rabbitmq-server.xml" />
<!-- rabbitmq 客户端测试 -->
<import resource="classpath:spring-rabbitmq-client.xml" />
<!-- 处理警情接收确认状态 -->
<!-- 每分钟执行一次 -->
<!-- <import resource="classpath:applicationContext-job-faultInfo.xml" />-->
</beans>
6、定时任务配置文件:applicationContext-job-faultInfo.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<bean id="statusSchedule" class="com.zgp.alarm.schedule.StatusSchedule">
<property name="rabbitTemplate" ref="rabbitTemplate" ></property>
</bean>
<!-- 处理警情接收确认状态: 每分钟执行一次 -->
<bean id="alarmStautsZzdJobDetail" class="org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean"
depends-on="rabbitTemplate">
<property name="targetObject" ref="statusSchedule" />
<property name="targetMethod" value="processAlarmStautsZzd" />
<property name="concurrent" value="false" />
</bean>
<bean id="alarmStautsJobTrigger" class="org.springframework.scheduling.quartz.CronTriggerFactoryBean">
<property name="jobDetail" ref="alarmStautsJobDetail" />
<property name="cronExpression" >
<value>0 */1 * * * ?</value>
</property>
</bean>
<!-- 任务调度触发器 -->
<bean autowire="no" class="org.springframework.scheduling.quartz.SchedulerFactoryBean" >
<property name="triggers">
<list>
<ref bean="alarmStautsJobTrigger" />
</list>
</property>
</bean>
</beans>
7、mybatis配置文件:mybatis-config.xml
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
<settings>
<!-- 配置日志记录工具 -->
<setting name="logImpl" value="SLF4J"/>
<!-- 将sql语句打印到控制台 -->
<setting name="logImpl" value="STDOUT_LOGGING" />
</settings>
<!-- 定义别名 -->
<typeAliases>
<typeAlias alias="alarmStatus" type="com.zgp.alarm.bean.AlarmStatus"></typeAlias>
<typeAlias alias="alarmStatusSend" type="com.zgp.alarm.bean.AlarmStatusSend" ></typeAlias>
<typeAlias alias="msgContentUnitsResult" type="com.zgp.alarm.bean.MsgContentUnitsResult"></typeAlias>
<typeAlias alias="msgContentCarsResult" type="com.zgp.alarm.bean.MsgContentCarsResult"></typeAlias>
</typeAliases>
</configuration>