引入 Activemq 以后日志冲突问题解决方案

记录一次解决引入 Activemq 以后日志冲突问题。

发现问题

最近项目中引入了 Activemq 进行消息的推送。开始运行正常也没有发现有什么问题,有一次看控制台日志时发现有点问题 SLF4J: Class path contains multiple SLF4J bindings. 通过了解发现同事在引入 Activemq 时使用html

<dependency>
             <groupId>org.apache.activemq</groupId>
	         <artifactId>activemq-all</artifactId>
            <version>5.14.3</version>
    </dependency>

的方式,由于项目中原本就使用了 slf4j-log4j12 记录日志,而 Activemq 自己也使用了这套日志框架,从而出现日志冲突问题。 此时,服务器会根据本身的规则去选择一种方式进行日志记录,这样的话就会存在日志丢失问题。spring

解决问题

方法一

使用 maven 的 exclusions 功能。就是在引入 Activemq 时,把项目中的日志 jar 包包起来,这样作就可使得Activemq 使用项目中存在的日志 jar 。 配置以下:apache

<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 没法使用!! 因此这种方式无效api

参考 https://www.slf4j.org/codes.html服务器

方法二

不依赖 activemq-all.jar ,而是单独依赖 Activemq 的原生 jar 。 那么 Activemq 中主要分为那些包,请参考:框架

http://activemq.apache.org/version-5-initial-configuration.htmlmaven

  • activemq-broker.jaride

  • 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 包。若是项目中已经引入就不用重复引入了,若是没有则须要加上。不然初始化时就会报错。

相关文章
相关标签/搜索