我正在尝试使用RabbitMQ开发发布/订阅系统。 RabbitMQ服务器在我的笔记本电脑中。 我已经编码了消费者和生产者类。生产者已正确连接到代理,但是运行消费者类时,我有一个 com.rabbitmq.client.ShutdownSignalException 。
我的代码如下。 我有一个Functions.class:
public class Functions {
public static String EXCHANGE_NAME = "exchange";
private static ConnectionFactory factory;
/**
* Initialization for ConnectionFactory
*/
static {
factory = new ConnectionFactory();
factory.setAutomaticRecoveryEnabled(true);
factory.setHost("localhost");
}
public static Connection createConnection() {
try {
return factory.newConnection();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}}
然后是MyCosumer.java类:
public class MyConsumer {
private Connection connection;
private Channel channel;
private String queueName = "consumer";
public MyConsumer() {
}
public void connect() throws IOException {
connection = Functions.createConnection();
channel = connection.createChannel();
channel.exchangeDeclare(Functions.EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
channel.queueDeclare(queueName, true, false, false, new HashMap<String, Object>()).getQueue();
}
public void setRoutingKeys(String... keys) throws IOException {
for (String bindingKey : keys) {
channel.queueBind(queueName, Functions.EXCHANGE_NAME, bindingKey);
}
}
public void clearRoutingKeys(String... keys) throws IOException {
for (String bindingKey : keys) {
channel.queueUnbind(queueName, Functions.EXCHANGE_NAME, bindingKey);
}
}
public void recieveMessages() throws IOException {
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
String message = new String(body, "UTF-8");
System.out.println(" [x] Received '" + envelope.getRoutingKey() + "':'" + message + "'");
}
};
channel.basicConsume(queueName, true, consumer);
}
public void disconnect() throws IOException, TimeoutException {
channel.close();
connection.close();
}
}
和MyProducer.java类:
public class MyProducer {
private Connection connection;
private Channel channel;
public MyProducer() {
}
public void connect() throws Exception {
connection = Functions.createConnection();
channel = connection.createChannel();
channel.exchangeDeclare(Functions.EXCHANGE_NAME, "topic");
}
public void sendMessage(String routingKey, String message) throws IOException {
channel.basicPublish(Functions.EXCHANGE_NAME, routingKey, null, message.getBytes());
System.out.println(" [x] Sent '" + routingKey + "':'" + message + "'");
}
public void disconnect() throws IOException, TimeoutException {
channel.close();
connection.close();
}}
主要类别如下:
生产者主体:
public class MainProducer {
public static String[] categorias = { "Beauty", "Drinks", "Diary", "Vegetable" };
public static void main(String[] args) throws Exception {
MyProducer productor = new MyProducer();
productor.connect();
send(productor, categorias[1], "Makeup", "Discount 20% in loreal lipstick");
send(productor, categorias[0], "Soap", "2x1 in Herbal Essence soap");
send(productor, categorias[0], "Makeup", "Discount 10% in l'ocitane lipstick");
send(productor, categorias[0], "Loreal Makeup", "Discount 20% in loreal lipstick");
productor.disconnect();
}
private static void send(MyProducer productor, String categoria, String producto, String mensaje) {
String routingKey = "offers." + categoria + "." + producto;
try {
productor.sendMessage(routingKey, mensaje);
} catch (IOException e) {
e.printStackTrace();
}
}}
主要消费者:
public class MainConsumer {
public static void main(String[] args) throws Exception {
MyConsumer consumer = new MyConsumer();
consumer.connect();
consumer.setRoutingKeys("kern");
consumer.recieveMessages();
}}
获得的异常是:
Exception in thread "main" java.io.IOException
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:124)
at com.rabbitmq.client.impl.AMQChannel.wrap(AMQChannel.java:120)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:142)
at com.rabbitmq.client.impl.ChannelN.queueDeclare(ChannelN.java:952)
at com.rabbitmq.client.impl.recovery.AutorecoveringChannel.queueDeclare(AutorecoveringChannel.java:333)
at main.MyConsumer.connect(MyConsumer.java:44)
at main.MainConsumer.main(MainConsumer.java:7)
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - home node 'rabbit@souhaila-UX430UAR' of durable queue 'consumer' in vhost '/' is down or inaccessible, class-id=50, method-id=10)
at com.rabbitmq.utility.ValueOrException.getValue(ValueOrException.java:66)
at com.rabbitmq.utility.BlockingValueOrException.uninterruptibleGetValue(BlockingValueOrException.java:36)
at com.rabbitmq.client.impl.AMQChannel$BlockingRpcContinuation.getReply(AMQChannel.java:443)
at com.rabbitmq.client.impl.AMQChannel.privateRpc(AMQChannel.java:263)
at com.rabbitmq.client.impl.AMQChannel.exnWrappingRpc(AMQChannel.java:136)
... 4 more
Caused by: com.rabbitmq.client.ShutdownSignalException: channel error; protocol method: #method<channel.close>(reply-code=404, reply-text=NOT_FOUND - home node 'rabbit@souhaila-UX430UAR' of durable queue 'consumer' in vhost '/' is down or inaccessible, class-id=50, method-id=10)
at com.rabbitmq.client.impl.ChannelN.asyncShutdown(ChannelN.java:509)
at com.rabbitmq.client.impl.ChannelN.processAsync(ChannelN.java:340)
at com.rabbitmq.client.impl.AMQChannel.handleCompleteInboundCommand(AMQChannel.java:162)
at com.rabbitmq.client.impl.AMQChannel.handleFrame(AMQChannel.java:109)
at com.rabbitmq.client.impl.AMQConnection.readFrame(AMQConnection.java:643)
at com.rabbitmq.client.impl.AMQConnection.access$300(AMQConnection.java:47)
at com.rabbitmq.client.impl.AMQConnection$MainLoop.run(AMQConnection.java:581)
at java.lang.Thread.run(Thread.java:748)