2016-12-02 230 views
2

我想創建一個RabbitMQ java客戶端。我創建了一個Maven項目是這樣的:運行RabbitMQ java客戶端

mvn archetype:generate -DgroupId=com.mycompany.app -DartifactId=rabbitmq-test -DarchetypeArtifactId=maven-archetype-quickstart -DinteractiveMode=false 

裏面我加入(如在他們的網站上描述)

<dependency> 
    <groupId>com.rabbitmq</groupId> 
    <artifactId>amqp-client</artifactId> 
    <version>4.0.0</version> 
</dependency> 

和單主文件我寫裏面的pom.xml文件:

package com.mycompany.app; 
import com.rabbitmq.client.ConnectionFactory; 
import com.rabbitmq.client.Connection; 
import com.rabbitmq.client.Channel; 


/** 
* Hello world! 
* 
*/ 
public class App 
{ 
     public static void main(String[] args) throws java.util.concurrent.TimeoutException, java.io.IOException { 

       ConnectionFactory factory = new ConnectionFactory(); 
       factory.setHost("localhost"); 
       Connection connection = factory.newConnection(); 
       Channel channel = connection.createChannel(); 
     } 
} 

沒有RabbitMQ的東西里面的代碼我運行

mvn package 
java -cp target/rabbitmq-test-1.0-SNAPSHOT.jar com.mycompany.app.App 

和代碼運行良好。 如果我包括進口和RabitMQ代碼,然後,如果我運行像我得到

Exception in thread "main" java.lang.NoClassDefFoundError: com/rabbitmq/client/ConnectionFactory 
    at com.mycompany.app.App.main(App.java:17) 

之前基本上是指「無法找到RabitMQ罐子」對吧? 但在我的第一個mvn package運行時,maven在某處下載了amqp-client-4.0.0.jar。那麼我該如何添加這個jar以及運行?

我試圖下載這個瓶子我自己,當我像這樣運行:

java -cp target/rabbitmq-test-1.0-SNAPSHOT.jar:target/amqp-client-4.0.0.jar:. com.mycompany.app.App 

我得到:

Exception in thread "main" java.lang.NoClassDefFoundError: org/slf4j/LoggerFactory 

所以,我下載了這個罐子,當我像這樣運行:

java -cp target/rabbitmq-test-1.0-SNAPSHOT.jar:target/amqp-client-4.0.0.jar:target/slf4j-api-1.7.21.jar:. com.mycompany.app.App 

我得到:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder". 
SLF4J: Defaulting to no-operation (NOP) logger implementation 
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details. 

我加

<dependency> 
     <groupId>org.slf4j</groupId> 
     <artifactId>slf4j-api</artifactId> 
     <version>1.7.21</version> 
</dependency> 

,但還是一樣...... 那麼,有什麼問題呢?除了我在java上無能爲力?

編輯: 在RabbitMQ的日誌,我看到:

=INFO REPORT==== 2-Dec-2016::15:18:18 === 
accepting AMQP connection <0.1713.0> (127.0.0.1:55976 -> 127.0.0.1:5672) 

當我得到staticLoggerBinder錯誤。那是什麼意思?發生了什麼事?

+0

您需要在您的代碼在類路徑使用的所有JAR文件,當你執行你的主類 – 2016-12-03 19:11:51

+0

@LutzHorn是的,但是這裏缺少的罐子是什麼? –

+0

包含'org.slf4j.impl.StaticLoggerBinder'的那個。見http://www.slf4j.org/codes。html#StaticLoggerBinder – 2016-12-05 07:41:34

回答

0

您需要在POM的maven-assembly-plugin下定義配置「jar-with-dependencies」。因此,當您使用包裝目標運行時,maven將在包裝內部包含所有必需的罐子。

否則,您必須手動提供所有必需的罐子。

0

您必須添加依賴關係的RabbitMQ在你的pom.xml文件檢查下面的依賴代碼

<modelVersion>4.0.0</modelVersion> 
    <groupId>com.mmi</groupId> 
    <artifactId>PushNotification</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 

    <properties> 
     <spring.version>4.3.9.RELEASE</spring.version> 
     <log4j.version>1.2.17</log4j.version> 
     <aspectj.version>1.7.4</aspectj.version> 
     <java.version>1.8</java.version> 
    </properties> 

    <dependencies> 
     <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.security</groupId> 
      <artifactId>spring-security-config</artifactId> 
      <version>3.0.1.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework</groupId> 
      <artifactId>spring-context-support</artifactId> 
      <version>${spring.version}</version> 
     </dependency> 

     <dependency> 
      <groupId>org.springframework.amqp</groupId> 
      <artifactId>spring-amqp</artifactId> 
      <version>1.7.2.RELEASE</version> 
     </dependency> 
     <dependency> 
      <groupId>org.springframework.amqp</groupId> 
      <artifactId>spring-rabbit</artifactId> 
      <version>1.7.2.RELEASE</version> 
     </dependency> 
    </dependencies>