无法正确连接RabbitMQ和SpringBoot

时间:2018-09-17 23:42:41

标签: spring spring-boot rabbitmq

我目前正在尝试构建一个发送和接收Rabbitmq消息的单一Spring Boot应用程序。老实说,我在这里进行了反复试验,没有找到合适的组合。

现在,尝试发送消息时发生错误。我的SpringBoot应用程序看起来像:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.*;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.amqp.rabbit.listener.adapter.MessageListenerAdapter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
@RestController
public class KCentralSvc {

    static final String topicExchangeName = "kcentral-exchange";

    static final String queueName = "kcentral";

    @Autowired
    private KCRepository kcRepo;

    @RequestMapping("/addItem")
    public String addItem(@RequestBody KCItem item) {
        System.out.println("Entering add item: "+item.toString());
        KCItem i = kcRepo.save(item);
        //String tmp = kcRepo.findByIsbn("1234").getName();
        KCSender sender = new KCSender();
        sender.send();
        return "this is a test "+item.toString();
    }

    @Bean
    Queue queue() {
        System.out.println("Queue() called");
        return new Queue(queueName, false);
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange(topicExchangeName);
    }

    @Bean
    Binding binding(Queue queue, TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with("kcentral.#");
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory,
            MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames(queueName);
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(KCReceiver receiver) {
        return new MessageListenerAdapter(receiver, "receiveMessage");
    }

    public static void main(String[] args) {
        System.out.println("Welcome K Services");
        SpringApplication.run(KCentralSvc.class, args);
    }

}

然后我有发送者类:

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;


public class KCSender{

      @Autowired
      private RabbitTemplate template;

      @Autowired
      private Queue queue;

      public void send() {
          String message = "Hello World!";
          if (template == null){
            System.out.println("template null");
          }
          if (queue==null){
            System.out.println("queue null");
          }
          this.template.convertAndSend(queue.getName(), message);
          System.out.println(" [x] Sent '" + message + "'");
      }

}

还涉及其他一些类,但问题似乎出在我试图“连接” rabbitTemplate和队列的方式上,因为两者均为空。任何提示将不胜感激。

0 个答案:

没有答案
相关问题