Весенний кроликMQ Hello World - PullRequest
0 голосов
/ 05 декабря 2018

Я хочу отправить сообщение через локальную очередь RabbitMQ.Просто "Hello World".И получите в той же программе.

Я следую этому уроку RabbitMQ Tutorial , который объясняет, как это сделать.Я сделал этот урок по крайней мере 5 раз, но никогда не работает.Я работаю над затмением.Я строю с Maven и нет ошибок в этом процессе.Но когда я запускаю его, ничего не получается.

Это вывод:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.1.1.RELEASE)

Ready ... running for 10000ms

Но ничего не происходит.Я написал код, как учебник.На самом деле, я скачал код RabbitMQ из его репозитория: Репозиторий Github для попытки, но ничего.

Что я делаю неправильно?

ОБНОВЛЕНИЕ

Мой код похож на учебник.Мой первый пакет: org.springframework.amqp.tutorials.rabbitmqamqptutorials

У меня есть этот код в пакете:

RabbitAmqpTutorialsRunner.java:

package org.springframework.amqp.tutorials.rabbitmqamqptutorials;

import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.tutorials.tut1.Tut1Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;


public class RabbitAmqpTutorialsRunner implements CommandLineRunner {

@Value("${tutorial.client.duration:0}")
private int duration;

@Autowired
private ConfigurableApplicationContext ctx;

@Override
public void run(String... arg0) throws Exception {
    System.out.println("Ready ... running for " + duration + "ms");
    Thread.sleep(duration);
    ctx.close();
}

}

RabbitAmqpTutorialsApplication.java:

package org.springframework.amqp.tutorials.rabbitmqamqptutorials;

 import org.springframework.amqp.core.AmqpTemplate;
 import org.springframework.amqp.rabbit.annotation.EnableRabbit;
 import org.springframework.boot.CommandLineRunner;
 import org.springframework.boot.SpringApplication;
 import org.springframework.boot.autoconfigure.SpringBootApplication;
 import org.springframework.context.ApplicationContext;
 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 import org.springframework.context.annotation.Bean;
 import org.springframework.context.annotation.Profile;
 import org.springframework.scheduling.annotation.EnableScheduling;



 @EnableRabbit 
 @SpringBootApplication
 @EnableScheduling
 public class RabbitmqAmqpTutorialsApplication {

 @Profile("usage_message")
    @Bean
    public CommandLineRunner usage() {
        return new CommandLineRunner() {

            @Override
            public void run(String... arg0) throws Exception {
                System.out.println("This app uses Spring Profiles to control its behavior.\n");
                System.out.println("Sample usage: java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
            }
        };
    }

    @Profile("!usage_message")
    @Bean
    public CommandLineRunner tutorial() {
        return new RabbitAmqpTutorialsRunner();
    }

    public static void main(String[] args) throws Exception {


        SpringApplication.run(RabbitmqAmqpTutorialsApplication.class, "java -jar rabbit-tutorials.jar --spring.profiles.active=hello-world,sender");
    }

}

У меня есть еще один пакет org.springframework.amqp.tutorials.tut1 с тремя классами:

Tut1Config.java:

package org.springframework.amqp.tutorials.tut1;

import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Queue;

import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitAdmin;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;




@Profile({"tut1","hello-world"})
@Configuration
public class Tut1Config {

    @Bean
    public Queue hello() {
        return new Queue("hello");
    }

    @Profile("receiver")
    @Bean
    public Tut1Receiver receiver() {
       return new Tut1Receiver();
   } 

    @Profile("sender")
    @Bean
    public Tut1Sender sender() {
       return new Tut1Sender();
    }
 }

Tut1Receiver.java:

package org.springframework.amqp.tutorials.tut1;

import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;

@RabbitListener(queues = "hello")
public class Tut1Receiver {

     @RabbitHandler
     public void receive(String in) {
          System.out.println(" [x] Received '" + in + "'");
     }
}

Tut1Sender.java:

package org.springframework.amqp.tutorials.tut1;

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 Tut1Sender {

     @Autowired
     private RabbitTemplate template;

     @Autowired
     private Queue queue;

     @Scheduled(fixedDelay = 1000, initialDelay = 500)
     public void send() {   
         String message = "Hello World!";
         this.template.convertAndSend(queue.getName(), message);
         System.out.println(" [x] Sent '" + message + "'");
    }

}

и конфигурация:

application.yml:

spring:
  profiles:
    active: usage_message

logging:
  level:
    org: ERROR

tutorial:
  client:
    duration: 10000
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...