Rabbitmq Spring AMQP项目,2个队列';s

Rabbitmq Spring AMQP项目,2个队列';s,rabbitmq,spring-amqp,Rabbitmq,Spring Amqp,我正在做一个项目,涉及两个队列,多个侦听器与它们交互。 流量: 新的HTTP请求到达服务器,然后它被转换成一个将成为消息的对象 此消息必须在两个队列中发布 我有两种类型的侦听器,它们从每个队列中获取消息,然后我想做什么就做什么 我一直在阅读,最好的方法是用扇出交换。这是我的密码: listener-configuration.xml <!-- CREATE CONNECTION FACTORY --> <rabbit:connection-factory id="conne

我正在做一个项目,涉及两个队列,多个侦听器与它们交互。 流量:

  • 新的HTTP请求到达服务器,然后它被转换成一个将成为消息的对象
  • 此消息必须在两个队列中发布
  • 我有两种类型的侦听器,它们从每个队列中获取消息,然后我想做什么就做什么
我一直在阅读,最好的方法是用扇出交换。这是我的密码:

listener-configuration.xml

<!-- CREATE CONNECTION FACTORY -->
<rabbit:connection-factory id="connectionFactory"
    host="localhost" username="guest" password="guest" />

<rabbit:admin connection-factory="connectionFactory" />

<!-- <!-- RABBIT QUEUE'S -->
<rabbit:queue id="trashroute.rabbit.queue" name="trashroute.rabbit.queue" auto-delete="false" auto-startup=false
    durable="true" />
<!-- Webapp Queue -->
<rabbit:queue id="trashroute2.rabbit.queue" name="trashroute2.rabbit.queue" auto-delete="false" auto-startup=false
    durable="true" /> 

<!-- CREATE AN EXCHANGE AND BIND THE QUEUE WITH MY.ROUTINGKEY.* TO THE EXCHANGE -->
<rabbit:fanout-exchange id="myExchange" name="trashroute-exchange">
    <rabbit:bindings>
        <rabbit:binding queue="trashroute.rabbit.queue"></rabbit:binding>
        <rabbit:binding queue="trashroute2.rabbit.queue"></rabbit:binding>
    </rabbit:bindings>
</rabbit:fanout-exchange>

<!-- CREATE THE RABBIT TEMPLATES -->
<rabbit:template connection-factory="connectionFactory" exchange="myExchange" queue="trashroute.rabbit.queue"/>
<rabbit:template connection-factory="connectionFactory" exchange="myExchange" queue="trashroute2.rabbit.queue"/>

<!-- INSTANTIATE THE LISTENERS -->
<bean id="persistenceListener" class="trashroute.rabbitmq.listener.PersistenceListener" />
<bean id="webappListener" class="trashroute.rabbitmq.listener.WebappListener" />

<!-- CREATE THE JsonMessageConverter BEAN -->
<bean id="jsonMessageConverter" class="org.springframework.amqp.support.converter.JsonMessageConverter" />

<!-- GLUE THE LISTENER AND QUEUE TO THE LISTENER CONTAINER -->
<rabbit:listener-container id="listenerContainer"
    connection-factory="connectionFactory" message-converter="jsonMessageConverter">
    <rabbit:listener ref="persistenceListener" queues="trashroute.rabbit.queue" />
    <rabbit:listener ref="webappListener" queues="trashroute2.rabbit.queue" />
</rabbit:listener-container>
<!--  First following line creates a rabbit connection factory with specified parameters -->
<rabbit:connection-factory id="connectionFactory" host="localhost" username="guest" password="guest" />

<!-- Obtain admin rights to create an exchange -->
<rabbit:admin connection-factory="connectionFactory" />

<!-- Create a bean which can send message to trashroute-exchange for the Java program to call -->
<rabbit:template id="template" connection-factory="connectionFactory"  exchange="myExchange"
message-converter="jsonMessageConverter" />


