RabbitMQ SpringAMQP xml配置
我正在尝试使用RabbitMQ创建一个小应用程序,其中发送方使用Spring AMQP xml配置编写,接收方使用PIKA使用python编写。 请让我知道我的方法是否正确 这是我的寄件人档案-RabbitMQ SpringAMQP xml配置,xml,rabbitmq,spring-amqp,pika,Xml,Rabbitmq,Spring Amqp,Pika,我正在尝试使用RabbitMQ创建一个小应用程序,其中发送方使用Spring AMQP xml配置编写,接收方使用PIKA使用python编写。 请让我知道我的方法是否正确 这是我的寄件人档案- import org.springframework.amqp.core.AmqpTemplate; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringAMQPRa
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringAMQPRabbitSender {
private final static String SENDER_XML = "springamqp-rabbit-sender-context.xml";
public static void main(String[] args) throws Exception {
AmqpTemplate amqpTemplate = (AmqpTemplate)(new ClassPathXmlApplicationContext(SENDER_XML)).getBean("amqpTemplate");
int messagCount = 0;
while (messagCount < 10){
amqpTemplate.convertAndSend("tp.routingkey.1", "Message # " + messagCount++);
}
System.out.println( messagCount + " message(s) sent successfully.");
}
}
这是正确的吗??我遗漏了什么吗?请建议
提前谢谢
但由于在少数几个示例中,我也看到了在发送方端配置的队列交换绑定,所以我想澄清一下
不,你绝对不需要在发送方这样做。只需要知道交换
和路由键
。队列及其绑定甚至不是接收方的问题,但我们通常在那里进行绑定
当然,正常的做法是代理端配置。正确。这就是全部?你面临一些问题吗?发送消息时是否有错误?一般来说:是什么让您来找我们寻求帮助?我只是想知道在xml文件中,是否有必要配置队列交换绑定?我有疑问,所以想澄清一下。正如我所研究的,发送方的上述xml配置应该足够了。但由于在少数几个示例中,我也看到了在发送方端配置的队列交换绑定,所以我想澄清一下。
<?xml version="1.0" encoding="utf-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:rabbit="http://www.springframework.org/schema/rabbit"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/rabbit
http://www.springframework.org/schema/rabbit/spring-rabbit-1.0.xsd">
<rabbit:connection-factory id="connectionFactory"
host="localhost" username="guest" password="guest"/>
<rabbit:admin connection-factory="connectionFactory"/>
<rabbit:template id="amqpTemplate" connection-factory="connectionFactory"
exchange="tpExchange"/>
</beans>
#!/usr/bin/env python
import pika
import sys
connection = pika.BlockingConnection(pika.ConnectionParamet(host='localhost'))
channel = connection.channel()
channel.exchange_declare(exchange='tpExchange',
exchange_type='topic')
channel.queue_declare(queue = "tpQueue")
key = "tp.routingkey.1"
channel.queue_bind(exchange='tpExchange',
queue="tpQueue",
routing_key="tp.routingkey.1")
print(' [*] Waiting for logs. To exit press CTRL+C')
def callback(ch, method, properties, body):
print(" [x] %r:%r" % (key, body))
channel.basic_consume(callback,
queue="tpQueue",
no_ack=True)
channel.start_consuming()