Amqp client not connecting to activemq server.

2019-08-26 03:17发布

问题:

I'm trying connect from a amqp client to a aqtivemq server with default settings. It always gives the error message saying connection refused. Then I tried it with a rabbitmq server instead of a activemq server and it works fine. I wonder whether activemq needs a linux library to communicate.

Activemq server versions used which does not connects: 5.4.2 / 5.10.0 Rabitmq version used: 3.3.5

rabitmq sample client code

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();
    }
}

Fails in the following line of code

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

How can I solve this issue ?

回答1:

I found a similar issue and I fixed checking the exchange declared was equals to the channel used to publish, in this way:

@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();
}

Please be carefoul: the URI (from and to) on Camel Spring DSL context and JUnit class must refer to same Exchange and Queue to prevent a reply-text=PRECONDITION_FAILED – parameters for queue ‘QUEUE’ in vhost ‘/’ not equivalen error or similar. To check the queues / exchanges configuration parameter use:

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

Take a look to this: http://www.andreagirardi.it/blog/camel-and-rabbitmq-finally-how-to/