gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:25:08 25 4
gpt4 key购买 nike

我正在尝试使用默认设置从 amqp 客户端连接到 aqtivemq 服务器。它总是给出错误消息说连接被拒绝。然后我用rabbitmq服务器而不是activemq服务器尝试了它,它工作得很好。我想知道activemq是否需要linux库来通信。

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

rabbitmq 示例客户端代码

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

如何解决这个问题?

最佳答案

我发现了类似的问题,并修复了检查声明的交换是否等于用于发布的 channel ,如下所示:

@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/

关于java - Amqp 客户端未连接到 activemq 服务器。,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26421762/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com