我正在尝试使用默认设置从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();


我该如何解决这个问题?

最佳答案

我发现了一个类似的问题,并通过以下方式固定检查了声明的交换是否等于用于发布的通道:

@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上下文中的URI(从和到)和JUnit类必须引用相同的Exchange和队列,以防止reply-text = PRECONDITION_FAILED –虚拟主机'/'中队列'QUEUE'的参数不等效错误或类似。要检查队列/交换配置参数,请使用:

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


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

09-11 20:24