Amqp客户端未连接到activemq服务器。

时间:2014-10-17 09:16:31

标签: java rabbitmq activemq

我尝试使用默认设置从amqp客户端连接到aqtivemq服务器。它总是给出错误消息,表示连接被拒绝。然后我尝试使用rabbitmq服务器而不是activemq服务器,它工作正常。我想知道activemq是否需要一个linux库进行通信。

使用的Activemq服务器版本不连接:5.4.2 / 5.10.0 使用Rabitmq版本:3.3.5

rabitmq示例客户端代码

import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.Channel;

public class Cache {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv)
            throws java.io.IOException {

        //creating the connection factory
        ConnectionFactory factory = new ConnectionFactory();
        factory.setHost("localhost");

        //Creating a connection to the server
        Connection connection = factory.newConnection();
        Channel channel = connection.createChannel();

        //declaring a queuw
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        String message = "Hello World!";

        //publishing the queue the queue
        channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
        System.out.println(" [x] Sent '" + message + "'");

        //closing the connection
        channel.close();
        connection.close();
    }
}

在以下代码行中失败

//Creating a connection to the server
    Connection connection = factory.newConnection();

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

我发现了一个类似的问题,我修复了检查交换声明等于用于发布的频道,这样:

@Test
public void test() throws KeyManagementException, NoSuchAlgorithmException, URISyntaxException, IOException {
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("10.211.55.20");
    factory.setPort(5672);
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

    channel.exchangeDeclare("KipcastDirect", "direct", 
           true,    /* durable */
           true,    /* autodelete */
           null);   /* */

    byte[] messageBodyBytes = "Hello, world!".getBytes();

    AMQP.BasicProperties.Builder basic = new AMQP.BasicProperties.Builder();
    AMQP.BasicProperties minBasic = basic.build();

    minBasic = basic.priority(0).deliveryMode(1).build();

    channel.basicPublish("KipcastDirect", "KipcastRouting", minBasic, messageBodyBytes);
    System.out.println(" [x] Sent ");

    channel.close();
}

请注意:Camel Spring DSL上下文和JUnit类的URI(来自和来自)必须引用相同的Exchange和队列以防止回复文本= PRECONDITION_FAILED - vhost'/'中队列'QUEUE'的参数不是等效误差或类似。要检查队列/交换配置参数,请使用:

rabbitmqadmin -V / list queue
rabbitmqadmin -V test list exchanges

看看这个:http://www.andreagirardi.it/blog/camel-and-rabbitmq-finally-how-to/