记录一次解决引入 Activemq 之后日志冲突问题。
发现问题
最近项目中引入了 Activemq 进行消息的推送。开始运行正常也没有发现有什么问题,有一次看控制台日志时发现有点问题 SLF4J: Class path contains multiple SLF4J bindings. 经过了解发现同事在引入 Activemq 时使用
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.3</version>
</dependency>
的方式,因为项目中本来就使用了 slf4j-log4j12 记录日志,而 Activemq 本身也使用了这套日志框架,从而出现日志冲突问题。 此时,服务器会根据自己的规则去选择一种方式进行日志记录,这样的话就会存在日志丢失问题。
解决问题
方法一
使用 maven 的 exclusions 功能。就是在引入 Activemq 时,把项目中的日志 jar 包包起来,这样做就可以使得Activemq 使用项目中存在的日志 jar 。 配置如下:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-all</artifactId>
<version>5.14.3</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
</exclusion>
</exclusions>
</dependency>
这样配置之后重新测试。发现问题还是存在。slf4j 官网也是这样建议解决包冲突问题的,但在这里就没有效果。 原来 activemq-all 使用的pom中使用了maven-plugin:maven-shade-plugin,来构建jar包,导致 exclusion 无法使用!! 所以这种方式无效
方法二
不依赖 activemq-all.jar ,而是单独依赖 Activemq 的原生 jar 。 那么 Activemq 中主要分为那些包,请参考:
http://activemq.apache.org/version-5-initial-configuration.html
-
activemq-broker.jar
-
activemq-client.jar
-
activemq-kahadb-store.jar
-
activemq-spring.jar
-
hawtbuf-1.11.jar
-
slf4j-api.jar
-
slf4j-log4j12.jar
-
log4j-1.2.17.jar
-
J2EE APIs which could be the j2ee.jar from Sun or your J2EE container or you could use Geronimo's freely distributable geronimo-spec-j2ee.jar. If you are inside a servlet container and being dependent on the j2ee.jar causes you troubles, the parts of the J2EE jar we are dependent on are as follows...
-
geronimo-spec-jms.jar
-
geronimo-spec-jta.jar
-
geronimo-spec-j2ee-management.jar
-
If you want to grab a J2EE specification jar we recommend the Apache repository 网上有些帖子每个人指明的依赖jar都不一样,这里还是以官网文档为准。
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-spring</artifactId> <version>5.14.3</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-kahadb-store</artifactId> <version>5.14.3</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>4.1.4.RELEASE</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> <version>5.14.3</version> </dependency> <dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.14.3</version> </dependency> <dependency> <groupId>org.fusesource.hawtbuf</groupId> <artifactId>hawtbuf</artifactId> <version>1.11</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-jms_1.1_spec</artifactId> <version>1.1.1</version> </dependency> <dependency> <groupId>org.apache.geronimo.specs</groupId> <artifactId>geronimo-j2ee-management_1.1_spec</artifactId> <version>1.0.1</version> </dependency>
注意事项
这个过程中还需要注意 jar 包的重复引入问题。 activemq-spring.jar 这个和 activemq-core.jar 重复。如果两个包都引入会出现部分类创建错误。这个需要不断的测试才能解决。 看完配置文件你可能会注意多了两个 spring 相关的 jar 包。如果项目中已经引入就不用重复引入了,如果没有则需要加上。否则初始化时就会报错。