我找到了解决方案。RabbitMQ 的 Java 库指的是交换/队列/路由键。在 Celery 中,队列名称实际上映射到 Java 库中引用的交换。默认情况下,芹菜的队列只是“芹菜”。如果您的 Django 设置使用以下语法定义一个名为“myqueue”的队列:
CELERY_ROUTES = {
'mypackage.myclass.runworker' : {'queue':'myqueue'},
}
然后,基于 Java 的代码需要执行以下操作:
ConnectionFactory factory = new ConnectionFactory();
Connection connection = null ;
try {
connection = factory.newConnection(mqHost, mqPort);
} catch (IOException ioe) {
log.error("Unable to create new MQ connection from factory.", ioe) ;
}
Channel channel = null ;
try {
channel = connection.createChannel();
} catch (IOException ioe) {
log.error("Unable to create new channel for MQ connection.", ioe) ;
}
try {
channel.queueDeclare("celery", false, false, false, true, null);
} catch (IOException ioe) {
log.error("Unable to declare queue for MQ channel.", ioe) ;
}
try {
channel.exchangeDeclare("myqueue", "direct") ;
} catch (IOException ioe) {
log.error("Unable to declare exchange for MQ channel.", ioe) ;
}
try {
channel.queueBind("celery", "myqueue", "myqueue") ;
} catch (IOException ioe) {
log.error("Unable to bind queue for channel.", ioe) ;
}
// Generate the message body as a string here.
try {
channel.basicPublish(mqExchange, mqRouteKey,
new AMQP.BasicProperties("application/json", "ASCII", null, null, null, null, null, null, null, null, null, "guest", null, null),
messageBody.getBytes("ASCII"));
} catch (IOException ioe) {
log.error("IOException encountered while trying to publish task via MQ.", ioe) ;
}
事实证明,这只是术语的差异。