<bean id="rabbitTemplate" class="org.springframework.amqp.rabbit.core.RabbitTemplate">
<property name="connectionFactory" ref="rabbitConnectionFactory"/>
<property name="messageConverter">
    <bean class="org.springframework.amqp.support.converter.JsonMessageConverter"/>
</property>
Sender main配置.java

@Configuration
public class MainConfiguration {

protected final String persistenceQueue = "trashroute.rabbit.queue";
protected final String webappQueue = "trashroute2.rabbit.queue";

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory("localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}

@Bean
public AmqpAdmin amqpAdmin() {
    return new RabbitAdmin(connectionFactory());
}

@Bean
public DataController DataController(){
    return new DataController();
}

@Bean
// Every queue is bound to the default direct exchange
public Queue persistenceQueue() { 
    //Create a new queue with an specific name and the durability value in true.
    return new Queue(this.persistenceQueue, true);
}

@Bean
public Queue webappQueue() {
    //Create a new queue with an specific name and the durability value in true.
    return new Queue(this.webappQueue, true);
}
}
@Configuration
public class SenderConfiguration {

protected final String persistenceQueue = "trashroute.rabbit.queue";
protected final String webappQueue = "trashroute2.rabbit.queue";

//Create the Template
@Bean
public RabbitTemplate rabbitTemplate() {
    RabbitTemplate template = new RabbitTemplate(connectionFactory());
    template.setMessageConverter(new JsonMessageConverter());
    return template;
}

@Bean
public ConnectionFactory connectionFactory() {
    CachingConnectionFactory connectionFactory = new CachingConnectionFactory(
            "localhost");
    connectionFactory.setUsername("guest");
    connectionFactory.setPassword("guest");
    return connectionFactory;
}

@Bean
public IServiceManager scheduledProducer() {
    return new ServiceManagerImpl();
}

@Bean
public BeanPostProcessor postProcessor() {
    return new ScheduledAnnotationBeanPostProcessor();
}

}
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    AnnotationConfigApplicationContext context;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Waiting five seconds...");
        Thread.sleep(5000);
        System.out.println("Sending message...");

        RabbitTemplate rabbitTemplate = (RabbitTemplate) context.getBean("rabbitTemplate");

        rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_TRASHROUTE_QUEUE, "Hello from trashroute queue!");
        rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_WEBAPP_QUEUE, "Hello from webapp queue!");

        Thread.sleep(10000);
        context.close();
    }
}

谁能告诉我我做错了什么?两个侦听器中的一个工作正常,第二个从不读取消息。

基于上述场景,我尝试创建一个使用Spring Java Config的示例应用程序

消息被发布到
trashroute
webapp
队列,相应的接收者(
persistence
webapp
)接收消息

java(包含发送方和接收方的配置)

PersistenceListener.java

public class PersistenceListener {

    public void receiveMessage(String message) {
        System.out.println("Persistence Listener: Messsage Received <" + message + ">");
    }
}
public class WebAppListener {
    public void receiveMessage(String message) {
        System.out.println("WebAppListener: Message Received <" + message + ">");
    }
}

希望这会有所帮助。尽管如果您想在生产中使用它,您需要重构代码。

您同时使用JavaConfig和XML配置吗?它们看起来是多余的。是的,因为如果它们在XML中,配置中的某些点就不起作用。这可能是因为其他一些原因。永远不要混合使用XML和Java配置。使用它们中的任何一个。
@SpringBootApplication
public class Application implements CommandLineRunner {

    @Autowired
    AnnotationConfigApplicationContext context;

    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(Application.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        System.out.println("Waiting five seconds...");
        Thread.sleep(5000);
        System.out.println("Sending message...");

        RabbitTemplate rabbitTemplate = (RabbitTemplate) context.getBean("rabbitTemplate");

        rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_TRASHROUTE_QUEUE, "Hello from trashroute queue!");
        rabbitTemplate.convertAndSend(RabbitConfiguration.BROADCAST_WEBAPP_QUEUE, "Hello from webapp queue!");

        Thread.sleep(10000);
        context.close();
    }
}